XPath function
sum()
Adds the numeric values of every node in a set. One non-numeric node makes the whole result NaN.
sum() converts each node in the set to a number and adds them. It is the only aggregate in XPath 1.0 — there is no avg(), min() or max().
Conversion is strict: a node whose string-value is not a number becomes NaN, and NaN plus anything is NaN. A single stray value therefore destroys the entire total rather than being skipped.
- Syntax
sum(node-set) → number- Returns
- number
- Kind
- Function
- Version
- XPath 1.0 (libxml2)
The sample documents
Every example on this page runs against this document. The sitemap one is namespaced on purpose — almost every real XML document is, and that changes which expressions match.
<?xml version="1.0" encoding="UTF-8"?>
<order id="A-1001" tier="gold">
<customer country="NO">Ada Lovelace</customer>
<lines>
<line sku="WIDGET-1" qty="2"><price>14.50</price></line>
<line sku="WIDGET-2" qty="1"><price>39.00</price></line>
<line sku="GIFT-WRAP" qty="1"><price>0.00</price></line>
</lines>
<note> Deliver after 09:00 </note>
</order>Related
- count()How many nodes a node-set contains. The fastest way to check whether an expression matches what you think it does.
- number()Converts to a number, producing NaN rather than an error when it cannot — which is how bad data passes silently.
- round(), floor() and ceiling()The three rounding functions. round() goes to the nearest integer, breaking ties upward — including for negatives.
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.