O'Reilly logo

XSLT, 2nd Edition by Doug Tidwell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

[2.0] avg()

Given a sequence, returns the average value of the items in the sequence.

Syntax

xs:anyAtomicType? avg(xs:anyAtomicType*)

Input

A sequence of values.

Output

The average of the given sequence. You can calculate averages for six different datatypes: xs:integer, xs:double, xs:decimal, xs:float, xs:yearMonthDuration, and xs:dayTimeDuration.

Given a sequence of numeric values, the XSLT processor returns the average of those numbers, converting datatypes as necessary. Given a sequence of durations, the XSLT processor returns the average of those durations.

The avg() function assumes you’ll send it a sequence containing sensible data; if not, the XSLT processor throws an error. Asking for the average of the sequence (42, 57, 'blue') returns an error, as you’d expect.

Some notes about how the avg() function works:

  • To calculate the average of a sequence of durations, all the values must be xs:dayTimeDurations or xs:yearMonthDurations. You can’t mix the two types of durations; if you do, the XSLT processor throws an error.

  • If all the items in the sequence are of type xs:untypedAtomic, the XSLT processor attempts to cast each value to xs:double. If any item in the sequence can’t be converted to an xs:double, the XSLT processor throws an error.

  • Finally, if you pass the avg() function the empty sequence, the function returns the empty sequence. Although you’re not giving the function any useful data in this case, the XSLT processor doesn’t throw an error.

Defined in

XQuery 1.0 and XPath 2.0 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required