XSLT line counter - is it that hard?

Posted by Mr AH on Stack Overflow See other posts from Stack Overflow or by Mr AH
Published on 2010-05-26T20:28:01Z Indexed on 2010/05/26 20:31 UTC
Read the original article Hit count: 299

Filed under:

I have cheated every time I've needed to do a line count in XSLT by using JScript, but in this case I can't do that. I simply want to write out a line counter throughout an output file. This basic example has a simple solution:

<xsl:for-each select="Records/Record">
   <xsl:value-of select="position()"/>
</xsl:for-each>

Output would be:

1

2

3

4

etc...

But what if the structure is more complex with nested foreach's :

<xsl:for-each select="Records/Record">
   <xsl:value-of select="position()"/>
   <xsl:for-each select="Records/Record">
       <xsl:value-of select="position()"/>
   </xsl:for-each>
</xsl:for-each>

Here, the inner foreach would just reset the counter (so you get 1, 1, 2, 3, 2, 1, 2, 3, 1, 2 etc). Does anyone know how I can output the position in the file (ie. a line count)?

© Stack Overflow or respective owner

Related posts about xslt