Re: [xsl] [XSLT2] Some common, generic grouping problems

Subject: Re: [xsl] [XSLT2] Some common, generic grouping problems
From: "Christian Roth" <roth@xxxxxxxxxxxxxx>
Date: Wed, 26 Jul 2006 13:20:32 +0200
(Very late reply, I know, so it's merely for the archives.)


This is for the following grouping task of my original post:

__Example XML #2__

<root>
    <arbitrary />
    <elem      color="dark-red" />
    <elem      color="red" />
    <arbitrary />
    <elem      color="red" />
    <arbitrary />
    <elem      color="dark-red" />
    <elem      color="red" />
    <arbitrary />
    <elem      color="red" />
    <arbitrary />
</root>

The task is to group all elements starting at 'dark-red' up to the last
red element before the subsequent 'dark-red' element, i.e.:

<root>
    <arbitrary />
    <red>
        <elem      color="dark-red" />
        <elem      color="red" />
        <arbitrary />
        <elem      color="red" />
    </red>
    <arbitrary />
    <red>
        <elem      color="dark-red" />
        <elem      color="red" />
        <arbitrary />
        <elem      color="red" />
    </red>
    <arbitrary />
</root>


David Carlisle kindly provided the following solution:


<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output indent="yes"/>

<xsl:template match="root">
  <root>
  <xsl:for-each-group select="*"
  group-starting-with="*[@color=('dark-red')]">
    <xsl:choose>
      <xsl:when test="@color=('dark-red')">
      <xsl:variable name="l"
  select="current-group()[@color='red'][last()]"/>
  <red>
    <xsl:copy-of select="current-group()[. &lt;&lt; $l],$l"/>
    </red>
    <xsl:copy-of select="current-group()[. >> $l]"/>
      </xsl:when>
      <xsl:otherwise>
      <xsl:copy-of select="current-group()"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each-group>
  </root>
</xsl:template>
</xsl:stylesheet>


I think the given code has a small mistake; the code

<xsl:variable name="l" 
     select="current-group()[@color='red'][last()]"/>

must read

<xsl:variable name="l" 
     select="current-group()[@color='red' or @color='dark-red'][last()]"/>

to correctly handle the case where the input document looks like

 ...
    <elem      color="dark-red" />
    <elem      color="dark-red" />
    <elem      color="red" />
    <arbitrary />
    <elem      color="dark-red" />
 ...

where the expected output should be

 ...
    <red>
        <elem      color="dark-red" />
    </red>
    <red>
        <elem      color="dark-red" />
        <elem      color="red" />
    </red>
    <arbitrary />
    <red>
        <elem      color="dark-red" />
 ...

i.e. two adjacent "dark-red" p elements must be placed into their own group.

I hope I get it right when summarizing the idea behind this code as follows:


__ SOLUTION IDEA __

* First, we partition the elements into groups, each starting with "dark-
red", our start criterion. 
* Then, within each group (i.e., list of elements), we look for the last
red element and remember it (variable 'l').
* Finally, we create the output in two stages: First, surrounded by the
grouping element, we copy all elements of the grouped elements that are
positioned earlier than or equal to the last red node (we calculated and
remembered earlier) in document order, and then we output the remainder
of the elements in that group, i.e. all elements later than the
remembered red element.


The solution can rather easily be extended to the general (=recursive)
case where the grouping should be performed on any nesting level by
replacing the

      <xsl:copy-of select="current-group()..."/>

statements with

      <xsl:for-each select="current-group()...">
        <xsl:copy>
          <xsl:copy-of select="@*" />
          <xsl:apply-templates select="." />
        </xsl:copy>
      </xsl:for-each>


Regards, Christian.

Current Thread