[xsl] Re: Multi-level grouping using variables

Subject: [xsl] Re: Multi-level grouping using variables
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sat, 10 Jan 2004 21:23:21 -0500
At 2004-01-09 18:17 -0500, kakridge@xxxxxxxxxxxxx wrote:
I have been having a problem with grouping.  If I have:
...
How can I group each grade, and within each grade, group all tutors with
their students?

By using variables and multi-level grouping. This allows you to easily detect groups in a subset scope of the document ... the Muenchian method works with document-wide scope. Note how the principles are similar, but I came up with the variable-based approach for a customer who needed three levels of grouping and the Muenchian method for that deep a level was getting very complex to maintain and understand.


An example is below.

I hope this helps.

...................... Ken

p.s. in our hands-on training we teach all of axis-, variable- and key-based grouping methods for XSLT 1.0 (the Muenchian method is the key-based method) ... we've just rescheduled our Washington course to March 15-19 ... announcements will be made soon, details on our home page ... other cities are also being planned so check for a location near you.


t:\ftemp>type kakridge.xml <School> <Grade> <Students> <Student> <Name>Bob</Name> <Tutor>Mary Smith</Tutor> </Student> <Student> <Name>Joe</Name> <Tutor>Mike Smith</Tutor> </Student> <Student> <Name>Ken</Name> <Tutor>Mary Smith</Tutor> </Student> </Students> <Name>Kindergarten</Name> </Grade> <Grade> <Students> <Student> <Name>Ted</Name> <Tutor>Mary Smith</Tutor> </Student> <Student> <Name>Sammy</Name> <Tutor>Mike Smith</Tutor> </Student> </Students> <Name>First</Name> </Grade> </School>

t:\ftemp>type kakridge.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:for-each select="/School/Grade">
    <xsl:variable name="students" select="Students/Student"/>
    <xsl:for-each select="$students">
      <xsl:if test="generate-id(.)=
                    generate-id($students[Tutor=current()/Tutor])">
Grade: <xsl:value-of select="../../Name"/>
Tutor: <xsl:value-of select="Tutor"/>
Students: <xsl:for-each select="$students[Tutor=current()/Tutor]">
            <xsl:if test="position()>1">,</xsl:if>
            <xsl:value-of select="Name"/>
          </xsl:for-each>
          <xsl:text>
</xsl:text>
      </xsl:if>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

t:\ftemp>saxon kakridge.xml kakridge.xsl

Grade: Kindergarten
Tutor: Mary Smith
Students: Bob,Ken

Grade: Kindergarten
Tutor: Mike Smith
Students: Joe

Grade: First
Tutor: Mary Smith
Students: Ted

Grade: First
Tutor: Mike Smith
Students: Sammy

t:\ftemp>rem Done


-- North America (Washington, DC): 3-day XSLT/2-day XSL-FO 2004-03-15 Instructor-led on-site corporate, government & user group training for XSLT and XSL-FO world-wide: please contact us for the details

G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
ISBN 0-13-065196-6                       Definitive XSLT and XPath
ISBN 0-13-140374-5                               Definitive XSL-FO
ISBN 1-894049-08-X   Practical Transformation Using XSLT and XPath
ISBN 1-894049-11-X               Practical Formatting Using XSL-FO
Member of the XML Guild of Practitioners:     http://XMLGuild.info
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/s/bc


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread