functx:avg-empty-is-zero

The average, counting “empty” values as zero

Description

The functx:avg-empty-is-zero function returns the average of the non-empty values in $values, over the number of nodes provided in $allNodes. This is useful for performing calculations where you want “missing” elements and/or attribute values to count as zero rather than not being included in the average at all.

Parameters

Description

values:numeric()

the values to be averaged

allNodes:node()

the sequence of all nodes to find the average over

Examples

XPath

Results

Explanation

functx:avg-empty-is-zero($in-xml//price/@discount, $in-xml//price)

4

The average discount for the prices is 4, if you want the discount on the 3rd and 4th prices to be counted as zero. Using fn:avg($in-xml//price/@discount) function would have raised an error because the discount on the 4th price is not a valid number. If the 4th price did not have a discount attribute, it would return 8.

XPath

Results

functx:avg-empty-is-zero($in-xml//price/@discount, $in-xml//price)

4

See Also