sum Function (XPath)
Returns the sum of all nodes in the node-set. Each node is first converted to a number value before summing.
number sum(node-set)
Example
This example demonstrates the XPath expression sum(//a)
. It returns 6, which is the sum of the numbers in <a>
elements.
XML File (as.xml)
<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="sum.xsl"?>
<root>
<a>1</a>
<a>3</a>
<a>2</a>
</root>
XSLT File (sum.xsl)
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select='sum(//a)'/>
</xsl:template>
</xsl:stylesheet>
Formatted Output
6
Processor Output
<?xml version="1.0" encoding="UTF-16"?>6