map:for-each

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

Signature

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

Properties

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

Rules

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 Properties of functions). 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.

Examples

The expression map:for-each(map{1:"yes", 2:"no"}, function($k, $v){$k}) returns some permutation of (1,2). (This function call is equivalent to calling map:keys. The result is in implementation-dependent order.)

The expression distinct-values(map:for-each(map{1:"yes", 2:"no"}, function($k, $v){$v})) returns some permutation of ("yes", "no"). (This function call returns the distinct values present in the map, in implementation-dependent order.)

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 function call returns a map with the same keys as the input map, with the value of each entry increased by one.)

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