我熟悉使用preceding axes在 XSLT 中用于查找确定给定当前上下文的前面元素的数量。但是,对于我存储在变量中的节点,我看不到一种方法可以执行相同的操作。例如:
<xsl:variable name="matchedBook" select="book[text()='The Hobbit']"/>
<xsl:variable name="precedingBookCount" select="count(???)"/>
给定以下 XML,precedingBookCount
应等于 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 operator <<
我可以使用它,但这似乎不存在于 XPath 1.0 中。
那么我怎样才能在 XPath 1.0 中做到这一点呢?
最佳答案
<xsl:variable name="precedingBookCount" select="count($matchedBook/preceding-sibling::book | $matchedBook/preceding::book)"/>
应该做。
其实做<xsl:variable name="precedingBookCount" select="count($matchedBook/preceding::book)"/>
就够了.
关于xslt - XPath 1.0 : Finding count of nodes before a specified node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11037979/