Project

Profile

Help

Support #5200

closed

How can I check if the nodes in any XPath result are in document order? Or put them in document order?

Added by Gerben Abbink over 2 years ago. Updated over 2 years ago.

Status:
Closed
Priority:
Low
Assignee:
-
Category:
-
Sprint/Milestone:
-
Start date:
2022-01-11
Due date:
% Done:

0%

Estimated time:
Legacy ID:
Applies to branch:
Fix Committed on Branch:
Fixed in Maintenance Release:
Platforms:

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.

Actions #1

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.

Actions #2

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.

Actions #3

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

Actions #4

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.

Actions #5

Updated by Gerben Abbink over 2 years ago

All the solutions work. Thank you for the (very) quick response.

Actions #6

Updated by Michael Kay over 2 years ago

  • Status changed from New to Closed

Please register to edit this issue

Also available in: Atom PDF