Project

Profile

Help

Support #3763

closed

Strange for-each-group select operation order

Added by Andy Yar almost 6 years ago. Updated almost 6 years ago.

Status:
Resolved
Priority:
Low
Assignee:
Category:
-
Sprint/Milestone:
-
Start date:
2018-04-27
Due date:
% Done:

0%

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

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

sortForEachGroup.xsl (1.49 KB) sortForEachGroup.xsl Test stylesheet demonstrating the grouping behaviour Andy Yar, 2018-04-27 09:30
sortForEachGroup.xml (250 Bytes) sortForEachGroup.xml Test input mimicking a real data structure Andy Yar, 2018-04-27 09:30
Actions #1

Updated by Michael Kay almost 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

Actions #2

Updated by Andy Yar almost 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.

Actions #3

Updated by Michael Kay almost 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.

Actions #4

Updated by Michael Kay almost 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

Also available in: Atom PDF