array:flatten

Replaces any array appearing in a supplied sequence with the members of the array, recursively.

Signature

array:flatten($input as item()*) as item()*

Properties

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

Rules

The function processes the items in the supplied sequence $input as follows:

The process is then repeated so long as the sequence contains an array among its items.

The function is equivalent to the following XQuery implementation (assuming static typing is not in force):

declare function flatten ($S as item()*) {
    for $s in $S return (
      typeswitch($s)
        case $a as array(*) return flatten($a?*)
        default return $s
)}

Notes

The argument to the function will often be a single array item, but this is not essential.

Unlike atomization, this function retains any nodes contained in the array.

Examples

The expression array:flatten([1, 4, 6, 5, 3]) returns (1, 4, 6, 5, 3).

The expression array:flatten(([1, 2, 5], [[10, 11], 12], [], 13)) returns (1, 2, 5, 10, 11, 12, 13).

The expression array:flatten([(1,0), (1,1), (0,1), (0,0)]) returns (1, 0, 1, 1, 0, 1, 0, 0).