xsl:for-each-group

Selects a sequence of nodes and/or atomic values and organizes them into subsets called groups.

Category: instruction
Content: ( xsl:sort* , sequence-constructor )
Permitted parent elements: any XSLT element whose content model is sequence constructor; any literal result element

Attributes

select

expression

Expression to select nodes/values.

group-by?

expression

Groups together all items having the same value for a grouping key. The grouping key may have multiple values (a sequence of values) in which case the item is added to more than one group.

group-adjacent?

expression

Groups together all items having the same value for a grouping key, provided that they are also adjacent in the input sequence. This is useful when you need to wrap a new element around a sequence of related elements in the source documents, for example a consecutive sequence of <bullet> elements. In this case the grouping key must be single-valued.

group-starting-with?

pattern

Processes the items in the supplied sequence in turn, starting a new group whenever one of the items matches a specified pattern. This is useful, for example, when matching an <h2> element and its following <p> elements.

group-ending-with?

pattern

Processes the items in the supplied sequence in turn, closing the current group whenever one of the items matches a specified pattern. This is useful when matching a sequence of items in which the last item in the group carries some distinguishing attribute such as continued="no".

composite?

boolean

If set to yes, then the group-by and group-adjacent expressions may evaluate to a sequence, and grouping is done by comparing the entire sequence. If set to no, then when group-by evaluates to a sequence, the relevant item has multiple grouping keys and goes in multiple groups; with group-adjacent, a sequence-valued grouping key is then an error.

collation?

{ uri }

Notes on the Saxon implementation

Earlier drafts of XSLT 3.0 introduced new attributes bind-group and bind-grouping-key; these are no longer supported in Saxon 9.6.

The composite attribute is newly implemented in Saxon 9.6.

Saxon 9.6 supports streamed grouping when the group-adjacent, group-starting-with, or group-ending-with attributes are used.

Details

There are four possible ways of defining the grouping using different attributes: group-by, group-adjacent, group-starting-with, and group-ending-with.

In XSLT 3.0, the capabilities of the xsl:for-each-group instruction are extended by virtue of the fact that the pattern used in group-starting-with or group-ending-with can now match atomic values as well as nodes.

XSLT 3.0 introduces the ability to bind variables to the current group and the current grouping key as an alternative to using the functions current-group() and current-grouping-key(). The advantage of using variables is that they have static scope, so the optimizer has more chance to see what the stylesheet is doing with each group. Streamed processing is only possible if the new variables are used, as this enables the processor to determine whether or not the use of the current group is streamable.

Examples

For examples of using the instruction, see the XSLT 2.0 specification.

Links to W3C specifications

XSLT 2.0 Specification

XSLT 3.0 Specification

See also

xsl:sort

current-group()

current-grouping-key()