XPath/XQuery filter function

Summary

Returns an array containing those members of the $array for which $function returns true.

Signature

array:filter(
$array as array(*),
$function as function(item()*) as xs:boolean
) as array(*)

Properties

This function is deterministic, context-independent, focus-independent, and higher-order.

Rules

The effect of the function is equivalent to the following recursive definition:

if (array:size($array) eq 0)
then [ ]
else op:array-concat(
        if ($function(array:head($array))) then array:head($array) else [ ],
        array:filter(array:tail($array))
     )

Examples

The expression array:filter(["A", "B", 1, 2], function($x) {$x instance of xs:integer}) returns [1, 2].

The expression array:filter(["the cat", "sat", "on the mat"], function($s){fn:count(fn:tokenize($s)) gt 1}) returns ["the cat", "on the mat"].

The expression array:filter(["A", "B", "", 0, 1], boolean#1) returns ["A", "B", 1].

Error Conditions

As a consequence of the function signature and the function calling rules, a type error occurs if the supplied function $function returns anything other than a single xs:boolean item; there is no conversion to an effective boolean value.