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 ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.