Expressions are the basic building blocks of a query. Expressions
can be used alone or in combination with other expressions to form
complex queries. DB2® XQuery
supports several kinds of expressions for working with XML data.
Expression evaluation and processing
A number of operations are often included in the processing
of expressions. These operations include extracting atomic values
from nodes, using type promotion and subtype substitution to obtain
values of an expected type, and computing the Boolean value of a sequence.
Primary expressions
Primary expressions are the basic primitives of the language. They
include literals, variable references, parenthesized expressions, context
item expressions, constructors, and function calls.
Path expressions Path expressions identify nodes within an
XML tree. Path expressions in DB2 XQuery
are based on the syntax of XPath 2.0.
Predicates
A predicate filters a sequence by retaining the qualifying
items. A predicate consists of an expression, called a predicate expression,
that is enclosed in square brackets ([]). The predicate expression is evaluated
once for each item in the sequence, with the selected item as the context
item. Each evaluation of the predicate expression returns an xs:boolean value
called the predicate truth value. Those items for which the predicate
truth value is true are retained, and those for which the predicate truth
value is false are discarded.
Sequence expressions
Sequence expressions construct, filter, and combine sequences of
items. Sequences are never nested. For example, combining the values 1, (2,
3), and ( ) into a single sequence results in the sequence (1, 2, 3).
Arithmetic expressions
Arithmetic expressions perform operations that involve
addition, subtraction, multiplication, division, and modulus.
Comparison expressions
Comparison expressions compare two values. XQuery provides three
kinds of comparison expressions: value comparisons, general comparisons, and
node comparisons.
Logical expressions
Logical expressions use the operators and and or to
compute a Boolean value (true or false).
Constructors
Constructors create XML structures within a query. XQuery provides
constructors for creating element nodes, attribute nodes, document nodes,
text nodes, processing instruction nodes, and comment nodes. XQuery provides
two kinds of constructors: direct constructors and computed constructors.
FLWOR expressions
FLWOR expressions iterate over sequences and bind variables to
intermediate results. FLWOR expressions are useful for computing joins between
two or more documents, restructuring data, and sorting the result.
Conditional expressions
Conditional expressions use the keywords if, then,
and else to evaluate one of two expressions based
on whether the value of a test expression is true or false.
Quantified expressions
Quantified expressions return true if some or every item in one
or more sequences satisfies a specific condition. The value of a quantified
expression is always true or false.
Cast expressions
A cast expression creates a new value of a specific type based
on an existing value.
Castable expressions
Castable expressions test whether a value can be cast to a specific
data type. If the value can be cast to the data type, the castable expression
returns true. Otherwise, the expression returns false.