Re: [xsl] Grouping problem

Subject: Re: [xsl] Grouping problem
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Tue, 4 Dec 2007 15:08:17 +0000
On 04/12/2007, Terry Ofner <tofner@xxxxxxxxxxx> wrote:
> Dear xsl-list,
>
> I am attempting to group the following using XSLT 2.0. Here is a
> snippet of the XML (edited for readability):
>
> <?xml version="1.0"?>
> <Root>
> <State>
>         <!--first standard; first lesson-->
>         <Standard>
>                 <SS id="1.4.1">1.4.1  standard text </SS>
>                 <Lesson>Prefixes and Suffixes</Lesson>
>         </Standard>
>
>         <!--same standard as first; new lesson-->
>         <Standard>
>                 <SS id="1.4.1">1.4.1  standard text</SS>
>                 <Lesson>Root Words</Lesson>
>         </Standard>
>
>         <!--new standard; new lesson-->
>         <Standard>
>                 <SS id="1.4.2">1.4.2  standard text</SS>
>                 <Lesson>Context Clues</Lesson>
>         </Standard>
>
>         <!--two standards grouped with one lesson-->
>         <Standard>
>                 <SS id="1.4.5">1.4.5  standard text/SS>
>                 <SS id="1.4.6">1.4.6  standard text</SS>
>                 <Lesson>Synonyms</Lesson>
>         </Standard>
>         . . .
> </State>
> </Root>
>
> Here is what I am seeking:
>
>         <!--Lessons grouped after common standard-->
>
>         <Standard>
>                 <SS id="1.4.1">1.4.1  standard text </SS>
>                 <Lesson>Prefixes and Suffixes</Lesson>
>                 <Lesson>Root Words</Lesson>
>         </Standard>
>
>         . . .
>
>         <!--grouped standards separated-->
>
>         <Standard>
>                 <SS id="1.4.5">1.4.5  standard text </SS>
>                 <Lesson>Synonyms</Lesson>
>         </Standard>
>         <Standard>
>                 <SS id="1.4.6">1.4.6  new standard grouped with preceeding
> sibling</SS>
>                 <Lesson>Synonyms</Lesson>
>         </Standard>
>
> So far I have been able to group the <SS> elements using for-each-group:
>
> <xsl:stylesheet version="2.0"
>       xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> <xsl:output method="xml" indent="no"/>
>
> <xsl:template match="State">
>    <xsl:for-each-group select="Standard/SS" group-by="@id">
>        <xsl:for-each select="current-group()"/>
>        <xsl:copy-of select="."/><xsl:text>&#10;</xsl:text>
>        </xsl:for-each-group>
>        </xsl:template>
> </xsl:stylesheet>
>
>
> When I try to work in the <Lesson> elements, I get the entire
> standards listing.
> I have also wondered if I need to first separate the grouped standards.
>
> Any help would be appreciated.

Your input isn't well-formed and in your stylesheet you've got:

 <xsl:for-each select="current-group()"/>

...which won't do anything (I'm surprised it doesn't produce a warning).

But you were very close, you just need:

    <xsl:template match="State">
        <xsl:for-each-group select="Standard/SS" group-by="@id">
            <Standard>
                <xsl:copy-of select="."/>
                <xsl:copy-of
select="current-group()/following-sibling::Lesson"/>
            </Standard>
        </xsl:for-each-group>
    </xsl:template>

which outputs:

   <Standard>
      <SS id="1.4.1">1.4.1  standard text </SS>
      <Lesson>Prefixes and Suffixes</Lesson>
      <Lesson>Root Words</Lesson>
   </Standard>
   <Standard>
      <SS id="1.4.2">1.4.2  standard text</SS>
      <Lesson>Context Clues</Lesson>
   </Standard>
   <Standard>
      <SS id="1.4.5">1.4.5  standard text</SS>
      <Lesson>Synonyms</Lesson>
   </Standard>
   <Standard>
      <SS id="1.4.6">1.4.6  standard text</SS>
      <Lesson>Synonyms</Lesson>
   </Standard>

cheers
-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

Current Thread