Array Functions
This module provides functions for manipulating arrays. It was introduced with XQuery 3.1 and extended with XQuery 4.0. All functions are described in detail in the XQuery Functions and Operators specification.
Conventions
All functions and errors in this module are assigned to the http://www.w3.org/2005/xpath-functions/array namespace, which is statically bound to the array prefix. 
Functions
array:append
| Signature | array:append( $array as array(*), $member as item()* ) as array(*) | 
|---|---|
| Summary | Returns a copy of $arraywith$memberattached. | 
| Examples | Result:[ 'member1' ] | 
array:build
| Signature | array:build( $input as item()*, $action as fn(item(), xs:integer) as item()* := fn:identity#1 ) as array(*) | 
|---|---|
| Summary | Builds an array by evaluating $actionfor each item in the$inputsequence. | 
| Examples | Result:[ 1, 2, 3 ]Result:[ 1, (1, 2), (1, 2, 3) ]Result:[ ('o', 'n', 'e'), ('t', 'w', 'o') ] | 
array:empty
| Signature | array:empty( $array as array(*) ) as xs:boolean | 
|---|---|
| Summary | Returns trueif$arraycontains no members. | 
| Examples | Result:false()Result:true()Result:false() | 
array:filter
| Signature | array:filter( $array as array(*), $predicate as fn(item()*) as xs:boolean ) as array(*) | 
|---|---|
| Summary | Returns a new array with those members of $arrayfor which$predicatereturnstrue. | 
| Examples | Result:[ 1, 3 ] | 
array:flatten
| Signature | array:flatten( $items as item()* ) as item()* | 
|---|---|
| Summary | Recursively flattens all arrays that occur in $items. | 
| Examples | Result:'a', 'b'Result:1, 2, 3, 4 | 
array:fold-left
| Signature | array:fold-left( $array as array(*), $zero as item()*, $action as fn(item()*, item()*, xs:integer) as item()* ) as item()* | 
|---|---|
| Summary | Evaluates the $actioncumulatively on successive members of$arrayfrom left to right, and uses$zeroas first argument.Note: Contrary to the official specification, the current position of the iteration can be retrieved via the third parameter of the higher-order function argument. | 
| Examples | Result:55. Computes the sum of the integers1to10. | 
array:fold-right
| Signature | array:fold-right( $array as array(*), $zero as item()*, $action as fn(item()*, item()*, xs:integer) as item()* ) as item()* | 
|---|---|
| Summary | Evaluates $actioncumulatively on successive members of$arrayfrom right to left, and uses$zeroas first argument.Note: Contrary to the official specification, the current position of the iteration can be retrieved via the third parameter of the higher-order function argument. | 
| Examples | The following query is equivalent to the expressionarray:reverse(array { 1 to 5 }). | 
array:foot
| Signature | array:foot( $array as array(*) ) as item()* | 
|---|---|
| Summary | Returns the last member of $array. | 
| Examples | Result:3Result:'c', 'd' | 
array:for-each
| Signature | array:for-each( $array as array(*), $action as fn(item()*, xs:integer) as item()* ) as array(*) | 
|---|---|
| Summary | Creates a new array, in which each member is computed by applying $actionto the corresponding member of$array. | 
| Examples | Result:[ 2, 3, 4, 5, 6] | 
array:for-each-pair
| Signature | array:for-each-pair( $array1 as array(*), $array2 as array(*), $action as fn(item()*, item()*, xs:integer) as item()* ) as array(*) | 
|---|---|
| Summary | Creates a new array by evaluating $actionfor each pair of members at the same position in$array1and$array2. | 
| Examples | Result:[ 5, 7, 9 ] | 
array:get
Updated: Default parameter added.
| Signature | array:get( $array as array(*), $position as xs:integer, $default as item() := () ) as item()* | ||
|---|---|---|---|
| Summary | Returns the member of $arrayat the specified$position. If the specified member does not exist, an error is raised, or (if specified) the value of$defaultis returned. | ||
| Errors | 
 | ||
| Examples | Result:1Result:'unknown' | 
array:head
| Signature | array:head( $array as array(*) ) as item()* | ||
|---|---|---|---|
| Summary | Returns the first member of $array. | ||
| Errors | 
 | ||
| Examples | Result:'a'Result:[ 'a', 'b' ] | 
array:index-of
| Signature | array:index-of( $array as array(*), $target as xs:anyAtomicType*, $collation as xs:string? := fn:default-collation() ) as xs:integer* | 
|---|---|
| Summary | Returns the positions of the members of $arraythat match$target. | 
| Examples | Result:2, 4 | 
array:index-where
| Signature | array:index-where( $array as array(*), $predicate as fn(item()*, xs:integer) as xs:boolean ) as xs:integer* | 
|---|---|
| Summary | Returns the positions of all members of $arraythat match the$predicatefunction. | 
| Examples | Result:3Result:2, 4 | 
array:insert-before
| Signature | array:insert-before( $array as array(*), $position as xs:integer, $member as item()* ) as array(*) | 
|---|---|
| Summary | Returns an array with the members of $arrayand an additional$memberat a specified$position. | 
| Examples | Result:[ 'a', ('x', 'y'), 'b' ] | 
array:items
Added: New function.
| Signature | array:items( $array as array(*) ) as item()* | 
|---|---|
| Summary | Returns a sequence with all the values of $array. | 
| Examples | Result:1, 2, 3 | 
array:join
Updated: $separator parameter added.
| Signature | array:join( $arrays as array(*)*, $separator as array(*)? := () ) as array(*) | 
|---|---|
| Summary | Concatenates the contents of several $arraysinto a single array. If a$separatoris supplied, its members are inserted before the members of the second and the following arrays. | 
| Examples | Result:[]Result:[ 1, 2, 3 ]Result:[ 'a', 'b', 'c', 'd' ]Result:[ 1, (), 2, (), 3 ] | 
array:members
Added: New function.
| Signature | array:members( $arrays as array(*) ) as record(value as item()*)* | 
|---|---|
| Summary | Returns the members of an array as a sequence of value records. | 
| Examples | Result:()Result:{ 'value': (1, 2) }Result:{ 'value': 1 }, { 'value': 2 } | 
array:put
| Signature | array:put( $array as array(*), $position as xs:integer, $member as item()* ) as array(*) | ||
|---|---|---|---|
| Summary | Returns a copy of $arraywith$memberreplaced at the specified$position. | ||
| Errors | 
 | ||
| Examples | Result:[ 'a', 'd', 'c' ] | 
array:remove
| Signature | array:remove( $array as array(*), $positions as xs:integer* ) as array(*) | ||
|---|---|---|---|
| Summary | Returns a copy of $arraywithout the member at the specified$positions. | ||
| Errors | 
 | ||
| Examples | Result:[ 'b' ] | 
array:reverse
| Signature | array:reverse( $array as array(*) ) as array(*) | 
|---|---|
| Summary | Returns a new array with all members of $arrayin reverse order. | 
| Examples | Result:[ 3, 2, 1 ] | 
array:size
| Signature | array:size( $array as array(*) ) as xs:integer | 
|---|---|
| Summary | Returns the number of members in $array. Note that because an array is an item,fn:countwhen applied to an array always returns1. | 
| Examples | Result:3Returns1, because the array contains a single sequence with 3 integers. | 
array:slice
| Signature | array:slice( $array as array(*), $start as xs:integer? := (), $end as xs:integer? := (), $step as xs:integer? := () ) as array(*) | 
|---|---|
| Summary | Returns selected members of an $arraybased on their position:
 | 
| Examples | Result:[ 3, 5 ]Result:[ 5, 3 ]Result:[ 1, 3, 5 ]Result:[ 1, 5 ] | 
array:sort
Updated: Reverted to 3.1 behavior
| Signature | array:sort( $array as array(*), $collation as xs:string? := fn:default-collation(), $key as fn($item as item()) as xs:anyAtomicType* := fn:data#1 ) as array(*) | 
|---|---|
| Summary | Returns a new array with sorted $arraymembers. A$collationand a$keycan be supplied, which will be applied on each sort items. The members resulting from the sort key will be sorted using the semantics of theltoperator. | 
| Examples | Result:[ 1, 2, 3 ]Result:[ 1, -2, 3 ]Result:[ 3, 2, 1 ] | 
array:split
| Signature | array:split( $array as array(*) ) as array(*)* | 
|---|---|
| Summary | Returns the members of $arrayas a sequence of singleton arrays. | 
| Examples | Result:[ 1 ], [ 2 ], [ 3 ]Result:[ () ], [ 1 ], [ (2, 3) ] | 
array:subarray
| Signature | array:subarray( $array as array(*), $position as xs:integer, $length as xs:integer := () ) as array(*) | ||||
|---|---|---|---|---|---|
| Summary | Constructs a new array with with $lengthmembers of$arraybeginning from the specified$position. The two-argument version of the function returns the same result as the three-argument version when called with$lengthequal to the value ofarray:size($array) - $position + 1. | ||||
| Errors | 
 | ||||
| Examples | Result:[ 'b', 'c' ] | 
array:tail
| Signature | array:tail( $array as array(*) ) as array(*) | ||
|---|---|---|---|
| Summary | Returns a new array with all members except the first from $array. | ||
| Errors | 
 | ||
| Examples | Result:[ 'b', 'c' ] | 
array:trunk
| Signature | array:trunk( $array as array(*) ) as array(*) | ||
|---|---|---|---|
| Summary | Returns a new array with all members except the last from $array. | ||
| Errors | 
 | ||
| Examples | Result:[ 'a', 'b' ] | 
Errors
| Code | Description | 
|---|---|
| FOAY0001 | The specified index extends beyonds the bounds of an array. | 
| FOAY0002 | The specified length is less than zero. | 
Changelog
Version 12.0- Added: array:items,array:members.
- Updated: array:get: Default parameter added.
- Updated: array:join
- Added: array:build,array:empty,array:foot,array:index-of,array:index-where,array:slice,array:split,array:trunk
- Updated: array:fold-left,array:fold-right,array:for-each,array:for-each-pair,array:sort
- Updated: array:putcollation argument was inserted between first and second argument.
- Added: array:put
- Removed: array:serialize(usefn:serializeinstead)
- Added: New module added.