Description
The fn:sum function sums the values in a sequence. The $arg sequence can contain a mixture of numeric and untyped values. Numeric values are promoted as necessary to make them all the same type. Untyped values are cast as numeric xs:double values.
The function can also be used on duration values, so the $arg sequence can contain all xs:yearMonthDuration values or all xs:dayTimeDuration values (but not a mixture of the two).
The $zero argument allows you to specify an alternate value for the sum of the empty sequence. If $arg is the empty sequence, and $zero is provided, the function returns $zero . The $zero argument could be the empty sequence, the integer 0, the value NaN , a duration of zero seconds, or any other atomic value.
This description is © Copyright 2007, Priscilla Walmsley. It is excerpted from the book XQuery by Priscilla Walmsley, O'Reilly, 2007. For a complete explanation of this function, please refer to Appendix A of the book. Arguments and Return TypeName | Type |
$arg |
xs:anyAtomicType* |
$zero |
xs:anyAtomicType? |
return value |
xs:anyAtomicType? |
Examples<xsl:variable name="ordDoc" select="doc('https://www.datypic.com/functx./input/order.xml')"/>
|
XPath Example | Results |
---|
sum( (1, 2, 3) ) |
6 |
sum($ordDoc//item/@quantity) |
7 |
sum($ordDoc//item/@dept) |
Error FORG0001 |
sum( (xs:yearMonthDuration('P1Y2M'),
xs:yearMonthDuration('P2Y3M')) ) |
P3Y5M |
sum( (1, 2, 3, () ) ) |
6 |
sum( (1, 2, xs:yearMonthDuration('P1Y')) ) |
Error FORG0006 |
sum( () ) |
0 |
sum( (), () ) |
() |
See Alsofn:count | The number of items in a sequence | fn:avg | The average of a sequence of values |
History |
Recommended Reading:
|