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

Functions and operations on sequences

Since each expression in XQuery results in a sequence, consequently also all functions in XQuery are functions on sequences. In this section, only functions are discussed dealing with the structure of sequences. Here, we distinguish two groups: In the first group the cardinality of sequences is regarded, in the second group sequences are modified.

When representing the syntax, the parameters which stand for the sequences are always named with $seq. The notation item()* stands for a general sequence type. Some functions and operations are only defined for sequences of atomic values or sequences of nodes. Those are introduced in later sections.

Cardinality of sequences

As already mentioned, each XQuery expression returns a sequence. However, the author of an expression often has a clear idea of whether the resulting sequence shall consist of exactly one item, whether it may be empty or not. With the help of the functions fn:zero-or-one(), fn:one-or-more(), fn:exactly-one(), fn:exists() and fn:empty() the desired properties of a sequence can be accordingly ensured. The number of the items of a sequence can be determined with fn:count(). The following table shows the signatures of these functions.

SignatureDescription
fn:zero-or-one(
$seq as item()*)
as item()?
returns the input sequence in case it has at most one item,
otherwise an error is generated
fn:one-or-more(
$seq as item()*)
as item()+
returns the input sequence in case it has at least one item,
otherwise an error is generated
fn:exactly-one(
$seq as item()*)
as item()
returns the input sequence in case it has exaclty one item,
otherwise an error is generated
fn:empty(
$arg as item()*)
as xs:boolean
returns true in case the input sequence is empty, otherwise false
fn:exists(
$arg as item()*)
as xs:boolean
returns true in case the input sequence is not empty,
otherwise false
fn:count(
$seq as item()*)
as xs:integer
returns the number of items of a sequence

Table: functions for querying the cardinality of sequences

Each of the following expressions returns its input argument:

fn:zero-or-one(())
fn:zero-or-one(1)
fn:zero-or-one((1))
fn:exactly-one((1))
fn:one-or-more(1)
fn:one-or-more((1,2))

 

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

<< backnext >>