How to count the number of iterations in xsl:for-each

时间:2016-03-02 11:01:09

标签: xslt

Inside an xsl:for-each loop I need the number of iterations to put in a rowspan attribute.

Is there in xsl something similar to the following?

for(int i = 0; i < collection.length(); i++)

I came to

<xsl:for-each select="foo">
    <xsl:if test="position()=1">
      <td>
        <xsl:attribute name="rowspan">
          <xsl:value-of select=...collection.length()...
            etc

so the running counter i is no problem. But in the first iteration I need to get the total number of iterations, or the analogy to collection.length(), for the rowspan= number.

2 个答案:

答案 0 :(得分:2)

您可以使用last()功能。或者在调用xsl:for-each之前将变量定义为count(foo)

答案 1 :(得分:1)

进一步简化你可以使用

<xsl:if test="position()=last()"> 

而不是在变量中计算