Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help in writing XSL, to find sum

Status
Not open for further replies.

deepasona

Programmer
Apr 22, 2003
7
0
0
IN
I am beginner.Please help me to do this.
The following is a part of my xmlfile

<UsageItem>
<UsageDetail>
<CallStamp>21-05-2003 08:25:46 (GMT +05:00:00)</CallStamp>
<CalledNo>123456</CalledNo>
<Volume>45</Volume>
<Unit>Seconds</Unit>
<Remark>OSB</Remark>
<Country>Singapore</Country>
<RateCode>R</RateCode>
<Amount ChargeType=&quot;G&quot;>
<Currency>ZWD</Currency>
<Value>334.100</Value>
</Amount>
</UsageDetail>
<Amount ChargeType=&quot;G&quot;>
<Currency>ZWD</Currency>
<Value>334.100</Value>
<TaxAmount>
<TaxName>For Standard tax</TaxName>
<Percentage>5.000</Percentage>
<Value>16.000</Value>
</TaxAmount>
</Amount>
</UsageItem>

<UsageItem>
<UsageDetail>
.
.
.
.
.

I want to loop through the above
<xsl:for-each select=&quot;PersProd/ProductNode/SectionPart/UsageItem&quot;>

I need to sum the

--------PersProd/ProductNode/SectionPart/UsageItem/amount/TaxAmount/Value
---------------

of all the Tax that come under the USAGEITEM Tag.....
and display the sum before displaying the Details of USAGEDETAILS...


Waiting for the reply..

Deepa




 
Hi Deepa,

You can try the sum function:

<xsl:variable name=&quot;total&quot; value=&quot;sum(UsageItem/TaxAmount/Value)&quot;/>

now, 'total' will have the sum of all the 'value' nodes under TaxAmount.

All the best.
 
Hi thanks a lot ,

One more thing...

how to display the variable total....
like in html
How to refer it is it something like
<tr>
<xsl:value-of total>
</tr>


 
a beginner myself, i found this on the xml.com pages.

in your xsl use

<tr>{$total}</tr>

kib.at.kibje.com
 
You don't have to use a variable either, so a shortcut like
Code:
<xsl:value-of select=&quot;sum(UsageItem/TaxAmount/Value)&quot;/>

will do.
 
the previous comment is totally true, basically i see two advantages.

If you want the same value several times in your xslt, declare a variable in your root (making it global) and define it there. (variables CAN NOT BE CHANGED in xml !!)

you can then reference the value in every place in your xslt where you want.

If you only use the value once, just use the method described above. No need to declare a variable inside a node, because the variable is only valid inside that node, and when you only use it once you end up with more code then with the above method.

kib.at.kibje.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top