XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round two / Grouping with xsl:for-each-group

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 2.)

Grouping in XSLT 1.0 was difficult and cumbersome. Steve Muench of Oracle developed a complex approach based on creating key values (keys), known in XSLT history as the Muenchian method in his honor.

XSLT 2.0 no longer requires this method, though it still works. A dedicated grouping instruction is introduced: xsl:for-each-group.

Like xsl:for-each, the instruction forms a loop, but over groups rather than individual items. Its input sequence is constructed using attribute select.

A grouping criterion is supplied through attribute group-by or similar attributes. Items are grouped accordingly, and the loop processes the groups. Each distinct criterion value starts a group; empty groups cannot exist by definition:

<xsl:for-each-group select="sequenz" group-by="kriterium">
  <!-- Ausgabe -->
</xsl:for-each-group>

During an iteration, function current-group() accesses the current group’s item sequence, processed like any other sequence. The current criterion value is obtained with current-grouping-key().

The book catalog grouped by publisher

Grouping the books by publisher is now easy. Output the current publisher using XSLT function current-grouping-key(). Output only each book’s title by applying a path expression to the sequence from current-group(). The group sequence is output together using xsl:value-of:

<xsl:for-each-group select="buchhandel/buch" group-by="buchverlag">
  <p><b><xsl:value-of select="current-grouping-key()"/></b><br/><xsl:value-of select="current-group()/buchtitel" separator=", "/></p>
</xsl:for-each-group>

Grouping works readily. The titles’ presentation is uninspiring, but we will fix it shortly:

Touchstone Where Wizards Stay Up Late

Spacetrotter Publishing Yet Another Hitchhiker's Guide to the Galaxy, Hitchhiker's Guide to the Galaxy, Another Hitchhiker's Guide to the Galaxy

etc.

Publisher groups appear in order of first occurrence (order of first appearence). Like xsl:for-each, xsl:for-each-group also supports sorting with xsl:sort:

<xsl:for-each-group select="buchhandel/buch" group-by="buchverlag">
  <xsl:sort select="buchverlag"/>
  <p><b><xsl:value-of select="current-grouping-key()"/></b><br/><xsl:value-of select="current-group()/buchtitel" separator=", "/></p>
</xsl:for-each-group>

xsl:for-each inside xsl:for-each-group

After ordering the groups by publisher, tidy the titles too. Since current-group() returns a sequence, it can be processed using xsl:for-each instead of being output directly using xsl:value-of:

<xsl:for-each-group select="buchhandel/buch" group-by="buchverlag">
  <xsl:sort select="buchverlag"/>
  <p>
    <b><xsl:value-of select="current-grouping-key()"/></b><br/>
    <ul>
      <xsl:for-each select="current-group()/buchtitel">
        <li><xsl:value-of select="."/></li>
      </xsl:for-each>
    </ul>
  </p>
</xsl:for-each-group>

xsl:for-each-group inside xsl:for-each-group

Groups can themselves be grouped. Here, authors are grouped inside the outer instruction xsl-for-each-group by nesting two such instructions:

...
<xsl:for-each-group select="buchhandel/buch" group-by="buchverlag">
  <xsl:sort select="buchverlag"/>
  <p><b><xsl:value-of select="current-grouping-key()"/></b></p>
  <xsl:for-each-group select="current-group()" group-by="buchautor">
    <xsl:sort select="buchautor[1]"/>
    <p><i><xsl:value-of select="current-grouping-key()"/></i></p>
    <ul>
      <xsl:for-each select="current-group()">
        <xsl:sort select="buchtitel"/>
        <li><xsl:value-of select="buchtitel"/></li>
      </xsl:for-each>
    </ul>
  </xsl:for-each-group>
</xsl:for-each-group>
...

Each grouping loop sorts using xsl:sort. The inner instruction xsl:for-each-group takes the outer current group and groups it by book author elements. Inside, the current group and grouping key change. The inner group becomes input to an instruction xsl:for-each outputting book titles after another sort.

This groups first by publisher, then by author within each publisher, with book titles alphabetically ordered.

   

back to top