xsl-list
[Top] [All Lists]

Re: sum() with selective attribute

2003-01-06 15:07:24
At 2003-01-06 16:53 -0500, Lee, Insoo wrote:
  for the given xml shown below, I would get a grand total per each
currency...

This is first a grouping problem to establish the unique values for currency.

  I guess I can do something like
sum(/REPORT/ENTITY/FUND_GROUP/ROW/SOME_VALUE) along with "xsl:if", but I'd
like to avoid scanning through the tree again looking up CURRENCY
attribute... any suggestion?

Then a simple XPath expression qualifying the currency you want.

I hope the example below helps.

.................. Ken

T:\ftemp>type insoo.xml
<REPORT>
    <ENTITY NUMBER="1">
        <FUND_GROUP CURRENCY="GBP">
            <ROW>
                <SOME_VALUE>100</SOME_VALUE>
            </ROW>
            <ROW>
                <SOME_VALUE>200</SOME_VALUE>
            </ROW>
            <ROW>
                <SOME_VALUE>300</SOME_VALUE>
            </ROW>
        </FUND_GROUP>

        <FUND_GROUP CURRENCY="USD">
            <ROW>
                <SOME_VALUE>100</SOME_VALUE>
            </ROW>
            <ROW>
                <SOME_VALUE>200</SOME_VALUE>
            </ROW>
            <ROW>
                <SOME_VALUE>300</SOME_VALUE>
            </ROW>
        </FUND_GROUP>
    </ENTITY>

    <ENTITY NUMBER="2">
        <FUND_GROUP CURRENCY="GBP">
            <ROW>
                <SOME_VALUE>100</SOME_VALUE>
            </ROW>
            <ROW>
                <SOME_VALUE>200</SOME_VALUE>
            </ROW>
        </FUND_GROUP>

        <FUND_GROUP CURRENCY="USD">
            <ROW>
                <SOME_VALUE>200</SOME_VALUE>
            </ROW>
            <ROW>
                <SOME_VALUE>300</SOME_VALUE>
            </ROW>
        </FUND_GROUP>
    </ENTITY>

</REPORT>

T:\ftemp>type insoo.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:variable name="currencies" select="//FUND_GROUP"/>

<xsl:output indent="yes"/>

<xsl:template match="/">
  <currency-sums>
    <xsl:for-each select="$currencies">
      <xsl:if test="generate-id(.)=
                    generate-id( $currencies[ @CURRENCY =
                                              current()/@CURRENCY ] )">
        <sum currency="{(_at_)CURRENCY}">
          <xsl:value-of select="sum( $currencies[(_at_)CURRENCY=
                                     current()/@CURRENCY]/ROW/SOME_VALUE )"/>
        </sum>
      </xsl:if>
    </xsl:for-each>
  </currency-sums>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>saxon insoo.xml insoo.xsl
<?xml version="1.0" encoding="utf-8"?>
<currency-sums>
   <sum currency="GBP">900</sum>
   <sum currency="USD">1100</sum>
</currency-sums>
T:\ftemp>


--
Upcoming hands-on in-depth XSLT/XPath and/or XSL-FO:
-                             North America:  Feb 3 - Feb 7,2003

G. Ken Holman                mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Crane Softwrights Ltd.         http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999 (F:-0995)
ISBN 0-13-065196-6                      Definitive XSLT and XPath
ISBN 0-13-140374-5                              Definitive XSL-FO
ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
ISBN 1-894049-10-1              Practical Formatting Using XSL-FO
Male Breast Cancer Awareness http://www.CraneSoftwrights.com/s/bc


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