Recognize first loop run in XSLT
foreach, functional-programming, xslt
Solution
I think the easiest way is to check the position of the current node. It's also faster than checking the existence of preceding value elements in the tree (and will still work if `xsl:sort` is added to the loop):
<xsl:for-each select="browser/value">
<xsl:if test="position()=1">
do something here
</xsl:if>
<xsl:value-of select="blah"/>
</xsl:for-each>
Problem
I am pretty new to XSLT and might be looking at my problem from the wrong angle - coming from languages such as C++ and Java. I hope someone can help me out. I want to make a loop call (xsl:for-each) in XSLT and do something specific on the first run through the loop. In other languages I would use a status variable for this, but variables cannot change their value in XSLT as I have learned, so how can I solve this problem? Here is what I want to do. The upper case part in the if clause is of course fake and represents my problem. ``` <xsl:for-each select="browser/value"> <xsl:if test="FIRST TIME IN LOOP"> do something once </xsl:if> <xsl:value-of select="current()" /> </xsl:for-each> ``` Thanks alot! Henrik