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