XSLT Reference
sum()
Returns the sum of the numeric values in a node-set or sequence, or a specified zero value for empty sequences.
sum(node-set)Description
sum() converts each node in a node-set to a number (using the XPath number() rules) and returns their total as a double. If any node cannot be converted to a number, its value is treated as NaN, and the entire result is NaN.
In XSLT 2.0+, sum() accepts any sequence of atomic values that can be added together. A second argument may be provided as a zero value to return when the sequence is empty (in XPath 1.0, sum() on an empty node-set returns 0).
sum() is the standard way to compute totals over repeated elements, such as order amounts, quantities, or scores, without writing an explicit loop.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
node-set | node-set / sequence | Yes | The nodes or items to sum. Each is converted to a number. |
zero | atomic value | No | Value to return if the sequence is empty (XPath 2.0+). Defaults to 0. |
Return value
xs:double (XPath 1.0) or the type matching the input sequence (XPath 2.0+) — the numeric sum of all items.
Examples
Sum all child element values
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<invoice>
<line amount="29.99"/>
<line amount="14.50"/>
<line amount="5.00"/>
</invoice>
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/invoice">
<xsl:text>Total: </xsl:text>
<xsl:value-of select="sum(line/@amount)"/>
</xsl:template>
</xsl:stylesheet>
Output:
Total: 49.49
Conditional sum (sum filtered values)
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<sales>
<sale region="North" amount="100"/>
<sale region="South" amount="250"/>
<sale region="North" amount="175"/>
<sale region="South" amount="80"/>
</sales>
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/sales">
<totals>
<north><xsl:value-of select="sum(sale[@region='North']/@amount)"/></north>
<south><xsl:value-of select="sum(sale[@region='South']/@amount)"/></south>
</totals>
</xsl:template>
</xsl:stylesheet>
Output:
<totals>
<north>275</north>
<south>330</south>
</totals>
Empty sequence with fallback (XSLT 2.0)
<!-- Returns 0 when there are no discount elements -->
<xsl:value-of select="sum(discount/@value, 0)"/>
Notes
- Nodes that cannot be converted to a number (e.g., text like
"N/A") produceNaN, which propagates through the sum. Validate data or usenumber()with a fallback before summing. sum()on an empty node-set returns0in XPath 1.0. In XPath 2.0+, the second argument controls this behaviour.- For an average, there is no built-in
avg()in XPath 1.0. Compute it assum(nodes) div count(nodes). XPath 2.0+ providesavg(). sum()works on attribute nodes as well as element text nodes; the pathsum(items/item/@qty)is valid.