Inspired? No home

Sum in XSLT

There is a lack of good quality XSLT resources on the web. Even simple tasks can be quite hard to find an answer to when trying to search. Most results are from newsgroups which is not always that relevant or self-explanatory. I will try to post some simple XSLT and X-Path tips every now and then.

This is how you do a simple sum in XSLT:

<xsl:value-of select="sum(XPATH)"/>

If you want to sum all positive values: <xsl:value-of select="sum(//rs:data/z:row[@InvoiceOrderTotalWithTax > 0]/@InvoiceOrderTotalWithTax)">

If you want to sum all negative values: <xsl:value-of select="sum(//rs:data/z:row[@InvoiceOrderTotalWithTax < 0]/@InvoiceOrderTotalWithTax)"/>

The xpath that is referred to here is generated from ADO.

Written on 15 July 2005.
blog comments powered by Disqus