How to sum() positive/negative numbers in XSLT?
Question
To sum a series of values (positive or negative) use the sum() function in XPath.
In this example, we get transactions from an XML source and puts it into a variable. Then we sum it up in the end.
XML Source:
<month month="04">
<balance in="987000,00" acnt="1100"/>
<balance in="167440,00" acnt="1280"/>
<balance in="1098000,00" acnt="1380"/>
<balance in="575000,00" acnt="1460"/>
<balance in="-75000,00" acnt="1469"/>
...
</month>
XSLT:
This results in the following structure:
<account>
<month month="name">
<in>987000.00</in>
</month>
...
</account>
To sum the values we use the following XSLT snippet:
<xsl:value-of select="sum(ext:node-set($sumSet)//in)"/>
This example was run in Visual Studio.
Solution
The question was a bit off so I edited the whole thing and provided the answer. :)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow