[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Grouping and Summing information

Subject: Grouping and Summing information
From: "Anton Damhuis" <ADamhuis@xxxxxxxxxxxxxx>
Date: Wed, 2 Feb 2005 19:51:57 +0200
damhuis
Hi,

I need some assistance with grouping and summing information.
I have based this solution on info from  http://www.jenitennison.com/ using
the "Muenchian Method"

The problem is I will only know at run time:
a) what the program must Group By (G)
b) what the program must Sum by (S)

There can be an unlimited amount of <col>'s but each @id will be incrementd,
and each <col> will always have a @type of either 'G' (for 'Group By') or
'S' (for Sum).

Example of more columns: Later on there might be a column called "Area Code"
and it would need to be Grouped by that as well.

Sample XML:
<data>
  <heading>
    <cols>
      <sort>0119744445</sort>
      <col id='1' type='G'>Nummber Called</col>
      <col id='2' type='S'>Call Duration</col>
      <col id='3' type='S'>Call Cost</col>
    </cols>
  </heading>
  <values>
    <cols>
      <sort>0114520000</sort>
      <col id='1' type='G'>0114520000</col>
      <col id='2' type='S'>100</col>
      <col id='3' type='S'>0.001</col>
    </cols>
    <cols>
      <sort>0119744445</sort>
      <col id='1' type='G'>0119744445</col>
      <col id='2' type='S'>111</col>
      <col id='3' type='S'>1.001</col>
    </cols>
    <cols>
      <sort>z0114520000</sort>
      <col id='1' type='G'>0114520000</col>
      <col id='2' type='S'>200</col>
      <col id='3' type='S'>0.002</col>
    </cols>
  </values>
</data>

My Transformation (XSL) looks like this:

.....
  <xsl:key name="totalByGroup" match="data/values/cols" use="sort" />
    ...
  <table>
    <xsl:for-each select="data/values/cols[count(. |
key('totalByGroup',sort)[1]) = 1]">
      <xsl:sort select="cols/sort" />
      <tr valign="top">
        <xsl:for-each select="col">
          <xsl:variable name="n" select="position()" />
          <td>
            <xsl:if test="@type = 'G'">
              <xsl:value-of select="." />
            </xsl:if>
            <xsl:if test="@type = 'S'">
              Error here=<xsl:value-of
select="sum(key('totalByGroup',sort)/col[($n)])" />!
            </xsl:if>
          </td>
        </xsl:for-each>
      </tr>
    </xsl:for-each>
  </table>

Quetion:
What am I doing wrong that the sum does not compute?

If I do a <xsl:value-of select="sum(key('totalByGroup',sort)/col[2])" />
outside of the 'for-each select="col"' it works fine.

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.