XPath 1.0:查找指定节点之前的节点数 [英] XPath 1.0: Finding count of nodes before a specified node
问题描述
我熟悉在XSLT中使用前置轴来确定确定给定当前上下文的前面的元素.但是,对于给定存储在变量中的节点,我看不到有办法做到这一点.例如:
I am familiar with using preceding axes in XSLT for finding determining the number of preceding elements given the current context. However, I don't see a way to do the same given a node that I've stored in a variable. For example:
<xsl:variable name="matchedBook" select="book[text()='The Hobbit']"/>
<xsl:variable name="precedingBookCount" select="count(???)"/>
鉴于以下XML,precedingBookCount
应该等于3.
Given the following XML, precedingBookCount
should equal 3.
<available>
<book>Lord of the Rings</book>
<book>The Hunger Games</book>
</available>
<purchased>
<book>Ready Player One</book>
<book>The Hobbit</book>
<book>Lord of the Flies</book>
</purchased>
我在XPath 2.0中看到有一个 NodeComp运算符我可以使用的<<
,但是XPath 1.0中似乎没有.
I see in XPath 2.0 that there is a NodeComp operator <<
that I could use, but this does not appear to be present in XPath 1.0.
那我该如何在XPath 1.0中做到这一点?
How can I go about doing this in XPath 1.0 then?
推荐答案
<xsl:variable name="precedingBookCount" select="count($matchedBook/preceding-sibling::book | $matchedBook/preceding::book)"/>
应该可以.
实际上,做<xsl:variable name="precedingBookCount" select="count($matchedBook/preceding::book)"/>
就足够了.
这篇关于XPath 1.0:查找指定节点之前的节点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!