返回XPath正确的顺序(文档顺序的insted) [英] Returning XPath correct order ( insted of document order)
问题描述
所以我已经在我的大学里获得了一个修复XPath的任务,并让它以正确的顺序返回节点。
我一直试图找到一个绕着编写一个很长的代码并将每个表达式分解为很少的和平并将它重新组合在一起的方法。
我想过一些想法,但我需要帮助才能让它们发生:)
-
在XPath jar中找到对节点进行排序并删除它的方法 - 问题是我不知道是否有可能看到XPath jar代码,并且没有在网上找到它.-如果你能指出我的话,这将是伟大的!
-
我发现这个选项看起来应该可以解决这个问题
http://cafeconleche.org/books/xmljava/chapters/ch16s06.html
但它要求dom执行XPath 3.0
我无法在任何地方找到dom - 任何想法是否有这样的事情? -
如果你有任何其他想法我会喜欢听!
例如:对于这个xml
< library>
< book name =book1>
你好
< / book>
< book name =book2>
world
< / book>
< book name =book3>
!!!
< / book>
< / library>
此表达式:/ library / book [3] / preceding-sibling :: book
我得到
book1
book2
insted:
book2
book1
我可以使用任何东西,只要我在java程序中这样做
感谢您的帮助:)
在XPath 1.0中,没有节点序列,只有节点集。 XPath 1.0规范未定义节点集中节点的顺序。许多API也没有定义它,但总是以文档顺序返回节点集中的节点 - 不是因为XPath需要它,而是因为XSLT需要它,而且XSLT已经设定了XPath引擎应该如何表现的期望。 / p>
如果您希望节点序列能够控制排序,则必须转到XPath 2.0(或XQuery 1.0,它是XPath 2.0的超集)。在XPath 2.0中,以下表达式返回您想要的内容:
reverse(/ library / book [3] / preceding-sibling: :book)
So ive been given an assignment in my university of "fixing" XPath and get it to return the nodes in the correct order.
Ive been trying to find a way around writing a very long code and breaking each expression into little peaces and putting it back together again.
Ive thought of a few ideas but i need help making them happen :)
Find the method in the XPath jar that sorts the nodes and delete it - problem is i dont know if its possible to see the XPath jar code , and didnt find it online.- if you can point me to it , that would be great !
Ive found this option which looks like it should do the trick http://cafeconleche.org/books/xmljava/chapters/ch16s06.html
but it demands dom implementation of XPath 3.0 and i cant find that dom anywhere - any idea if there is such thing ?If you have any other idea i would love to hear !
For example : for this xml
<library>
<book name="book1">
hello
</book>
<book name="book2">
world
</book>
<book name="book3">
!!!
</book>
</library>
and this expression : /library/book[3]/preceding-sibling::book Im getting
book1 book2
insted of :
book2 book1
I can use anything as long as i do it in a java program
Thank you for all your help :)
In XPath 1.0, there are no node sequences, only node sets. The XPath 1.0 specification doesn't define the order of nodes in a node set. Many APIs don't define it either, but invariably they return the nodes in the node-set in document order - not because XPath requires it, but because XSLT requires it, and XSLT has set the expectations for how XPath engines should behave.
If you want node sequences with control over the ordering, you will have to move to XPath 2.0 (or XQuery 1.0, which is a superset of XPath 2.0). In XPath 2.0, the following expression returns what you want:
reverse(/library/book[3]/preceding-sibling::book)
这篇关于返回XPath正确的顺序(文档顺序的insted)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!