As explained by @Michael Kay, there is no array data structure supported by the XPath data model (XDM) both for XPath 1.0 and XPath 2.0.
However, it is possible to use an array-like syntax like this:
In XPath 1.0/2.0 one can define a variable to contain a specific set of nodes and these can be accessed by their position (in document order), specifying this position in a predicate.
Here is an example:
<xsl:variable name="vTransfers" select="/*/transfer"/>
defines a variable named vTransfers
with value the node-set of all transfer
elements each of which is a child of the top element of the XML document.
Then:
$vTransfers[1]
selects the first element that is contained in $vTransfers
.
$vTransfers[2]
selects the second element that is contained in $vTransfers
, ...
$vTransfers[position() = $k]
selects the node from $vTransfers
whose position, in document order, is equal to the value contained in the variable $k
.
In addition XPath 2.0 supports the concept of sequences. A sequence is like a list of items. An item can be of any type -- not only node. The items in a sequence are ordered in the way they appear (are defined) in the sequence. If two items in a sequence are nodes, their order is still that defined in the sequence and this may be different from their document order.
Example:
<xsl:variable name="vNumList" as="xs:integer*" select="3, 5, 7"/>
then when referenced like this:
$vNumlist[2]
produces:
5
Remember: Although these synthactic constructs resemble selection of an item from an array, node-sets and sequences are not arrays. In particular, they typically lack the very fast access O(1) that an array has to its elements. In the case of node-sets and sequences the efficiency of accessing an item at a random position is typically O(N). This means that an algorithm that is O(N) when using an array, may be O(N^2) when using the array-like notations with node-sets or sequences.