Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
Signature
Argument | Type | Meaning |
sequence | xs:anyAtomicType* | The input sequence. Any untyped atomic values in the input are converted to xs:double values. The resulting sequence must consist entirely of numbers, or entirely of durations of the same kind. |
Result | xs:anyAtomicType? | The average of the values in the input sequence. This will be a value of the same primitive type as the values in the input sequence. If the input values are xs:integer values, the result will be an xs:decimal . |
Effect
If the input sequence is empty, the result is an empty sequence. This is not an error, even though a literal interpretation of the rules would involve dividing by zero.
In all other cases the result is the same as
sum($sequence) div count($sequence)
. Note that
$sequence
here is the atomized sequence generated by the function calling mechanism. If the sequence supplied in the call was a sequence of nodes, the number of atomic values is not necessarily the same as the number of nodes. For example, if
avg(@a)
is called to process a single attribute that is defined in the schema to contain a list of integers, then it will return the average of these integers.
The sequence of operations is as follows:
1.
The sequence supplied in the argument is atomized (this is a standard action of the function calling rules when the required type only allows atomic values).
2.
Any untyped atomic values in the resulting sequence (typically, values extracted from nodes in a schemaless document) are converted to
xs:double
values. If this conversion fails, a runtime error is reported.
3.
If the sequence now contains any NaN (not-a-number) values, the result of the
avg()
function is NaN.
4.
If the values are all numeric, they are summed according to the rules for the numeric
+
operator, which means that the result will depend on the types that are present in the sequence. If there is at least one
xs:double
, the sum will be an
xs:double
; otherwise, if there is an
xs:float
it will be an
xs:float
, otherwise
xs:decimal
or
xs:integer
.
5.
If the values are all durations, they are similarly summed according to the rules of the
+
operator. In consequence, it is not possible to mix the two duration types,
xs:dayTimeDuration
and
xs:yearMonthDuration
.
6.
Finally, the total is divided by the number of items using the
div
operator. In the case of a numeric total, this means that the average will be the same numeric type as the sum, unless the sum is an
xs:integer
in which case the average will be an
xs:decimal
. If the items are durations, the result will be a duration of the same type as the items.
The processor is allowed to use a different algorithm which might behave differently in the event of arithmetic overflow.
Examples
Expression | Result |
avg((1.0, 2.6, 3.0)) | xs:decimal(‘2.2’) |
avg(()) | () |
avg((1, xs:float(‘3.5’), 5.5)) | xs:float(‘3.3333333’) |
avg((1, 2, 3)) | xs:decimal(‘2.0’) |
avg((xs:dayTimeDuration(‘P1D’), xs:dayTimeDuration (‘PT12H’))) | xs:dayTimeDuration (‘PT18H’) |
See Also
count()
on page 733
max()
on page 830
min()
on page 830
sum()
on page 889
base-uri
The
base-uri()
function returns the base URI of a specific node in a document.
Signature
Argument | Type | Meaning |
input-node (optional) | node()? | The node whose base URI is required |
Result | xs:string | The base URI of the node specified in the first argument, or the context node if there are no arguments |