[英]xsl for-each: add code block every n rows?
我試圖將一些表示圖像庫的xml轉換為html表。 (必須使用html而不是css)。 如何使用xsl每隔六個左右添加行中斷</tr><tr>
?
我有這個:
<xsl:for-each select="//email/gallery" >
<td>
<img>
<xsl:attribute name="src">
<xsl:value-of select="gallery-image-location"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="gallery-image-alt"/>
</xsl:attribute>
</img>
</td>
<xsl:if test="????">
</tr>
<tr>
</xsl:if>
<xsl:for-each>
在Javascript中我會做類似的事情:
for (i=0; i<gallery.length; i++) {
htm += '<td><img src="' +
gallery[i].gallery-image-location +
'" alt="'+ gallery[i].gallery-image-alt +'"></td>';
if (i%6 == 5 && i != gallery.length-1) {
htm += '</tr><tr>';
}
}
如何使用xsl每隔六個左右添加行中斷?
在XSLT中你沒有!
XSLT處理節點 ,而不是標簽。
這是XSLT的位置分組方式 :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="gallery[position() mod 6 = 1]">
<tr>
<xsl:apply-templates mode="proc"
select=".|following-sibling::gallery[not(position() > 5)]"
/>
</tr>
</xsl:template>
<xsl:template match="gallery" mode="proc">
<td>
<img src="{gallery-image-location}" alt="{gallery-image-alt}"/>
</td>
</xsl:template>
<xsl:template match="gallery[not(position() mod 6 = 1)]"/>
</xsl:stylesheet>
當此轉換應用於以下XML文檔時 :
<email>
<gallery>
<gallery-image-location>http://server/picts/1</gallery-image-location>
<gallery-image-alt>Description 1</gallery-image-alt>
</gallery>
<gallery>
<gallery-image-location>http://server/picts/2</gallery-image-location>
<gallery-image-alt>Description 2</gallery-image-alt>
</gallery>
<gallery>
<gallery-image-location>http://server/picts/3</gallery-image-location>
<gallery-image-alt>Description 3</gallery-image-alt>
</gallery>
<gallery>
<gallery-image-location>http://server/picts/41</gallery-image-location>
<gallery-image-alt>Description 4</gallery-image-alt>
</gallery>
<gallery>
<gallery-image-location>http://server/picts/5</gallery-image-location>
<gallery-image-alt>Description 5</gallery-image-alt>
</gallery>
<gallery>
<gallery-image-location>http://server/picts/6</gallery-image-location>
<gallery-image-alt>Description 6</gallery-image-alt>
</gallery>
<gallery>
<gallery-image-location>http://server/picts/7</gallery-image-location>
<gallery-image-alt>Description 7</gallery-image-alt>
</gallery>
<gallery>
<gallery-image-location>http://server/picts/8</gallery-image-location>
<gallery-image-alt>Description 8</gallery-image-alt>
</gallery>
<gallery>
<gallery-image-location>http://server/picts/9</gallery-image-location>
<gallery-image-alt>Description 9</gallery-image-alt>
</gallery>
</email>
產生了想要的正確結果 :
<tr>
<td>
<img src="http://server/picts/1" alt="Description 1"/>
</td>
<td>
<img src="http://server/picts/2" alt="Description 2"/>
</td>
<td>
<img src="http://server/picts/3" alt="Description 3"/>
</td>
<td>
<img src="http://server/picts/41" alt="Description 4"/>
</td>
<td>
<img src="http://server/picts/5" alt="Description 5"/>
</td>
<td>
<img src="http://server/picts/6" alt="Description 6"/>
</td>
</tr>
<tr>
<td>
<img src="http://server/picts/7" alt="Description 7"/>
</td>
<td>
<img src="http://server/picts/8" alt="Description 8"/>
</td>
<td>
<img src="http://server/picts/9" alt="Description 9"/>
</td>
</tr>
如果您使用的是XSLT 2
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="email">
<xsl:for-each-group select="gallery" group-by="(position() - 1) idiv 6">
<tr>
<xsl:apply-templates select="current-group()"/>
</tr>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="gallery">
<td>
<img src="{gallery-image-location}" alt="{gallery-image-alt}"/>
</td>
</xsl:template>
</xsl:stylesheet>
首先,假設您使用的是XML或HTML的輸出格式,我認為您不能像使用</tr><tr>
段那樣放置不匹配的標簽。 XSL(在這些模式下)不僅像你的Javascript一樣產生字符串輸出。 (雖然我可能錯了。)
你在那里做的與尋呼密切相關; 你可能會看一下分頁腳本。
這是我的一個(未經測試的)建議:
<!-- For every sixth item, starting with the first... -->
<xsl:for-each select="//email/gallery[position() mod 6 = 1]">
<tr>
<!-- Get that item's position... -->
<xsl:variable name="thisPos" select="position()" />
<!-- and select the six (or less) items starting with that position. -->
<xsl:for-each select="//email/gallery[position() >= $thisPos and position() < $thisPos + 6]">
<td><img>
<xsl:attribute name="src">
<xsl:value-of select="gallery-image-location"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="gallery-image-alt"/>
</xsl:attribute>
</img></td>
</xsl:for-each>
</tr>
</xsl:for-each>
哦,和IIRC,循環的內部也可以縮短一點:
<td><img src="{gallery-image-location}" alt="{gallery-image-alt}" /></td>
那些花括號將有助於在長腳本上保存你的理智。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.