Re: [xsl] Grouping by key

Subject: Re: [xsl] Grouping by key
From: gopinath.emmidisetty@xxxxxxxxxxxxxxxxxxxxxxxxx
Date: Fri, 12 Nov 2004 10:45:55 +0000
Hi,

Thanks, now the categorised output is working fine.  Now, I have got 
another question.  While categorising the output, I want to total the 
values in one of the columns.  I know there is a function called 
sum(node-set) and tried to use(search for sum function in my xsl file). 
>From the fact that I don't get the results, I don't know how to apply sum 
function in juction with key function.  Any suggestions, please?

Thanks.

XML Docment:

<documents>
<account>
<accountnumber colno="0">0001</accountnumber>
<companyname colno="1">ABC Ltd</companyname>
<town colno="2">Ford</town>
<ordervalue colno="3">115.49</ordervalue>
<postcode colno="4">FD13QG</postcode>
</account>
<account>
<accountnumber colno="0">0002</accountnumber>
<companyname colno="1">XYZ Ltd</companyname>
<town colno="2">Ford</town>
<ordervalue colno="3">115.49</ordervalue>
<postcode colno="4">XY13QG</postcode>
</account>
...
...
...
</documents>

xsl document:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
        version="1.0">
        <xsl:output method="xml" omit-xml-declaration="yes"/>
        <xsl:param name="catCol" select="2"/>
        <xsl:key name="categorise" match="account" use="*[@colno]" />
 
        <xsl:template match="/">
                        <div> <xsl:apply-templates select="documents" /> 
</div>
        </xsl:template>

        <xsl:template match="documents">
                <table>
                        <xsl:apply-templates select="account[generate-id() 
= generate-id( key( 'categorise', *[@colno=$catCol] )[1] )]" 
mode="catcols">
                                <xsl:sort select="*[@colno=$catCol]"/>
                         </xsl:apply-templates>
                </table>
        </xsl:template>
 
        <xsl:template match="account" mode="catcols">
                <tr>
                        <td colspan="2">
                                <b><xsl:value-of select="*[@colno=$catCol]" /></b> ( <xsl:value-of select="count(.|key( 'categorise', *[@colno=$catCol] ) )" /> )
                        </td>
                        <td colspan="2">
                                <xsl:value-of select="sum(*[@colno=3][position()])" />
                        </td>
                        <xsl:apply-templates select="key('categorise', *[@colno=$catCol])" mode="catrows" />
                </tr>

        </xsl:template>

        <xsl:template match="account" mode="catrows">
                <tr>
                        <td width="100"> <xsl:value-of select="*[@colno=0]" /> </td>
                        <td width="100"> <xsl:value-of select="*[@colno=1]" /> </td>
                        <td width="100"> <xsl:value-of select="*[@colno=3]" /> </td>
                        <td width="100"> <xsl:value-of select="*[@colno=4]" />      </td>
                </tr>
        </xsl:template>
</xsl:stylesheet>

Current Thread