• +49-(0)721-402485-12
Your experts for XML, XQuery and XML Databases

Changes in sequences

We have already introduced the concatenation of sequences with the comma operator. With this operator, also single items can be added to a sequence:

let $seq := (1, 2, 3)
return ($seq, 4)

returns:

(1, 2, 3, 4)

With the help of the fn:insert-before() function, it is also possible to insert items into other positions of the sequence. The deletion of items can be achived with fn:remove() by indicating the relevant position. In the following example the item at position 3 is replaced by the item "New".

let $seq := ("I1", "I2", "I3", "I4", "I5")
return fn:insert-before(fn:remove($seq, 3), 3, "New")

Result:

("I1", "I2", "New", "I4", "I5")

The order of the items in a sequence can be reversed with fn:reverse(). With fn:subsequence() it is possible to build an excerpt from a sequence by indicating the start position and the number of elements. Here, these indications are values of xs:double type. They are initially rounded in order to get values of xs:integer type. In case the indication of a number is missing, the input sequence is outputted from the start position to the end. For example, the following expression:

let $seq := (6,5,4,3,2,1,0)
return subsequence(($seq, fn:reverse($seq)),
fn:count($seq) div 2,
fn:count($seq))

results in the sequence:

(3,2,1,0,0,1,2)

A special role plays the fn:unordered() function. It only represents the waiver of a specific order. A XQuery implementation may use this for optimisation purposes. In this way, for example, it is not required to restore the document order when uniting sequences with the union operator:

fn:unordered($seq1 union $seq2)

The following table provides an overview of the functions discussed.

SignatureDescription
fn:insert-before(
$seq as item()*,
$position as xs:integer,
$seqnew as item()*)
as item()*
returns a new sequence in which the items of the $seqnew
sequence have been inserted into the $position position in the
$seq sequence; if $position is greater than the number of the
items in $seq, $seqnew is added to $seq; if $position has the value 1 or less, $seqnew is put in front of the items of $seq.

fn:remove(
$seq as item()*,
$position as xs:integer)
as item()*
returns a new sequence which is made up by removing in
$seq the item at position $position;
in case of invalid position indications, $seq is returned.
fn:reverse(
$seq as item()*)
as item()*
returns a new sequence containing all items of $seq
in reversed order.
fn:subsequence(
$seq as item()*,
$start as xs:double[,
$length as xs:double])
as item()*
returns a new sequence beginning with the item at position
round($start) in $seq; the rest of $seq is outputted in case $length is missing, otherwise only round($length) items are returned.
fn:unordered(
$seq as item()*)
as item()*
returns a sequence containing all items of $seq,
whereby their order is undetermined.

 

Source: "XQuery – Grundlagen und fortgeschrittene Methoden", dpunkt-Verlag, Heidelberg (2004)

<< backnext >>