[xsl] Multiple Grouping & Muenchian Method

Subject: [xsl] Multiple Grouping & Muenchian Method
From: "Steve Sze" <steveyksze@xxxxxxxxx>
Date: Sat, 22 Jul 2006 17:53:49 -0400
Hi Peter

Ahhh....  It looks great!!!  So, it is possible to have numerous
grouping combinations in version 1.0.

Thank you very much!!!
Steve

-----------------------------

Hi All

Is it possible to have a group within a group.  I did look at
http://www.biglist.com/lists/xsl-list/archives/200101/msg00070.html
but I either get a date with data from both dates or the same date for
each data set.

Thank You
Steve

Desire Sample Output
--------------------
07-13-2006
USA
Group     Name        id
AAA       Adel          12345
AAA       Barry         12346
AAA       Carl           12347
BBB       Dave          12345
BBB       Ethel          12346
BBB       Fred           12347

EUR
Group    Name         id
CCC      George       24567
CCC      Harold        23458
CCC      Jennifer      23459

07-14-2006
USA
Group     Name        id
BBB       Dave          12345
BBB       Ethel          12346
BBB       Fred           12347

EUR
Group    Name         id
CCC      George       24567
CCC      Harold        23458
CCC      Jennifer      23459

=====================================

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

<xsl:output method="html" indent="yes" />

<xsl:key name="by-date" match="entry" use="date" />
<xsl:key name="by-country" match="entry" use="country" />

<xsl:template match="/report">
<html>
<head>
  <title/>
</head>
<body>
  <table border="1">
    <xsl:variable name="break_by_date"
select="//entry[key('by-date', date)]"/>
    <xsl:variable name="break_by_country" select="key('by-country',
currency)"/>
    <xsl:for-each select="$break_by_date[generate-id() =
generate-id(key('by-country', country)[1])]">
      <tr>
        <td><xsl:value-of select="date"/></td>
      </tr>
      <tr>
        <td><xsl:value-of select="country" /></td>
      </tr>
      <tr>
        <td>Group</td>
        <td>Name</td>
        <td>id</td>
      </tr>
      <xsl:for-each select="key('by-country', country)">
        <tr>
          <td><xsl:value-of select="../@type" /></td>
          <td><xsl:value-of select="name" /></td>
          <td><xsl:value-of select="id" /></td>
        </tr>
      </xsl:for-each>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

Sample XML file
<report>
    <table>
            <date>07-13-2006
                    <group type="AAA">111111
                            <entry>
                                    <date>07-13-2006</date>
                                    <name>Adel</name>
                                    <country>USA</country>
                                    <id>12345</id>
                            </entry>
                            <entry>
                                    <date>07-13-2006</date>
                                    <name>Barry</name>
                                    <country>USA</country>
                                    <id>12346</id>
                            </entry>
                            <entry>
                                    <date>07-13-2006</date>
                                    <name>Carl</name>
                                    <country>USA</country>
                                    <id>12347</id>
                            </entry>
                    </group>
                    <group type="BBB">111111
                            <entry>
                                    <date>07-13-2006</date>
                                    <name>Dave</name>
                                    <country>USA</country>
                                    <id>12345</id>
                            </entry>
                            <entry>
                                    <date>07-13-2006</date>
                                    <name>Ethel</name>
                                    <country>USA</country>
                                    <id>12346</id>
                            </entry>
                            <entry>
                                    <date>07-13-2006</date>
                                    <name>Fred</name>
                                    <country>USA</country>
                                    <id>12347</id>
                            </entry>
                    </group>
                    <group type="CCC">111111
                            <entry>
                                    <date>07-13-2006</date>
                                    <name>George</name>
                                    <country>EUR</country>
                                    <id>24567</id>
                            </entry>
                            <entry>
                                    <date>07-13-2006</date>
                                    <name>Harold</name>
                                    <country>EUR</country>
                                    <id>23458</id>
                            </entry>
                            <entry>
                                    <date>07-13-2006</date>
                                    <name>Jennifer</name>
                                    <country>EUR</country>
                                    <id>23459</id>
                            </entry>
                    </group>
            </date>
            <date>07-14-2006
                    <group type="BBB">111111
                            <entry>
                                    <date>07-14-2006</date>
                                    <name>Dave</name>
                                    <country>USA</country>
                                    <id>12345</id>
                            </entry>
                            <entry>
                                    <date>07-14-2006</date>
                                    <name>Ethel</name>
                                    <country>USA</country>
                                    <id>12346</id>
                            </entry>
                            <entry>
                                    <date>07-14-2006</date>
                                    <name>Fred</name>
                                    <country>USA</country>
                                    <id>12347</id>
                            </entry>
                    </group>
                    <group type="CCC">111111
                            <entry>
                                    <date>07-14-2006</date>
                                    <name>George</name>
                                    <country>EUR</country>
                                    <id>24567</id>
                            </entry>
                            <entry>
                                    <date>07-14-2006</date>
                                    <name>Harold</name>
                                    <country>EUR</country>
                                    <id>23458</id>
                            </entry>
                            <entry>
                                    <date>07-14-2006</date>
                                    <name>Jennifer</name>
                                    <country>EUR</country>
                                    <id>23459</id>
                            </entry>
                    </group>
            </date>
    </table>
</report>

------------------------------

Date: Fri, 21 Jul 2006 15:17:17 -0400
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
From: "Jacoby, Peter R." <PJACOBY@xxxxxxxxxxxx>
Subject: RE: [xsl] Multiple Grouping & Muenchian Method
Message-ID: <3CFEFF6C55FB5C42A6E68E9521D5C436025FA18F@xxxxxxxxxxxxxxxxxxxx>

Is it possible to have a group within a group.=20

Hi, Steve,


You were really close in your code.  The concept that you were missing =
is to
define one xsl:key with two parameters using the concat function.

If you change your key declaration to be
<xsl:key name=3D"byDateAndCountry" match=3D"entry"
use=3D"concat(parent::group/parent::date/text(), country)" />
or more succinctly:
<xsl:key name=3D"byDateAndCountry" match=3D"entry" =
use=3D"concat(../../text(),
country)" />

You can then call one key indexed by an entry's date as well as country.

A full stylesheet is below.  Please let us know if you have any other =
questions.
This all becomes a lot easier in 2.0 with xsl:for-each-group.  Hopefully =
someone
will post the 2.0 solution so we can see how much simpler it is.

-Peter

<?xml version=3D"1.0" ?>
<xsl:stylesheet version=3D"1.0" =
xmlns:xsl=3D"http://www.w3.org/1999/XSL/Transform";>
<xsl:output method=3D"xml" omit-xml-declaration=3D"no" indent=3D"yes"
encoding=3D"UTF-8"/>

<xsl:key name=3D"byDateAndCountry" match=3D"entry" =
use=3D"concat(../../text(),
country)" />

<xsl:template match=3D"/">
      <table border=3D"1">
              <xsl:apply-templates select=3D"report/table/date"/>
      </table>
</xsl:template>

<xsl:template match=3D"date">
      <tr>
              <td>
                      <xsl:value-of select=3D"normalize-space(text())" />
              </td>
      </tr>
=09
      <xsl:for-each select=3D"group/entry[generate-id() =3D
generate-id(key('byDateAndCountry', concat(../../text(), =
country))[1])]">
              <tr>
                      <td>
                              <xsl:value-of select=3D"country" />
                      </td>
              </tr>
              <tr>
                      <td>Group</td>
                      <td>Name</td>
                      <td>ID</td>
              </tr>
              <xsl:apply-templates select=3D"key('byDateAndCountry',
concat(../../text(), country))" />
      </xsl:for-each>
</xsl:template>

<xsl:template match=3D"entry">
      <tr>
              <td>
                      <xsl:value-of select=3D"../@type" />
              </td>
              <td>
                      <xsl:value-of select=3D"name" />
              </td>
              <td>
                      <xsl:value-of select=3D"id" />
              </td>
      </tr>
</xsl:template>

</xsl:stylesheet>

Current Thread