What Are current-group and current-grouping-key in XSLT?

In XSLT 2.0 and later versions, current-group() and current-grouping-key() are built-in functions designed specifically for working within the <xsl:for-each-group> instruction. They provide direct access to the items being processed inside a particular group and the evaluation key that formed that group. Together, they eliminate the complex, inefficient Muenchian grouping techniques required in XSLT 1.0, enabling clear, declarative transformations for structured and flat XML datasets.

The Role of <xsl:for-each-group>

Before examining the functions, it is essential to understand the parent element: <xsl:for-each-group>. This instruction iterates over a sequence of items and partitions them into distinct subsets based on an algorithm defined by one of several attributes:

During each iteration of <xsl:for-each-group>, XSLT makes the current group's data accessible via current-group() and current-grouping-key().

Understanding current-group()

The current-group() function returns a sequence containing all the items (nodes or atomic values) that belong to the group currently being processed in the loop iteration.

Key Characteristics

Example

Given a flat list of employee records:

<employees>
  <emp department="IT" name="Alice"/>
  <emp department="HR" name="Bob"/>
  <emp department="IT" name="Charlie"/>
</employees>

You can group by department and process each member:

<xsl:for-each-group select="employees/emp" group-by="@department">
  <department name="{@department}">
    <xsl:for-each select="current-group()">
      <member><xsl:value-of select="@name"/></member>
    </xsl:for-each>
  </department>
</xsl:for-each-group>

Understanding current-grouping-key()

The current-grouping-key() function returns the atomic value that represents the key shared by all members of the current group.

Key Characteristics

Example

Using current-grouping-key() to dynamically construct wrapper elements:

<xsl:for-each-group select="employees/emp" group-by="@department">
  <department name="{current-grouping-key()}" total-staff="{count(current-group())}">
    <xsl:apply-templates select="current-group()"/>
  </department>
</xsl:for-each-group>

Summary of Functional Differences

By utilizing these two functions inside <xsl:for-each-group>, stylesheets remain concise, maintainable, and significantly more performant than legacy XSLT 1.0 key-based workarounds.