xslt - XPath 1.0 : Finding count of nodes before a specified node

标签 xslt xpath xslt-1.0 xpath-1.0

我熟悉使用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/

相关文章:

java - 变压器配置异常 :Could not compile sytlesheet

通过涉及特殊字符的 XSL 转换输出 HTML

php - 使用 PHP、simplexml_load_file 和 Xpath 从 XML 文件返回唯一值

python - 如何使用 Python 在 Selenium 中为给定的 HTML 构建 xpath

xml - 如何检查 XSLT 中是否存在值

xml - XPath:如何选择具有一个或另一个属性的所有元素

java - 如何使用多个输入的任何循环指定输入目录?

html - XPath - 除标题中的元素外的所有元素

xml - 强制清空 CDATA 元素

java - 如何使用 Saxon 调试 XSLT 中的扩展函数?