Reputation: 103
I know this question is asked many times. I tried the same as the answer provided for those question. It didn't works for me. I just want to increment a variable if Value of attribute is mentioned twice. i tried the below method which also not works
<xsl:variable name="counter" select="0" saxon:assignable="yes"/>
<xsl:for-each select="//MARKER/*/*/*">
<xsl:if test="contains(@Name,'Black' )">
<saxon:assign name="counter" select="$counter+1"/>
</xsl:if>
</xsl:for-each>
After this also value of counter is zero instead of 2. Can any one suggest me, how to achieve this
Upvotes: 0
Views: 866
Reputation: 117140
Why don't you simply count the nodes you are interested in, e.g.
<xsl:value-of select="count(//MARKER/*/*/*[contains(@Name,'Black' )]"/>
Thanks it works. But generally. How can we increment the variable?
In general, variables in XSLT 1.0 are immutable.
Saxon 6.5 provides an exception to the rule in the form of the saxon:assignable
and saxon:assign
extensions. If you were using Saxon 6.5, your attempt would probably work - see a demo here: http://xsltransform.net/94hvTzQ
Strictly speaking, it should not work like that, because:
xsl:for-each
is not a loop;xsl:for-each
is out-of-scope for the
instructions that are outside of it.For these reasons, attempting to do the same thing in XSLT 2.0 will fail - even though XSLT 2.0 allows you to reassign a variable - see the demo here: http://xsltransform.net/94hvTzQ/1
As you can see, each instance of the xsl:for-each
instruction increments the initial variable by 1, and does so independently of the other instances - proving that xsl:for-each
is not a loop. At the end, the original variable is returned unchanged, because of the 2nd reason above.
Upvotes: 4