Re: [xsl] Grouping elements using XSL

Subject: Re: [xsl] Grouping elements using XSL
From: Josh Canfield <joshcanfield@xxxxxxxxx>
Date: Wed, 21 Jul 2004 18:12:15 -0700
Here is a solution for your problem to mull over...

Enjoy,
Josh


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

  <!-- key to return the ROW nodes with matchine DAYSOPEN nodes-->
  <xsl:key name="days-open" match="ROW" use="DAYSOPEN"/>
  <!-- key to return the ROW nodes with matchine MONTH nodes-->
  <xsl:key name="month" match="ROW" use="MONTH"/>

  <xsl:template match="/REPORT">
    <table>
    <!-- Build Header Row -->
    <tr><th>Days Open</th>
    <xsl:for-each select="DATA/ROW[count(. | key('month',MONTH)[1])=1]">
      <xsl:sort select="MONTH" data-type="text"/>
      <th><xsl:value-of select="MONTH"/></th>
    </xsl:for-each>
    <th>Total</th>
    </tr>

    <!-- Build the content Rows -->
    <!-- Iterate over the set of ROW nodes containing the first unique
DAYSOPEN value -->
    <xsl:for-each select="DATA/ROW[count(. | key('days-open', DAYSOPEN)[1])=1]">
      <xsl:sort select="DAYSOPEN" data-type="number"/>
      <!-- Hold on to the current days-open -->
      <xsl:variable name="days-open" select="DAYSOPEN"/>
      <tr>
        <td><xsl:value-of select="$days-open"/></td>
        <!-- Iterate over a set of ROW nodes contianing the first
unique MONTH value -->
        <xsl:for-each select="../ROW[count(. | key('month',MONTH)[1])=1]">
          <xsl:sort select="MONTH" data-type="text"/>
          <!-- create a cell containing the sum COUNT nodes from the
ROW nodes with common DAYSOPEN and MONTH nodes-->
          <td><xsl:value-of select="sum(../ROW[(DAYSOPEN=$days-open)
and (MONTH=current()/MONTH)]/COUNT)"/></td>
        </xsl:for-each>
        <!-- create a cell containing the sum of the COUNT nodes of
ROWs with a common DAYSOPEN node-->
        <td><xsl:value-of select="sum(../ROW[DAYSOPEN=$days-open]/COUNT)"/></td>
      </tr>
    </xsl:for-each>
    </table>
  </xsl:template>

</xsl:stylesheet>

On Wed, 21 Jul 2004 19:21:33 -0400, michael.s.eberhart@xxxxxxxxxxx
<michael.s.eberhart@xxxxxxxxxxx> wrote:
> 
> 
> Hi,
> 
> I'm new to XSL and need help (that is an understatement) grouping elements
> for display.  I have been reading up on several web sites, but I am still
> extremely confused.
> 
> My data looks like this:
> 
> <REPORT>
>      <DATA>
>            <ROW>
>                  <DAYSOPEN>12</DAYSOPEN>
>                  <REGION>MA</REGION>
>                  <MONTH>2004-01</MONTH>
>                  <COUNT>14</COUNT>
>            </ROW>
>            <ROW>
>                  <DAYSOPEN>15</DAYSOPEN>
>                  <REGION>RI</REGION>
>                  <MONTH>2004-02</MONTH>
>                  <COUNT>14</COUNT>
>            </ROW>
>            <ROW>
>                  <DAYSOPEN>12</DAYSOPEN>
>                  <REGION>OH</REGION>
>                  <MONTH>2004-01</MONTH>
>                  <COUNT>10</COUNT>
>            </ROW>
>            <ROW>
>                  <DAYSOPEN>9</DAYSOPEN>
>                  <REGION>MS</REGION>
>                  <MONTH>2004-02</MONTH>
>                  <COUNT>11</COUNT>
>            </ROW>
>            <ROW>
>                  <DAYSOPEN>12</DAYSOPEN>
>                  <REGION>PA</REGION>
>                  <MONTH>2004-03</MONTH>
>                  <COUNT>3</COUNT>
>            </ROW>
>      </DATA>
> </REPORT>
> 
> I need the display on the WEB page to be :
> 
> Days Open         2004-01           2004-02           2004-03
> Total
> 9                 0           11          0                 11
> 12                24          0           3                 27
> 15                0           14          0                 14
> 
> Region is ignored on this particular report but the data set will be used
> for multiple reports. All data including months can change each time app is
> executed.
> 
> Any help you can give me would be most greatly appreciated.
> 
> Thanks
> 
> Mike

Current Thread