This function recursively flattens arrays in the input sequence, leaving non-arrays intact.
declare function flatten($seq as item*) as item* { for $value in $seq return typeswitch ($value) case array return flatten($value[]) default return $value };