[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Grouping problem

Subject: Re: Grouping problem
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Tue, 4 Dec 2007 15:08:17 +0000
Re:  Grouping problem
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.