XPath/XQuery for-each function


Applies a supplied function to every entry in a map, returning the concatenation of the results.


$map as map(*),
$action as function(xs:anyAtomicType, item()*) as item()*
) as item()*


This function is nondeterministic-wrt-ordering, context-independent, focus-independent, and higher-order.


The function map:for-each takes any map as its $map argument and applies the supplied function to each entry in the map, in implementation-dependent order; the result is the sequence obtained by concatenating the results of these function calls.

The function is non-deterministic with respect to ordering (see ). This means that two calls with the same arguments are not guaranteed to process the map entries in the same order.

The function supplied as $action takes two arguments. It is called supplying the key of the map entry as the first argument, and the associated value as the second argument.


The expression map:for-each(map{1:"yes", 2:"no"}, function($k, $v){$k}) returns (1,2).

The expression distinct-values(map:for-each(map{1:"yes", 2:"no"}, function($k, $v){$v})) returns ("yes", "no").

The expression map:merge(map:for-each(map{"a":1, "b":2}, function($k, $v){map:entry($k, $v+1)})) returns map{"a":2, "b":3}.

This XQuery example converts the entries in a map to attributes on a newly constructed element node:

let $dimensions := map{'height': 3, 'width': 4, 'depth': 5}; return <box>{ map:for-each($dimensions, function ($k, $v) { attribute {$k} {$v} }) }</box>

The result is the element <box height="3" width="4" depth="5"/>.