XPath/XQuery flatten function


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


$input as item()*
) as item()*


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


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

  • An item that is an array is replaced by its members, retaining order.

  • Any other item is retained unchanged.

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 (
        case $a as array(*) return flatten($a?*)
        default return $s


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).


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.