Roland
Roland

Reputation: 5232

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

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.

Upvotes: 1

Views: 2077

Answers (2)

Rama Krishnam
Rama Krishnam

Reputation: 73

just further simplify you can use

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

instead of taking count in the variable

Upvotes: 1

michael.hor257k
michael.hor257k

Reputation: 117140

You could use the last() function. Or define a variable as count(foo) before calling xsl:for-each.

Upvotes: 2

Related Questions