[xsl] subtotal on subgroups question

Subject: [xsl] subtotal on subgroups question
From: "Ibeling, Narisa" <narisa.h.ibeling@xxxxxxxxxxxxx>
Date: Tue, 22 Oct 2002 17:08:41 -0500
Here is my xml:

<?xml version="1.0" encoding="UTF-8" ?> 
- <document xmlns:sql="http://apache.org/cocoon/SQL/2.0";> 
- <records> 
- <row> 
<center>Rikshospital - Copenhagen</center> 
<geography>Europe</geography> 
<inv_name>Prof. P. Olsen</inv_name> 
<wektotl>0</wektotl> 
<wekaort>0</wekaort> 
<wekmitr>0</wekmitr> 
<country>Denmark</country> 
<cumwekt>33</cumwekt> 
<cumweka>23</cumweka> 
<cumwekm>10</cumwekm> 
</row> 
- <row> 
<center>Rikshospital - Oslo</center> 
<geography>Europe</geography> 
<inv_name>Prof. J. Svennevig</inv_name> 
<wektotl>0</wektotl> 
<wekaort>0</wekaort> 
<wekmitr>0</wekmitr> 
<country>Norway</country> 
<cumwekt>57</cumwekt> 
<cumweka>43</cumweka> 
<cumwekm>14</cumwekm> 
</row> 
</records>
</document>

I am grouping the output data in geography and then country.  I got it to work.  But I am having difficulties getting the subtotals by geography.  
Say, I need to get the totals of Europe and Asian or North America.  I use sum() function to get the grand total but I could not get the subtotals.

Here is my xsl:
<xsl:key name="group-by-geo" match="row" use="geography" />
<xsl:key name="group-by-country" match="row" use="concat(geography,' ',country)" />
    
    <xsl:template match="document/records">
    
    <fo:table>
        <fo:table-column column-width="250pt"/>
        <fo:table-column column-width="150pt"/>
        <fo:table-column column-width="50pt"/>
        <fo:table-column column-width="50pt"/>
        <fo:table-column column-width="50pt"/>
        <fo:table-column column-width="50pt"/>
        <fo:table-column column-width="50pt"/>
        <fo:table-column column-width="50pt"/>
        
        <fo:table-body font-size="8pt" font-family="sans-serif">
        <xsl:apply-templates
           match="row[generate-id(.)=generate-id(key('group-by-geo', geography)[1])]"
           mode="row_by_group" />
        
          <fo:table-row>
            <fo:table-cell>
                <fo:block>
                    <fo:leader leader-pattern="space" leader-length="20cm" />
                </fo:block>
            </fo:table-cell>
          </fo:table-row>
          <fo:table-row>
            <fo:table-cell>
                <fo:block>
                    <fo:leader leader-pattern="space" leader-length="20cm" />
                </fo:block>
            </fo:table-cell>
          </fo:table-row>
          <fo:table-row>
            <fo:table-cell>
                <fo:block>
                    TOTALS
                </fo:block>
            </fo:table-cell>
          </fo:table-row>
                                 
          
           <fo:table-row>
             <fo:table-cell font-size="10pt" font-weight="bold">
                   <fo:block>WORLDWIDE TOTAL</fo:block>
             </fo:table-cell>
             <fo:table-cell>
             </fo:table-cell>
             <fo:table-cell text-align="center">
                <fo:block><xsl:value-of select="sum(row/cumwekt)"/></fo:block>
             </fo:table-cell>
             <fo:table-cell text-align="center">
                <fo:block><xsl:value-of select="sum(row/cumweka)"/></fo:block>
             </fo:table-cell>
             <fo:table-cell text-align="center">
                <fo:block><xsl:value-of select="sum(row/cumwekm)"/></fo:block>
             </fo:table-cell>
             <fo:table-cell text-align="center">
                <fo:block><xsl:value-of select="sum(row/wektotl)"/></fo:block>
             </fo:table-cell>
             <fo:table-cell text-align="center">
                <fo:block><xsl:value-of select="sum(row/wekaort)"/></fo:block>
             </fo:table-cell>
             <fo:table-cell text-align="center">
                <fo:block><xsl:value-of select="sum(row/wekmitr)"/></fo:block>
             </fo:table-cell>
            </fo:table-row>
        </fo:table-body>
    </fo:table>
  </xsl:template>
  
  <xsl:template match="row" mode="row_by_group">
  <xsl:variable
      name="geo_group"
      select="key('group-by-geo', geography)" />
  
    <fo:table-row>
       <fo:table-cell font-weight="bold">
          <fo:block><xsl:value-of select="geography" /></fo:block>
       </fo:table-cell>
    </fo:table-row>

    <xsl:apply-templates
     select="$geo_group[generate-id(.) =
             generate-id(key('group-by-country', concat(geography,' ',country))[1])]"
     mode="row_by_country">
      <xsl:sort select="country"/>
    </xsl:apply-templates>
 
  </xsl:template>

  <xsl:template match="row" mode="row_by_country">
    <fo:table-row>
       <fo:table-cell font-weight="bold">
          <fo:block text-indent="0.2in"><xsl:value-of select="country" /></fo:block>
       </fo:table-cell>
    </fo:table-row>
    <xsl:apply-templates
     select="key('group-by-country', concat(geography,' ',country))"
     mode="render_content">
    <xsl:sort select="center"/>
    </xsl:apply-templates>
   
  </xsl:template>

  <xsl:template match="row" mode="render_content">
    <fo:table-row>
       <fo:table-cell>
          <fo:block text-indent="0.4in"><xsl:value-of select="center" /></fo:block>
       </fo:table-cell>
       <fo:table-cell>
          <fo:block><xsl:value-of select="inv_name" /></fo:block>
       </fo:table-cell>
       <fo:table-cell text-align="center">
          <fo:block><xsl:value-of select="cumwekt" /></fo:block>
       </fo:table-cell>
       <fo:table-cell text-align="center">
          <fo:block><xsl:value-of select="cumweka" /></fo:block>
       </fo:table-cell>
       <fo:table-cell text-align="center">
          <fo:block><xsl:value-of select="cumwekm" /></fo:block>
       </fo:table-cell>
       <fo:table-cell text-align="center">
          <fo:block><xsl:value-of select="wektotl" /></fo:block>
       </fo:table-cell>
       <fo:table-cell text-align="center">
          <fo:block><xsl:value-of select="wekaort" /></fo:block>
       </fo:table-cell>
       <fo:table-cell text-align="center">
          <fo:block><xsl:value-of select="wekmitr" /></fo:block>
       </fo:table-cell>
    </fo:table-row>
  </xsl:template>

Your help is greatly appreciated.


Thanks,

Narisa Ibeling


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


Current Thread