gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 01:29:38 24 4
gpt4 key购买 nike

如果我有一个使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com