Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 8th, 2003, 05:00 AM
Registered User
 
Join Date: Nov 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSL-help-sum of nodes baed on a condition

Hi All,

I'm badly in need of a help of finding the sum of the nodes based on a condition.

Here is how my xml look like

<?xml version="1.0" encoding="ISO-8859-1"?>

<?xml-stylesheet type="text/xsl" href="activexsl.xsl"?>
<page>
<row>
        <Code>A112</Code>
                <LastName>KUMAR</Name>
            <DOD>03/12/2080</DOD>
                    <Amount>2000</Amount>
            <State>TN</State>
</row>
<row>
        <Code>A113</Code>
                <LastName>KUMAR</Name>
            <DOD>03/12/2080</DOD>
                    <Amount>200</Amount>
            <State>TN</State>
</row>
<row>
        <Code>A113</Code>
                <LastName>KUMAR</Name>
            <DOD>03/12/2080</DOD>
                    <Amount>2000</Amount>
            <State>TN</State>
</row>
<row>
        <Code>A114</Code>
                <LastName>KUMAR</Name>
            <DOD>03/12/2080</DOD>
                    <Amount>2000</Amount>
            <State>TN</State>
</row>
</page>
I need to display the result in a html page grouping by code and also I have to provide subtotals for a code.

The format is like

Code 112
LastName DOD Amount State
KUMAR 03/12/2080 2000 TN

Total :2000

Code 113
LastName DOD Amount State
KUMAR 03/12/2080 200 TN
KUMAR 03/12/2080 2000 TN

Total :2200

Code 114
LastName DOD Amount State
KUMAR 03/12/2080 2000 TN

Total :2000

ok. Here is my xsl...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

    <xsl:for-each select="page/row">
        <table width="100%">
        <xsl:variable name = "PrevCode" select = "preceding-sibling::node()[1]/Code"/>
            <xsl:variable name = "NextCode" select = "following-sibling::node()[1]/Code"/>
        <xsl:if test="(Code != $PrevCode) or not(boolean($PrevCode))">
                 <p class="content-text-bold"> Code:<xsl:value-of select="Code"/> </p>
             <tr>
                <td>Last name</td>
                      <td>DOD</td>
              <td>Amount</td>
              <td> State</td>
            </tr>
        </xsl:if>
<tr class="content-text" align="center">
<td width="68"><xsl:value-of select="LastName"/></td>
<td width="70"><xsl:value-of select="DOD"/></td>
<td width="87"><xsl:value-of select="Amount"/></td>
<td width="44"><xsl:value-of select="State"/></td>
</tr>
 </table>

<xsl:if test ="(Code != $NextCode) or not(boolean($NextCode)) ">
<xsl:variable name="total" select="sum(../row[Code=$PrevCode]/Amount)"/>
Total : <xsl:value-of select="$total"/>
</xsl:if>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>


Now when i calculate the sum, whenever successive codes are varying, I'm displaying the result, and I'm calculating it by using

<xsl:variable name="total" select="sum(../row[Code=$PrevCode]/Amount)"/>

It works well, but for first(A112) and and last(A114) codes, I'm getting total as 0 and 2200 respectively.

Code 112
LastName DOD Amount State
KUMAR 03/12/2080 2000 TN.
Total :0


Code 113
LastName DOD Amount State
KUMAR 03/12/2080 200 TN
KUMAR 03/12/2080 2000 TN

Total :2200

Code 114
LastName DOD Amount State
KUMAR 03/12/2080 2000 TN

Total :2200

Can anybody help in me resolving this and tell me what is the problem with the above?

Thanks,
kumar.


 
Old November 8th, 2003, 07:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Your grouping technique is not good and is not generic: if the sequence of rows would be permuted, your grouping technique would not work anymore. Use Muenchian grouping:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="code-to-row" match="page/row" use="Code"/>

    <xsl:template match="/">
        <xsl:for-each select="page/row[generate-id() = generate-id(key('code-to-row', Code)[1])]">
            <xsl:sort select="Code"/>
            <xsl:variable name="current-group" select="/page/row[Code = current()/Code]"/>
            <table>
                <tr>
                    <td colspan="4">
                        <b>
                            Code: <xsl:value-of select="Code"/>
                        </b>
                    </td>                        
                </tr>
                   <tr>
                      <td>Last name</td>
                      <td>DOD</td>                       
                      <td>Amount</td>
                      <td> State</td>
                   </tr>                
                <xsl:for-each select="$current-group">
                    <tr>
                        <td>
                            <xsl:value-of select="LastName"/>
                        </td>
                        <td>
                            <xsl:value-of select="DOD"/>
                        </td>
                        <td>
                            <xsl:value-of select="Amount"/>
                        </td>
                        <td>
                            <xsl:value-of select="State"/>
                        </td>
                    </tr>
                </xsl:for-each>
                <tr>
                    <td colspan="4">
                        <i>
                            Total: <xsl:value-of select="sum($current-group/Amount)"/>
                        </i>
                    </td>
                </tr>
            </table>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
Look at http://jenitennison.com/xslt/grouping/index.html. Here you can find many useful things concerned with grouping issues.

Regards,
Armen
 
Old November 8th, 2003, 10:08 AM
Registered User
 
Join Date: Nov 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Aremn,

Thanks a lot for the help and suggesting better practices...

skumar






Similar Threads
Thread Thread Starter Forum Replies Last Post
Usage of '*' in test condition of <xsl:when> vikkiefd XSLT 5 March 12th, 2008 10:31 PM
How to sum nodes with some intermediate process r2u_2 XSLT 3 January 7th, 2008 01:41 AM
XSL:Variable SELECT=sum(condictions) andreracco XSLT 3 August 15th, 2007 08:51 AM
Count nodes based of if condition suri_1811 XSLT 1 December 7th, 2006 08:00 PM
Handling recursive XML Nodes using XSL RichaM XSLT 5 June 21st, 2005 02:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.