java - 如何从 xpath 表达式中获取真正的节点顺序 (java)

标签 java xslt xpath

如果我有一个使用 preceding-sibling:: 的 XPath 表达式,我得到的 NodeList 的顺序不正确。我怎样才能得到正确的订单?示例:

<library>
  <book name="book1">
    hello
  </book>
  <book name="book2">
    world
  </book>
  <book name="book3">
    !!!
  </book>
</library>

如果我尝试计算 XPath 表达式:/library/book[3]/preceding-sibling::book,我得到这个顺序:

  • 第一本书
  • 书2

但是如果我尝试计算:/library/book[3]/preceding-sibling::book[1],我得到节点:

  • 书2

那么,我怎样才能从这种表达式中得到真正的顺序:

/library/book[3]/preceding-sibling::book ?

最佳答案

来自 http://www.w3.org/TR/xpath/#predicates

An axis is either a forward axis or a reverse axis. An axis that only ever contains the context node or nodes that are after the context node in document order is a forward axis. An axis that only ever contains the context node or nodes that are before the context node in document order is a reverse axis. Thus, the ancestor, ancestor-or-self, preceding, and preceding-sibling axes are reverse axes; all other axes are forward axes. Since the self axis always contains at most one node, it makes no difference whether it is a forward or reverse axis. The proximity position of a member of a node-set with respect to an axis is defined to be the position of the node in the node-set ordered in document order if the axis is a forward axis and ordered in reverse document order if the axis is a reverse axis. The first position is 1.

关于使用特定 XPath 引擎评估某些 XPath 表达式后结果节点集的顺序,这完全取决于实现。在 XSLT 中,当指令处理按文档顺序排序的节点集时,它是明确定义的。在 XPath 中,节点集是这样定义的 http://www.w3.org/TR/xpath/#section-Introduction :

node-set (an unordered collection of nodes without duplicates)

这就是为什么在标准 DOM API 中有 settings获得有序和无序结果以及 XQuery

关于java - 如何从 xpath 表达式中获取真正的节点顺序 (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5497197/

相关文章:

xml - 如果使用 PowerShell 不存在,如何添加属性?

c# - 如何使用 selectsinglenode() 访问具有属性和命名空间的 xml 节点

java - SQL语法错误: Reading queries from text file: derby

java - boolean 递归中的 "unreachable statement"

Java:在嵌套列表中匹配列表

xml - 比较两个xml并获取公共(public)节点

xml - XML 属性的跟随兄弟

java - 我们可以不使用 final 关键字来避免继承吗?

xml - 从 XSL 中的 CDATA 标记中呈现 HTML 标记

javascript - 我试图在这个基本的 xhtml 页面上使用 JavaScript 和 XPath...但是我没有得到任何结果?