Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
Grouping keys are compared based on their type. For example, numbers are compared as numbers, and dates are compared as dates. Two grouping keys are considered equal based on the rules of the XPath
eq
operator, which is explained in Chapter 8 on page 582. Strings are compared using the collation specified in the
collation
attribute if present (for more details on collations, see under
eq
. If two values cannot be compared (because they are of noncomparable types; for example,
xs:date
and
xs:integer
), then they are considered not equal, which means the items end up in different groups.
If the
group-by
expression for any item in the population evaluates to an empty sequence, then the item will not be allocated to any groups, which means it will not be processed at all.
group-adjacent
When the
group-adjacent
attribute is used to define the grouping criteria, items are assigned to groups on the following basis:
The
group-adjacent
expression is evaluated once for each item in the population. During this evaluation, the context item is this item, the context position is the position of this item in population order, and the context size is the size of the population. The value that results from evaluating the
group-adjacent
expression is atomized (see page 81). Unlike the
group-by
attribute, the result of evaluating the
group-adjacent
expression, after atomization, must be a single value. A type error is reported if the value is an empty sequence, or if it is a sequence containing more than one atomic value.
Values of the grouping key are compared in the same way as for the
group-by
attribute. This means, for example, that strings are compared using the collation defined in the
collation
attribute if specified, and that NaN values compare equal.
There are two main reasons for using
group-adjacent
in preference to
group-by
:
group-starting-with
The
group-starting-with
attribute is an XSLT pattern (not an expression). Patterns are described in Chapter 12. Patterns apply only to nodes, so this attribute must be used only when the population consists entirely of nodes. The nodes in the population are assigned to groups on the following basis:
The result is that the initial node in each group (the one that comes first in population order) will always match the pattern, with the possible exception of the first group, which may contain no node that matches the pattern.
The
group-starting-with
attribute is useful where the population consists of a repeating group of nodes whose first member can be readily identified: for example, a
group-starting-with=“header”
.
group-ending-with
This attribute behaves in a very similar way to
group-starting-with
, except that the pattern identifies the last item in a group instead of the first. The nodes in the population are assigned to groups on the following basis:
The most common use case for this option is where input records contain a continuation marker of some kind. For example, the population might consist of elements that have the attribute
continued=“yes”
or
continued=“no”
. A group can then be defined using the criterion
group-ending-with=“*[@continued=“no“]”
.