xsl-list
[Top] [All Lists]

Grouping and Summing information

2005-02-02 10:51:57
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.

From this I can assetain that it is possibly the 'for-each select="col"'
that is causing it not to work, but I dont understand why not.

I am able to change the XML Data, if there is a different / better solution.

Any pointers / help greatly appriciated.

Thanks for any help given
  Anton


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>