我每行只需要3个产品。每个产品都在模板“OneProduct”(不包括在内)中,推杆大多数Table标签都在此表之外。以下实际上可行,但是,它是如何以正确的方式完成的?
<xsl:if test="(position() mod 3) = 1">
<xsl:text disable-output-escaping="yes">
<![CDATA[<TR name="PROD_ROW">]]>
</xsl:text>
</xsl:if>
<TD width="33%" align="center" name="PRODUCT_CELL">
<xsl:call-template name="OneProduct">
<xsl:with-param name="productId" select="$productId" />
</xsl:call-template>
</TD>
<xsl:if test="( (position()+1) mod 3 = 1)">
<xsl:text disable-output-escaping="yes">
<![CDATA[</TR>]]>
</xsl:text>
</xsl:if>
答案 0 :(得分:1)
在回答您的问题时,这不是首选方式。这是你可以用程序语言做的事情;遍历增加计数器的元素,然后在到达第3个,第6个等元素时输出分隔元素。但是XSLT是一种函数式语言,需要采用不同的方法。
您可以使用 xsl:apply-templates 来选择每行中第一个元素。假设你有像这样的XML
<products>
<product id="1" name="Product 1" />
<product id="2" name="Product 2" />
<product id="3" name="Product 3" />
<product id="4" name="Product 4" />
<product id="5" name="Product 5" />
</products>
然后您的 xsl:apply-templates 将是这样的:
<xsl:apply-templates select="product[position() mod 3 = 1]" />
在与产品元素匹配的模板中,您将选择行中的所有元素(即当前元素以及以下两个元素)
<xsl:apply-templates
select="self::*|following-sibling::product[position() < 3]" mode="cell" />
请注意此处使用模式,因为您将有两个匹配产品的模板,您需要区分它们。
最后,你只需要这个'cell'模板来输出产品。
<xsl:template match="product" mode="cell">
<td>
<xsl:value-of select="@name" />
</td>
</xsl:template>
唯一需要担心的是,如果每行没有确切数量的单元格(例如,在此示例中有五个产品,那么最后一行只有两个单元格。
尝试以下XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="/*">
<table>
<xsl:apply-templates select="product[position() mod 3 = 1]" />
</table>
</xsl:template>
<xsl:template match="product">
<tr>
<xsl:apply-templates select="self::*|following-sibling::product[position() < 3]" mode="cell" />
</tr>
</xsl:template>
<xsl:template match="product" mode="cell">
<td>
<xsl:if test="position() < 3 and not(following-sibling::product[1])">
<xsl:attribute name="colspan">
<xsl:value-of select="4 - position()" />
</xsl:attribute>
</xsl:if>
<xsl:value-of select="@name" />
</td>
</xsl:template>
</xsl:stylesheet>
当应用于示例XML时,输出以下内容
<table>
<tr>
<td>Product 1</td>
<td>Product 2</td>
<td>Product 3</td>
</tr>
<tr>
<td>Product 4</td>
<td colspan="2">Product 5</td>
</tr>
</table>