Re: [xsl] Another grouping question

Subject: Re: [xsl] Another grouping question
From: Josh Canfield <joshcanfield@xxxxxxxxx>
Date: Tue, 24 Aug 2004 12:19:41 -0700
Here is a template that will give you the output you requested....


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

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

<xsl:template match="/">
  <xsl:call-template name="table">
    <xsl:with-param name="nodes" select="/root/type/kind/age"/>
    <xsl:with-param name="title" select="'ALL TYPES'"/>
  </xsl:call-template>

  <xsl:for-each select="/root/type">
    <xsl:call-template name="table">
      <xsl:with-param name="nodes" select="kind/age"/>
    <xsl:with-param name="title" select="concat('TYPE ',@name)"/>
    </xsl:call-template>
  </xsl:for-each>
</xsl:template>

<xsl:template name="table">
  <xsl:param name="nodes"/>
  <xsl:param name="title"/>

<xsl:text>&#x0A;&#x0A;</xsl:text>
<xsl:value-of select="$title"/>
<xsl:text>
AGE   DIV_TOTAL   SEC   HGT   KIU
</xsl:text>
  <xsl:call-template name="row">
    <xsl:with-param name="minAge" select="0"/>
    <xsl:with-param name="maxAge" select="3"/>
    <xsl:with-param name="nodes" select="$nodes"/>
  </xsl:call-template>
  <xsl:text>&#x0A;</xsl:text>
  <xsl:call-template name="row">
    <xsl:with-param name="minAge" select="4"/>
    <xsl:with-param name="maxAge" select="5"/>
    <xsl:with-param name="nodes" select="$nodes"/>
  </xsl:call-template>
  <xsl:text>&#x0A;</xsl:text>
  <xsl:call-template name="row">
    <xsl:with-param name="minAge" select="6"/>
    <xsl:with-param name="maxAge" select="10"/>
    <xsl:with-param name="nodes" select="$nodes"/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="row">
  <xsl:param name="minAge"/>
  <xsl:param name="maxAge"/>
  <xsl:param name="nodes"/>

  <xsl:variable name="byAge" select="$nodes[number(@name) &gt;=
$minAge and number(@name) &lt;= $maxAge]"/>

  <xsl:value-of select="$minAge"/><xsl:text>-</xsl:text><xsl:value-of
select="$maxAge"/><xsl:text>    </xsl:text>
  <xsl:value-of select="sum($byAge/division)"/><xsl:text>    </xsl:text>
  <xsl:value-of select="sum($byAge/division[@name='SEC'])"/><xsl:text>
   </xsl:text>
  <xsl:value-of select="sum($byAge/division[@name='HGT'])"/><xsl:text>
   </xsl:text>
  <xsl:value-of select="sum($byAge/division[@name='KIU'])"/><xsl:text>
   </xsl:text>
</xsl:template>

</xsl:stylesheet>

Josh

On Sat, 21 Aug 2004 00:53:56 +0000, William Jordan
<williamhjordan@xxxxxxxxxxx> wrote:
> Sorry about this question.  I know there have been many questions regarding
> grouping, but I for one am still not getting it.
>
> Here is my problem.
>
> I am using XSLT1.0
>
> My data is in this format.
>
> <type name="UNIT1">
>   <kind name="DMI">
>      <age name="2">
>         <division name="SEC">9</division>
>         <division name="HGT">4</division>
>         <division name="KIU">12<division>
>      </age>
>      <age name="3">
>         <division name="SEC">4</division>
>         <division name="HGT">56</division>
>         <division name="KIU">125<division>
>      </age>
>      <age name="5">
>         <division name="SEC">61</division>
>         <division name="HGT">316</division>
>         <division name="KIU">83<division>
>      </age>
>   </kind>
>   <kind name="TYD">
>      <age name="3">
>         <division name="SEC">8</division>
>         <division name="HGT">71</division>
>         <division name="KIU">948</division>
>      </age>
>      <age name="10">
>         <division name="SEC">794</division>
>         <division name="HGT">51</division>
>         <division name="KIU">6</division>
>      </age>
>   </kind>
> </type>
> <type name="UNIT2">
>   <kind name="POR">
>      <age name="10">
>         <division name="SEC">7</division>
>         <division name="HGT">94</division>
>         <division name="KIU">5</division>
>      </age>
>   </kind>
> </type>
>
> My xsl file should first sum all divisions, grouped by the number of days
> aged <age>.  So, for the first table, the first row (0 - 3), records where
> age is between 0 and 3 should be included.  For the second row, records
> where the age is between 4 and 5 should be included.  All types are
included
> in this first table.
>
> The second table should include only those records where type = UNIT1.  The
> rows will work the same (row 1 where age is between 0 and 3, etc...).
>
> Same for the third table.
>
> The output should be thus
>
> ALL TYPES
> AGE   DIV_TOTAL   SEC   HGT   KIU
> 0  3   1237           21      131     1085
> 4  5   460             61      316      83
> 6  10  957            801    145      11
>
> TYPE UNIT1
> AGE   DIV_TOTAL   SEC   HGT   KIU
> 0  3   210             13       60       137
> 4  5   460             61      316      83
> 6  10  957            794    51        6
>
> TYPE UNIT2
> AGE   DIV_TOTAL   SEC   HGT   KIU
> 0  3    0                0       0         0
> 4  5    0                0       0         0
> 6  10  106             7       94       5
>
> I know everyone must be getting tired of grouping questions.  I have read
up
> on them and still don't get how to do them in XSLT.
>
> Thanks in advance
>
> Bill
>
> _________________________________________________________________
> Express yourself instantly with MSN Messenger! Download today - it's FREE!
> http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

Current Thread