Here's the XML
<row>
<cell>blah blah</cell>
<check>
<option/>
<option/>
<option/>
<option/>
<option/>
<option/>
</check>
</row>
Here is the XSL
<xsl:template match="row">
<xsl:variable name="inputLevel">
<xsl:number count="option" level="any" from="."/>
</xsl:variable>
<xsl:value-of select="$inputLevel"/>
</xsl:template>
All I get is "0". http://www.w3schools.com/XPath/xpath_syntax.asp says "." means the current node. Shouldn't it be returning "6"?
Edit1: I wanted to look for option tags at ANY level, not just check. Should have explained but the option tags could exist at any level below
From the XSLT 1.0 W3C specification:
From this text it is clear that only nodes that are ancestors or are preceding the current node are counted.
In this question, the current node is the top element node
row
and it has 0 ancestor and 0 preceding element nodes.Therefore, the returned result is correct!
Solution:
Use:
The result of evaluating this expression is the count of all
option
elements in the document, that are descendents of the current node (therow
element).I think the problem is that the xpath expression
option
won't match anything at therow
element - try this instead:To look for
option
elements at any level use the following syntax:I don't think the
from
attribute is reqired, and thelevel
attribute probably isn't doing what you think it is (I'm also not sure what it does...)If you want to count descendant
option
s you shouldn't usexsl:number
but: