Array Functions
This module provides functions for manipulating arrays. It was introduced with XQuery 3.1 and will be 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 $array with $member attached. |
Examples | Result: [ 'member1' ] |
array:build
Added: New function.
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 $action for each item in the $input sequence. |
Examples | Result: [ 1, 2, 3 ] Result: [ 1, (1, 2), (1, 2, 3) ] Result: [ ('o', 'n', 'e'), ('t', 'w', 'o') ] |
array:empty
Added: New function.
Signature | array:empty( $array as array(*) ) as xs:boolean |
---|---|
Summary | Returns true if $array contains 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 $array for which $predicate returns true . |
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
Updated: Positional argument added to the function parameter. Calculations are skipped once a condition in the supplied function is met.
Signature | array:fold-left( $array as array(*), $zero as item()*, $action as fn(item()*, item()*, xs:integer) as item()* ) as item()* |
---|---|
Summary | Evaluates the $action cumulatively on successive members of $array from left to right, and uses $zero as first argument. |
Examples | Result: 55 . Computes the sum of the integers 1 to 10 . |
array:fold-right
Updated: Positional argument added to the function parameter. Calculations are skipped once a condition in the supplied function is met.
Signature | array:fold-right( $array as array(*), $zero as item()*, $action as fn(item()*, item()*, xs:integer) as item()* ) as item()* |
---|---|
Summary | Evaluates $action cumulatively on successive members of $array from right to left, and uses $zero as first argument. |
Examples | The following query is equivalent to the expression array:reverse(array { 1 to 5 }) . |
array:foot
Added: New function.
Signature | array:foot( $array as array(*) ) as item()* |
---|---|
Summary | Returns the last member of $array . |
Examples | Result: 3 Result: 'c', 'd' |
array:for-each
Updated: Positional argument added to the function parameter.
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 $action to the corresponding member of $array . |
Examples | Result: [ 2, 3, 4, 5, 6] |
array:for-each-pair
Updated: Positional argument added to the function parameter.
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 $action for each pair of members at the same position in $array1 and $array2 . |
Examples | Result: [ 5, 7, 9 ] |
array:get
Signature | array:get( $array as array(*), $position as xs:integer ) as item()* | ||
---|---|---|---|
Summary | Returns the member of $array at the specified $position . | ||
Errors |
| ||
Examples | Result: 1 |
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
Added: New function.
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 $array that match $target . |
Examples | Result: 2, 4 |
array:index-where
Added: New function.
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 $array that match the $predicate function. |
Examples | Result: 3 Result: 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 $array and an additional $member at a specified $position . |
Examples | Result: [ 'a', ('x', 'y'), 'b' ] |
array:join
Signature | array:join( $arrays as array(*)* ) as array(*) |
---|---|
Summary | Concatenates the contents of several $arrays into a single array. |
Examples | Result: [] Result: [ 1, 2, 3 ] Result: [ 'a', 'b', 'c', 'd' ] |
array:put
Signature | array:put( $array as array(*), $position as xs:integer, $member as item()* ) as array(*) | ||
---|---|---|---|
Summary | Returns a copy of $array with $member replaced 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 $array without 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 $array in 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:count when applied to an array always returns 1 . |
Examples | Result: 3 Returns 1 , 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 $array based on their position:
|
Examples | Result: [ 3, 5 ] Result: [ 5, 3 ] Result: [ 1, 3, 5 ] Result: [ 1, 5 ] |
array:sort
Updated: Support for multipe sort key definitions.
Signature | array:sort( $array as array(*), $collation as xs:string? := (), $keys as (fn(item()*) as xs:anyAtomicType*)* := (), $orders as enum('ascending', 'descending')* := 'ascending' ) as array(*) |
---|---|
Summary | Returns a new array with sorted $array members, using an optional $collation . Multipe $collations , $keys and $orders can be supplied, which will be applied on each sort items. The items resulting from the sort keys will be sorted using the semantics of the lt operator. |
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 $array as 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 $length members of $array beginning from the specified $position . The two-argument version of the function returns the same result as the three-argument version when called with $length equal to the value of array: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
Added: New function.
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 11.0- 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:put
collation argument was inserted between first and second argument.
- Added:
array:put
- Removed:
array:serialize
(usefn:serialize
instead)
- Added: New module added.