Re: [xsl] grouping and sum

Subject: Re: [xsl] grouping and sum
From: dmitrik@xxxxxxxxxxxxxx
Date: Mon, 6 Dec 2004 13:36:12 -0500 (GMT-05:00)
In a test version of the xml there are two Customers with the same name, and two different names.
The following xsl  displays  3 unique customers, which is correct, but the summation only 
works for the two single customers, not the instance with two trades for the same customer.
The current-group variable also does not seem to work - any idea what it requires? Would this
instance with the outer loop grouping follow your suggestion about  doing the summation in the 
inner loop?


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <xsl:key name="trade-to-step" match="Portfilio/Trade" use="Customer"/>
    
    <xsl:template match="/">
        <xsl:for-each select="Portfilio/Trade[generate-id() = generate-id(key('trade-to-step',Customer)[1])]">            
           <!-- <xsl:variable name="current-group" select="Trade[Customer= current()/Customer]"/>-->
           <xsl:variable name="current-group" select="Portfilio/Trade[Customer= current()]"/>
            <table>
                <tr>
                    <td colspan="4">
                        <b>
                            Code: <xsl:value-of select="Customer"/>	
			   Total: <xsl:value-of select="sum(Step/StepCharge_TTBlack)"/>
                        </b>
                    </td>                        
                </tr>
                   <tr>                     
                      <td>Amount</td>                      
                   </tr>                
                <xsl:for-each select="$current-group">
                    <tr>
                        <td>
                            <xsl:value-of select="Trade/NotionalRec"/>	
                        </td>                        
                    </tr>
                </xsl:for-each>
                <tr>
                    <td colspan="4">
                        <i>
                           <!-- Total: <xsl:value-of select="sum($current-group/NotionalRec)"/>-->
                           Total: <xsl:value-of select="sum(Step/StepCharge_TTBlack)"/>
                        </i>
                    </td>
                </tr>
            </table>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>


Thanks,
Dmitri


-----Original Message-----
From: David Carlisle <davidc@xxxxxxxxx>
Sent: Dec 6, 2004 11:18 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] grouping and sum


> Is it possible to group the whole xml document by the Customers node
> without hardcoding a specific customer? 

yes see jeni's site.
http://jenitennison.com/xslt/grouping

You may find it easiest not tp try doing grouping and summation all
inone step, but rather o a first pass that just does grouping, then use
easier XPaths that call sum() once all teh nodes that you want to sum
are grouped together.

David



________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

Current Thread