XSL:数字计数未按预期工作 -- 我的 XPath 有问题吗?
这是 XML
<row>
<cell>blah blah</cell>
<check>
<option/>
<option/>
<option/>
<option/>
<option/>
<option/>
</check>
</row>
这是 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>
我得到的只是“0”。 http://www.w3schools.com/XPath/xpath_syntax.asp 说“。”表示当前节点。它不应该返回“6”吗?
Edit1:我想寻找任何级别的选项标签,而不仅仅是检查。应该已经解释过,但选项标签可以存在于下面的任何级别
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想计算后代
选项
,则不应使用xsl:number
,但:If you want to count descendant
option
s you shouldn't usexsl:number
but:我认为问题在于 xpath 表达式
option
不会与row
元素中的任何内容匹配 - 请尝试以下操作:查找
option
元素在任何级别使用以下语法:我不认为
from
属性是必需的,并且level
属性可能没有按照您的想法执行(我'我也不确定它是做什么的......)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...)来自 XSLT 1.0 W3C 规范 :
从本文中可以清楚地看出,仅计算祖先节点或当前节点之前的节点。
在此问题中,当前节点是顶部元素节点
行
并且它有 0 个祖先和 0 个前导元素节点。因此,返回的结果是正确的!
解决方案:
使用:
计算该表达式的结果是所有
option
元素的计数。在文档中,它们是当前节点的后代(row
元素)。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).