Support #5200
closedHow can I check if the nodes in any XPath result are in document order? Or put them in document order?
0%
Description
Some XPath expressions, such as
//A, //B
do not return the nodes in document order.
Is there a function I can call to check if the nodes are in document order? Or a function that puts them in document order?
I use XPathSelector.evalute() to evalute the XPath expression.
Updated by Martin Honnen over 2 years ago
You can create a function, even inline with e.g. function($seq) { $seq/. }
, that will return the nodes passed in in document order. I don't know whether there is an extension function in Saxon to check whether a sequence of nodes is already in document order.
Updated by Martin Honnen over 2 years ago
You could implement one with e.g. function($seq) { deep-equal($seq, $seq/.) }
.
Comment by Michael Kay: this could give a spurious true
result, since two different nodes can be deep-equal to each other.
Updated by John Lumley over 2 years ago
If I read the spec correctly
(//A, //B) | ()
should return them in document order, unless some (incorrect) optimisation has elminated the empty sequence. See https://www.w3.org/TR/xpath-31/#combining_seq
Updated by Michael Kay over 2 years ago
You can sort any node sequence into document order (removing duplicates at the same time) using the expression
$input|()
or
$input/.
or
./$input
To check that a sequence is already in document order, with no duplicates, you could do
for-each-pair($s, tail($s), function($x, $y){ $x << $y })
and test that the result is all true, something you can do using the proposed 4.0 function fn:all()
, or using every $x in ... satisfies $x
.
Updated by Gerben Abbink over 2 years ago
All the solutions work. Thank you for the (very) quick response.
Please register to edit this issue