RE: RE: [xsl] XSLT 2.0 multi-level grouping challenge/problem

Subject: RE: RE: [xsl] XSLT 2.0 multi-level grouping challenge/problem
From: cknell@xxxxxxxxxx
Date: Thu, 24 May 2007 12:24:41 -0400
Thanks. Sorry about the tags. 

I was inventing a similar structure to my actual one, but with completely unrelated content when I got cut-and-paste happy. I thought the document I presented would be easier for a general audience to grasp rather than the stuff I'm really working with.

Andrew's model showed me the error of my ways. Yours appears to be the same in the critical area.

Thanks again for taking the time.
-- 
Charles Knell
cknell@xxxxxxxxxx - email



-----Original Message-----
From:     Bjorndahl, Brad <brad.bjorndahl@xxxxxxxxxxxxxxxx>
Sent:     Thu, 24 May 2007 12:12:51 -0400
To:       <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject:  RE: [xsl] XSLT 2.0 multi-level grouping challenge/problem

I did something like this recently. I tested this with your xml (which
needed a little fixing).


<xsl:transform version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
  xmlns:xs="http://www.w3.org/2001/XMLSchema";>
  
  <xsl:output method="xml" indent="yes"/>
  
  <xsl:template match="/donors">
    <xsl:variable name="DONORS" >
      <DONORS>
        <xsl:for-each-group select="donor" group-by="state" >
          <state name="{current-grouping-key()}" >
          
            <xsl:for-each-group select="current-group()" group-by="city"
>
              <city  name="{current-grouping-key()}" >
              
                <xsl:for-each-group select="current-group()"
group-by="organ">
                  <organ name="{current-grouping-key()}"><xsl:value-of
select="count(current-group())" /></organ>
                  
                </xsl:for-each-group>
              </city>
            </xsl:for-each-group>
          </state>
        </xsl:for-each-group>
      </DONORS>
    </xsl:variable>
    <xsl:apply-templates select="$DONORS" />
  </xsl:template>

  <xsl:template match="DONORS" >
    <table border="1">
      <tr>
        <th>State</th>
        <th>City</th>
        <th>Organ</th>
        <th>count</th>
      </tr>
      <xsl:apply-templates />
    </table>
  </xsl:template>
  
  <xsl:template match="state" >
    <tr>
      <td rowspan="{count(city/organ)}" ><xsl:value-of select="@name"
/></td>
      <xsl:apply-templates />
    </tr>
  </xsl:template>
  
  <xsl:template match="city" >
    <xsl:choose>
      <xsl:when test="position() eq 1" >
        <td rowspan="{count(organ)}"><xsl:value-of select="@name"
/></td>
        <xsl:apply-templates />
      </xsl:when>
      <xsl:otherwise>
        <tr>
          <td rowspan="{count(organ)}"><xsl:value-of select="@name"
/></td>
          <xsl:apply-templates />
        </tr>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="organ" >
    <xsl:choose>
      <xsl:when test="position() eq 1" >
        <td><xsl:value-of select="@name" /></td>
        <td><xsl:value-of select="." /></td>
      </xsl:when>
      <xsl:otherwise>
        <tr>
          <td><xsl:value-of select="@name" /></td>
          <td><xsl:value-of select="." /></td>
        </tr>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:transform> 

Brad 

Current Thread