Increment a value in XSLT

Viewed 103804

I'm reasonably new to xlst and am confused as to whether there is any way to store a value and change it later, for example incrementing a variable in a loop.

I'm a bit baffled by not being able to change the value of a after it's set doesn't make sense to me, making it more of a constant.

For example I want to do something like this:

<xsl:variable name="i" select="0" />
<xsl:for-each select="data/posts/entry">
    <xsl:variable name="i" select="$i + 1" />
    <!-- DO SOMETHING -->
</xsl:for-each>

If anyone can enlighten me on whether there is an alternative way to do this
Thanks

4 Answers

Another approach (IE specific) we can follow is to take help of Javascript using "msxsl:script"

<msxsl:script implements-prefix='yourprefix' language='JavaScript'>  
   <![CDATA[var counter=1; function getCounter(){return counter++;}]]>
</msxsl:script>  

Then we can call this method in our xsl

<xsl:value-of select="yourprefix:getCounter()" />
Related