home contribute faq download

FunctX XSLT Functions

fn:sum

The sum of values in a sequence

Google
Webxsltfunctions.com

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 Type

NameType
$arg xs:anyAtomicType*
$zero xs:anyAtomicType?
return value xs:anyAtomicType?

Examples

<xsl:variable name="ordDoc" select="doc('http://www.functx.com/input/order.xml')"/>
XPath ExampleResults
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 Also

fn:countThe number of items in a sequence
fn:avgThe average of a sequence of values

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26W3C, XQuery 1.0 and XPath 2.0 Functions and Operators, http://www.w3.org/TR/xpath-functions/
Datypic XSLT Services

Recommended Reading:

XQuery