Support #3763
closedStrange for-each-group select operation order
0%
Description
Calling a XPath function inside for-each-group's select results in strange results when used with group-starting-with. Tested with Saxon EE 9.7.0.21 and EE 9.8.0.8.
In my use case, I need to pre-sort input data before performing grouping by a for-each-group.
The documentation is a bit confusing regarding the group-starting-with usage, maybe I got the whole idea wrong...
Files
Updated by Michael Kay over 6 years ago
I think the problem is with the condition
group-starting-with="label[preceding::label[1] + 1 != .]
The preceding axis selects nodes based on their position in the tree that they belong to, not their position in the sequence that you are grouping. You have sorted the nodes to produce a sequence that is not in document order, but the nodes still have their original parents, siblings, and children.
I think you can probably achieve the effect with
group-adjacent="label - position()"
( a technique invented by David Carlisle)
If the sequence is
1,2,3,4,6,7,8,15,16
then the values of the grouping key will be
0,0,0,0,1,1,1,7,7
Updated by Andy Yar over 6 years ago
Thanks for your explanation Michael. That solution of yours works.
The W3C's XSLT 2.0 document (https://www.w3.org/TR/2009/PER-xslt20-20090421/#element-for-each-group) describes the population for sorting as a result of select XPath evaluation. The group-starting-with then operates on that population keeping its population order. I find that a bit confusing when the axis selectors don't operate on the population but on the original document.
Updated by Michael Kay over 6 years ago
Yes, it's a common problem that people don't appreciate that axes relate to the tree the node belongs to, rather than the sequence of nodes they are currently processing. One of those mistakes that everyone makes once.
Updated by Michael Kay over 6 years ago
- Tracker changed from Bug to Support
- Status changed from New to Resolved
- Assignee set to Michael Kay
Please register to edit this issue