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)?
A line in an XML file is not really the same as an element. In your first example you don’t really count the lines – but the number of elements.
An XML file could look like this:
Or the exact same XML file can look like this:
which the XSLT will interpet exactly the same – it will not really bother with the line breaks.
Therefore it’s hard to show line numbers in the way you want using XSLT – it’s not really meant for for that kind of parsing.
Someone correct me if I’m wrong, but I’d say you would need Javascript or some other scripting language to do what you want.