Click to See Complete Forum and Search --> : Counting in <xsl:for-each>


grbrg
04-01-2004, 10:22 AM
How can I count while running through a <xsl:for-each>? What I want to do is basically generate different names for every element parsed this way: name0, name1, name 2, ...

What I though of was generating a variable and then adding 1 each time the for-each is run:


<xsl:variable name="counter" select="0" />
<xsl:for-each select="...">
<xsl:variable name="counter" select="sum($counter+1)" />
</xsl:for-each>


This does not seem to work. Excuse my ignorance - I'm sure there is a much simpler solution, but I'm quite new at XSL and it's just a little thing I need to do. Thanks for you help!

grbrg
04-02-2004, 01:52 AM
I just found a solution for my problem:


<xsl:value-of select="(count(preceding::ELEMENT)"/>


This counts the number of ELEMENTs that were previously handled - exactly what I needed! :)

grbrg
04-02-2004, 02:20 AM
Btw, the function "position()" returns the position of the element in the element list, for those that are interested... ;)