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