[2.0] New Grouping Syntax in XSLT 2.0
In 2001, the XSL Working Group released a document entitled “XSLT Requirements Version 2.0.” More than half of the document came under the heading “Must Simplify Grouping.” (I can’t imagine how the group would have met a requirement named “Must Make Grouping More Complicated and Confusing.”) We’ll take a look at those changes in this section.
XSLT 2.0’s grouping functions are built around the <xsl:for-each-group> element. Within this element, we’ll use the new XSLT functions
current-group() and current-grouping-key() to work with the data we’re grouping. There are four
mutually exclusive attributes for the <xsl:for-each-group> element, each of
which performs a different style of grouping:
group-byThis is the most common type of grouping. We use an XPath expression to define what identifies a group (all of the
<address>elements that have the same<zip>code, for example), so we can then iterate through each group.group-adjacentThis approach is useful when you want to build a group containing all the adjacent nodes that match an XPath expression. As an example, we’ll take all the adjacent
<p>elements in a document, convert each one to an<li>element, and put<ul>and</ul>tags around the entire group.group-starting-withThe
group-starting-withattribute defines an XPath expression that identifies the start of a group. Once a group starts, every element is added to the group until the start of another group is found.group-starting-withand
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access