How to get a counter inside xsl:for-each loop that would reflect the number of current element processed.
For example my source XML is
position()
. E.G.:
<countNo><xsl:value-of select="position()" /></countNo>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<newBooks>
<xsl:for-each select="books/book">
<newBook>
<countNo><xsl:value-of select="position()"/></countNo>
<title>
<xsl:value-of select="title"/>
</title>
</newBook>
</xsl:for-each>
</newBooks>
</xsl:template>
</xsl:stylesheet>
Try inserting <xsl:number format="1. "/><xsl:value-of select="."/><xsl:text>
in the place of ???.
Note the "1. " - this is the number format. More info: here
You can also run conditional statements on the Postion() which can be really helpful in many scenarios.
for eg.
<xsl:if test="(position( )) = 1">
//Show header only once
</xsl:if>
Try:
<xsl:value-of select="count(preceding-sibling::*) + 1" />
Edit - had a brain freeze there, position() is more straightforward!