xsl-list
[Top] [All Lists]

[xsl] More addition questions

2007-10-10 12:21:18
Hi,
I am using Xalan and XSLT 1.0 for the following

XML:

<?xml version="1.0" encoding="UTF-8"?>
<first>
    <second>
        <a val="4" key="1">a</a>
        <a val="2" key="2">b</a>
    </second>
    <second>
        <a val="3" key="1">c</a>
        <a val="8" key="2">d</a>
        <a val="1" key="2">e</a>
    </second>
</first>

OUTPUT:

  <?xml version="1.0" encoding="UTF-8" ?> 
 <top>
 <sumsForEachElementByKeyMultipliedByPosition> 
  <!-- sums For Each Element Grouped By Key And Multiplied By Position:
-->
  <element key="1">7</element> <!--  This would be e.g. (4 + 3) * 1 -->
  <element key="1">14</element> <!-- This would be e.g. (4 + 3) * 2 -->
  </sumsForEachElementByKeyMultipliedByPosition>
 <sumsForEachElementByKeyMultipliedByPosition>
  <element key="2">11</element> <!-- This would be e.g. (2 + 8 + 1) * 1
-->
  <element key="2">22</element> <!-- This would be e.g. (2 + 8 + 1) * 2
-->
  <element key="2">33</element> <!-- This would be e.g. (2 + 8 + 1) * 3
-->
  </sumsForEachElementByKeyMultipliedByPosition>

<!-- This is where I am stuck. Basically, after doing the above, I want
to add the numbers by each group again so the oupput should look like...
-->

  <sumOfAGroup> 
        <sum>21</sum> <!-- 7 + 14 -->
        <sum>66</sum> <!-- 11 + 22 + 33 -->
  </sumOfAGroup> 
  </top>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
    <xsl:key name="a" match="//a" use="@key"/>
    <xsl:template match="/">
        <top>
            
            <xsl:for-each select="//a[generate-id() =
generate-id(key('a', @key)[1])]">
                <sumsForEachElementByKeyMultipliedByPosition>
                    <xsl:for-each select="key('a', @key)">
                        <element>
                            <xsl:attribute name="key"><xsl:value-of
select="@key"/></xsl:attribute>
                            <xsl:value-of select="sum( key('a',
@key)/@val ) * position()"/>
                        </element>
                    </xsl:for-each>
                </sumsForEachElementByKeyMultipliedByPosition>
            </xsl:for-each>
           
            <sumOfAGroup>
                <!-- STUCK -->
            </sumOfAGroup>
            
        </top>
    </xsl:template>
</xsl:stylesheet>

Any suggestions without doing a mullti-pass?

Thanks,
HC.

--~------------------------------------------------------------------
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>
--~--