1.15.6 XQuery query body

Organization of this subsection
(1) Function
(2) Format
(3) XQuery sequence concatenation expressions
(4) FLWOR expressions
(5) XQuery quantified expressions
(6) XQuery conditional expressions
(7) XQuery logical expressions
(8) XQuery comparison expression
(9) XQuery range expression
(10) XQuery arithmetic expression
(11) XQuery sequence operation expression
(12) XQuery unary expression
(13) XQuery value expression
(14) XQuery path expression
(15) XQuery primary expressions

(1) Function

The body of an XQuery query specifies an XQuery expression that determines the results of the XQuery query.

(2) Format

XQuery-query-body ::= XQuery-expression

XQuery-expression ::= XQuery-sequence-concatenation-expression

(a) Expressions that can be included in an XQuery expression

XQuery expressions are constructed as combinations of the expressions listed in the following table.

Table 1-65 Expressions that can be included in an XQuery expression

No.ExpressionExplanation
1XQuery sequence concatenation expressionExpression that concatenates XQuery sequences.
2FLWOR expression
  • Evaluates an XQuery expression by examining each separate XQuery item in the specified XQuery sequence, and returns the resulting XQuery sequence.
  • Binds an XQuery variable to the specified XQuery sequence, evaluates an XQuery expression in which that XQuery variable is specified, and returns the resulting XQuery sequence.
3XQuery quantified expressionExpression that determines whether any (some) or all (every) of the XQuery items making up the specified XQuery sequence satisfy the specified criteria.
4XQuery conditional expressionExpression that selects an XQuery expression depending on the result of the specified criteria and then evaluates the selected XQuery expression.
5XQuery logical expressionExpression that performs logical operations (OR, AND).
6XQuery comparison expressionExpression that performs a comparison. Comparisons include value comparisons, general comparisons, and node comparisons.
7XQuery range expressionExpression that generates an XQuery sequence of consecutive integer (xs:int type) atomic values.
8XQuery arithmetic expressionExpression that performs addition, subtraction, multiplication, division, or modulo calculation.
9XQuery sequence operation expressionExpression for obtaining the union, intersection or difference for an XQuery sequence consisting of nodes.
10XQuery unary expressionExpression that performs a unary operation.
11XQuery value expressionExpression that specifies a value.
12XQuery path expressionExpression that traverses an XQuery data model tree and selects specific nodes.
13XQuery primary expressionThe basic components, listed below, that constitute an XQuery path expression:
  • XQuery literal
  • XQuery variable reference
  • Parenthesized XQuery expression
  • Context item expression
  • XQuery function invocation

(3) XQuery sequence concatenation expressions

(a) Function

Concatenates XQuery sequences.

When concatenating multiple XQuery sequences, the XQuery sequences to be concatenated are evaluated from left to right and each XQuery item in the XQuery sequences is added to the end of the resulting XQuery sequence. The resulting concatenated XQuery sequence includes all the XQuery items in the XQuery sequences being concatenated. The number of XQuery items contained in the resulting concatenated XQuery sequence will be equal to the number of XQuery items in the XQuery sequence being concatenated.

(b) Format

XQuery-sequence-concatenation-expression ::= XQuery-single-expression [, XQuery-single-expression]...

XQuery-single-expression ::= {FLWOR-expression| XQuery-quantified-expression| XQuery-conditional-expression| XQuery-logical-expression}

(c) Example

The following is an example of an XQuery sequence concatenation expression.

No.ExampleResultExplanation
11, 2, 3, 4, 5(1, 2, 3, 4, 5)Returns an XQuery sequence that is the concatenation of five XQuery sequences consisting of single atomic values of type xs:int, resulting from the evaluation of the XQuery integer literals from 1 to 5.
2(1, 2, 3), (), (4, 5)(1, 2, 3, 4, 5)Returns an XQuery sequence that is the concatenation of the XQuery sequence consisting of the atomic values 1, 2, and 3 of type xs:int, an empty XQuery sequence, and an XQuery sequence consisting of the atomic values 4 and 5 of type xs:int.
The evaluation results are the same as for the XQuery expression 1, 2, 3, 4, 5.

(4) FLWOR expressions

(a) Function

Binds an XQuery variable to an XQuery sequence, evaluates an XQuery expression containing that XQuery variable, and then returns the resulting XQuery sequence.

The methods for binding an XQuery variable and evaluating the XQuery expression are as follows.

In the W3C standard, the FLWOR expressions include the FOR clause, LET clause, WHERE clause, ORDER BY clause, and RETURN clause, but HiRDB supports only the FOR clause, LET clause, and RETURN clause.

(b) Format

FLWOR-expression ::= {FOR-clause|LET-clause }[{FOR-clause|LET-clause }]...
      RETURN-clause
FOR-clause ::= for $ XQuery-variable-name in XQuery-single-expression
      [, $ XQuery-variable-name in XQuery-single-expression]...
LET-clause ::= let $ XQuery-variable-name : equals-sign XQuery-single-expression
      [, $ XQuery-variable-name : equals-sign XQuery-single-expression]...
RETURN-clause ::= return XQuery-single-expression

(c) Operands

Any combination of one or more FOR or LET clauses can be specified.

If multiple FOR and LET clauses are specified, each clause is treated as nested.

Example:

XQuery expression 1 and XQuery expression 2 below have the same contents:

[Figure]XQuery expression 1

for $XQuery-variable-name-1 in XQuery-single- expression-1
let $XQuery-variable-name-2 := XQuery-single- expression-2
for $XQuery-variable-name-3 in XQuery-single- expression-3
let $XQuery-variable-name-4 := XQuery-single- expression-4
return XQuery-single-expression-5

[Figure]XQuery expression 2

for $XQuery-variable-name-1 in XQuery-single- expression-1
return
 let $XQuery-variable-name-2 := XQuery-single- expression-2
 return
   for $XQuery-variable-name-3 in XQuery-single- expression-3
   return
     let $XQuery-variable-name-4 := XQuery-single- expression-4
       return XQuery-single-expression-5

The evaluation of the RETURN clause is repeated as described below:

  1. If a single XQuery variable is used, the RETURN clause is executed repeatedly to evaluate each XQuery item that makes up the XQuery sequence that is the result of evaluating the XQuery single expression.
  2. If multiple XQuery variables are used, the FOR clauses are treated as nested.
    Example:
    XQuery expression 1 and XQuery expression 2 below have the same contents:
    [Figure]XQuery expression 1

    for $XQuery-variable-name-1 in XQuery-single- expression-1,
       $XQuery-variable-name-2 in XQuery-single- expression-2
    return XQuery-single- expression-3

    [Figure]XQuery expression 2

    for $XQuery-variable-name-1 in XQuery-single- expression-1
    return for $XQuery-variable-name-2 in XQuery-single- expression-2
           return XQuery-single- expression-3

XQuery-variable-name
Specifies the name of the XQuery variable to be associated with an XQuery item in the XQuery sequence resulting from the evaluation of the XQuery single expression.
The XQuery variable specified here in the relevant FLWOR expression will be bound within any XQuery single expression specified as input to any XQuery variable name specified after this XQuery variable name, and within XQuery single expressions in any FOR or LET clauses that are specified after this FOR clause, and then within the XQuery single expression specified in the RETURN clause.
XQuery-single-expression
Specifies the XQuery single expression that will be used to return the XQuery sequence that becomes the input.

Associates the result of the evaluation of XQuery-single-expression with an XQuery variable and then evaluates the RETURN clause.

XQuery-variable-name
Specifies the name of the XQuery variable to be associated with the XQuery sequence resulting from the evaluation of the XQuery single expression.
The XQuery variable specified here in the relevant FLWOR expression will be bound within any XQuery single expression that is specified as input to any XQuery variable name specified after this XQuery variable name, and within the XQuery single expression in any FOR or LET clauses that are specified after this LET clause, and then within the XQuery single expression specified in the RETURN clause.
XQuery-single-expression
Specifies the XQuery single expression that will return the XQuery sequence to be the input.
XQuery-single-expression
Specifies an XQuery single expression that represents the XQuery sequence to be returned.
In a FOR clause, it is evaluated once for each iteration of XQuery items.
If a LET clause is specified without a FOR clause, it is evaluated only once.
The evaluation result of a FLWOR expression is the XQuery sequence that is the concatenation of the results of repeatedly evaluating the XQuery-single-expression specified here.
(d) Example

The following is an example of a FLWOR expression.

Element represented by the XQuery variable book:

<bookinfo book_id="452469630​">
 <title>Relational Databases Explained</title>
 <category>database </category>
 <author>Jeff Jones</author>
 <author>Bob Adams</author>
<price>30</price>
</bookinfo>

FLWOR expression examples and results:
No.ExampleResultExplanation
1for $a in $book/author
return fn:string($a)
("Jeff Jones","Bob Adams")Applies the fn:string function to every author element node that is a child of the bookinfo element node and returns the results expressed as character strings.
2for $i in (10, 20),
$j in (1,2)
return ($i + $j)
(11, 12, 21, 22)Returns the same result as
for $I in (10, 20)
return
for $j in (1, 2)
return ( $i + $j )
3let $a := $book/author
return $a/fn:string()
("Jeff Jones","Bob Adams")Applies the fn:string function to every author element node that is a child of the bookinfo element node and returns the results expressed as character strings.
4for $i in (1,2)
let $a := $book/author[$i]
return fn:string($a)
("Jeff Jones","Bob Adams")Applies the fn:string function to the first and second element nodes of the author element nodes that are children of the bookinfo element node and returns the results expressed as character strings.
5let $a := $book/author
for $i in (1,2)
return fn:string($a[$i])
("Jeff Jones","Bob Adams")Applies the fn:string function to the first and second element nodes of the author element nodes that are children of the bookinfo element node and returns the results expressed as character strings.
6for $a in $book/author
return fn:count($a)
(1,1)Returns the XQuery sequence that is the concatenation of the results of applying the fn:count function to every author element node that is a child of the bookinfo element node.
7let $a := $book/author
return fn:count($a)
(2)Returns the result of applying the fn:count function to the XQuery sequence consisting of the author element nodes that are the children of the bookinfo element node.

(5) XQuery quantified expressions

(a) Function

Determines whether any (some) or all (every) of the XQuery items making up the specified XQuery sequence satisfy the specified criteria, and returns the result.

(b) Format

XQuery-quantified-expression::= {some|every} $ XQuery-variable-name in XQuery-single-expression
  [, $  variable name in XQuery-single-expression ]... satisfies XQuery-single-expression

(c) Operands
some
If some is specified, and if the condition specified in the satisfies clause is TRUE for at least one of the XQuery items in the XQuery sequence generated by the in clause, the result will be TRUE. Otherwise, the result will be FALSE. If the XQuery sequence generated by the in clause is an empty XQuery sequence, the result of the XQuery quantified expression will be FALSE.
every
If every is specified, and if the condition specified in the satisfies clause is TRUE for all of the XQuery items in the XQuery sequence generated by the in clause, the result will be TRUE. Otherwise, the result will be FALSE. If the XQuery sequence generated by the in clause is an empty XQuery sequence, the result of the XQuery quantified expression will be TRUE.
XQuery-variable-name
Specifies the name of an XQuery variable to be associated with the XQuery item in the XQuery sequence resulting from the evaluation of XQuery-single-expression.
The XQuery variable specified here in the relevant XQuery quantified expression will be bound within any XQuery single expression specified as input to any XQuery variable name specified after this XQuery variable name, and within the XQuery single expression specified in the satisfies clause.
in XQuery-single-expression
Specifies the XQuery single expression specifying the XQuery sequence that will be used as the input.
satisfies XQuery-single-expression
Specifies the XQuery single expression specifying the condition to be evaluated for the XQuery variable. If multiple in clauses are specified, XQuery-single-expression is used to evaluate multiple XQuery variables.
(d) Example

The following is an example of an XQuery quantified expression.

Element represented by the XQuery variable book:

<bookinfo book_id="452469630​">
 <title>Relational Databases Explained</title>
 <category>database </category>
 <author>Jeff Jones</author>
 <author>Bob Adams</author>
<price>30</price>
</bookinfo>

XQuery quantified expression examples and results:

No.ExampleResultExplanation
1some $text in $book /author/text() satisfies ( $text eq "Bob Adams" )TRUETRUE, because a text node of an author element node which is a child of the bookinfo element node has contents equal to Bob Adams.
2some $i in (1, 2, 3), $j in (4, 5, 6) satisfies $i + $j >= 6TRUEResult of this XQuery quantified expression is TRUE because some was specified and there are combinations of XQuery items (example: $i = 1, $j = 5) such that $i + $j >= 6.
3every $i in (1, 2, 3), $j in (4, 5, 6) satisfies $i + $j >= 6FALSEResult of this XQuery quantified expression is FALSE because every was specified and there are combinations of XQuery items (example: $i = 1, $j = 4) that do not satisfy $i + $j >= 6.

(6) XQuery conditional expressions

(a) Function

Selects an XQuery expression depending on the result of the specified criteria and then returns the result of evaluating the selected XQuery expression.

(b) Format

XQuery-conditional-expression ::= if ( XQuery-expression ) then XQuery-single-expression else XQuery-single-expression

(c) Operands
XQuery-expression
Specifies an XQuery expression to evaluate as a condition. If the result of evaluating the XQuery expression specified here is TRUE, the XQuery single expression specified immediately after the then clause is evaluated. If it is FALSE, the XQuery single expression specified immediately after the else clause is evaluated. However, if the result of evaluating the XQuery expression specified here is not a single XQuery sequence containing an atomic value of the xs:boolean type, it is evaluated after converting it to a single XQuery sequence containing an atomic value of the xs:boolean type.
XQuery-single-expression
Specifies the XQuery single expression to evaluate if the condition specified in the if clause is TRUE.
XQuery-single-expression
Specifies the XQuery single expression to evaluate if the condition specified in the if clause is FALSE.
(d) Example

The following is an example of an XQuery conditional expression.

XML element represented by the XQuery variable book1:

<bookinfo book_id="452469630​">
 <title>Relational Databases Explained</title>
 <category>database </category>
 <author>Jeff Jones</author>
 <author>Bob Adams</author>
 <price>30</price>
</bookinfo>

XML element represented by the XQuery variable book2:

<bookinfo book_id="452469631​">
 <title> Introduction to XML</title>
 <author>Sue Smith</author>
 <price>25</price>
</bookinfo>

XQuery conditional expression examples and results:

No.ExampleResultExplanation
1if ($book1/price > $book2/price)
then $book1 else $book2
The element node indicated in the XQuery variable book1 specified in the then clauseBecause the typed-value (=30.00) of the price element node that is the child of the element node indicated in $book1 is greater than the typed-value (=25.00) of the price element node that is the child of the element node indicated in $book2, the element node indicated in $book1 is returned.

(7) XQuery logical expressions

(a) Function

Returns the result of performing a logical operation (OR, AND) on XQuery comparison expressions.

Under certain conditions, the evaluation result from the XQuery comparison expression is returned unchanged.

(b) Format

XQuery-logical-expression ::= XQuery-OR-expression
XQuery-OR-expression ::= XQuery-AND-expression [or XQuery-AND-expression]...
XQuery-AND-expression ::= XQuery-comparison-expression [and XQuery-comparison-expression]...

(c) Operands

If the or operator is specified, the OR logical operation is performed on the results of evaluating XQuery AND expressions.

If you specify the XQuery AND expression by itself, the evaluation result from that XQuery AND expression is returned unchanged.

When the OR logical operation is performed, if the result of evaluating each XQuery AND expression is not a single XQuery sequence with a Boolean value (atomic value of the xs:boolean type), the OR logical operation is performed after implicitly converting the expression to a single XQuery sequence with a Boolean value (atomic value of the xs:boolean type).

The following table indicates the results of the OR operation based on the Boolean values of the first operand and second operand.

Table 1-66 Result of the OR logical operation in an XQuery logical expression

No.First operandSecond operand
TRUEFALSE
1TRUETRUETRUE
2FALSETRUEFALSE

If the and operator is specified, the AND logical operation is performed on the results of evaluating XQuery comparison expressions.

If you specify the XQuery comparison expression by itself, the evaluation result from that XQuery comparison expression is returned unchanged.

When the AND logical operation is performed, if the result of evaluating each XQuery comparison expression is not a single XQuery sequence with a Boolean value (atomic value of the xs:boolean type), the AND logical operation is performed after implicitly converting the expression to a single XQuery sequence with a Boolean value (atomic value of the xs:boolean type).

The following table indicates the results of the AND operation based on the Boolean values of the first operand and second operand.

Table 1-67 Result of the AND logical operation in an XQuery logical expression

No.First operandSecond operand
TRUEFALSE
1TRUETRUEFALSE
2FALSEFALSEFALSE

(8) XQuery comparison expression

(a) Function

Returns the result of a comparison to an XQuery range expression.

Under certain conditions, the evaluation result from the XQuery range expression is returned unchanged.

(b) Format

XQuery-comparison-expression ::= XQuery-range-expression [{value-comparison| general-comparison| node-comparison} XQuery-range-expression]

value-comparison ::= {eq| ne| lt| le| gt| ge}
general-comparison ::= {=| !=| <| <=| >| >=| <>}
node-comparison ::= {is| <<| >>}

(c) Operands

If you specify a value comparison, general comparison or node comparison, the results of evaluating the XQuery range expressions are returned.

If you specify the XQuery range expression by itself, the evaluation result from that XQuery range expression is returned unchanged.

Compares single atomic values to each other.

The following table describes each of the value comparison functions.

Table 1-68 Value comparison methods

No.Value comparison operatorConditions which evaluate to TRUE
1eqThe values of the first comparison term and the second comparison term are equal.
2neThe values of the first comparison term and the second comparison term are not equal.
3ltThe value of the first comparison term is less than the value of the second comparison term.
4leThe value of the first comparison term is less than or equal to the value of the second comparison term.
5gtThe value of the first comparison term is greater than the value of the second comparison term.
6geThe value of the first comparison term is greater than or equal to the value of the second comparison term.

Comparisons are performed in the following order:

  1. Each comparison term is atomized.
  2. If the result of atomizing either comparison term is an empty XQuery sequence, the result of the comparison will be an empty XQuery sequence.
  3. If the result of atomizing either comparison term is an XQuery sequence of two or more XQuery items, an error results.
  4. If the result of atomizing a term is an xs:untypedAtomic type, it is converted to xs:string type before being compared.
  5. When comparing xs:string type values, if the lengths of the data to compare are different, the length of the shorter item determines how many characters will be compared. The characters are compared, starting from the left. If the results of comparing characters are equal, the lengths of the character strings are compared. The comparison results in the longer xs:string type value being considered to have the greater value.
  6. When comparing numeric data, if the XQuery data types to be evaluated are different, the comparison is performed using the XQuery data type with the greater range. The relative ranges of the numeric data types are as follows:
    xs:double > xs:decimal > xs:int
  7. When comparing xs:hexBinary types, if the lengths of the data to compare are different, the length of the shorter item determines how many characters will be compared. The characters are compared, starting from the left. If the results are equal, the lengths of the character strings are compared. The comparison results in the longer xs:hexBinary type value being considered to have the greater value.

An error results if the XQuery data types of the comparison terms cannot be compared. The following table indicates which combinations of XQuery data types can be compared.

Table 1-69 Combinations of XQuery data types that can be compared

XQuery data type of the first operandXQuery data type of the second operand
xs:stringNumeric data typexs:dateTimexs:datexs:timexs:hexBinarys:boolean
xs:stringYNNNNNN
Numeric data typeNYNNNNN
xs:dateTimeNNYNNNN
xs:dateNNNYNNN
xs:timeNNNNYNN
xs:hexBinaryNNNNNY#N
xs:booleanNNNNNNY#
Legend:
Y: Can compare.
N: Cannot compare.
#
Can only be compared using the comparison operators eq or ne, and not the other comparison operators.

The following tables list the results of comparing xs:double types with each operator.

Table 1-70 Results of comparing xs:double types (eq operator)

No.First operand
(value converted to xs:double type)
Second operand (value converted to xs:double type)
+0-0+INF-INFNaNOther negative numbersAll others
1+0TRUETRUEFALSEFALSEFALSEFALSEFALSE
2-0TRUETRUEFALSEFALSEFALSEFALSEFALSE
3+INFFALSEFALSETRUEFALSEFALSEFALSEFALSE
4-INFFALSEFALSEFALSETRUEFALSEFALSEFALSE
5NaNFALSEFALSEFALSEFALSEFALSEFALSEFALSE
6Other negative numbersFALSEFALSEFALSEFALSEFALSEComparison resultComparison result
7All othersFALSEFALSEFALSEFALSEFALSEComparison resultComparison result
Legend:
+0: Positive 0
-0: Negative 0
+INF: Positive infinity
-INF: Negative infinity
NaN: Not a number
Comparison result: Result of the comparison is determined according to Table 1-68.

Table 1-71 Results of comparing xs:double types (ne operator)

No.First operand
(value converted to xs:double type)
Second operand (value converted to xs:double type)
+0-0+INF-INFNaNOther negative numbersAll others
1+0FALSEFALSETRUETRUETRUETRUETRUE
2-0FALSEFALSETRUETRUETRUETRUETRUE
3+INFTRUETRUEFALSETRUETRUETRUETRUE
4-INFTRUETRUETRUEFALSETRUETRUETRUE
5NaNTRUETRUETRUETRUETRUETRUETRUE
6Other negative numbersTRUETRUETRUETRUETRUEComparison resultComparison result
7All othersTRUETRUETRUETRUETRUEComparison resultComparison result
Legend:
+0: Positive 0
-0: Negative 0
+INF: Positive infinity
-INF: Negative infinity
NaN: Not a number
Comparison result: Result of the comparison is determined according to Table 1-68.

Table 1-72 Results of comparing xs:double types (lt operator)

No.First operand
(value converted to xs:double type)
Second operand (value converted to xs:double type)
+0-0+INF-INFNaNOther negative numbersAll others
1+0FALSEFALSETRUEFALSEFALSEFALSETRUE
2-0FALSEFALSETRUEFALSEFALSEFALSETRUE
3+INFFALSEFALSEFALSEFALSEFALSEFALSEFALSE
4-INFTRUETRUETRUEFALSEFALSETRUETRUE
5NaNFALSEFALSEFALSEFALSEFALSEFALSEFALSE
6Other negative numbersTRUETRUETRUEFALSEFALSEComparison resultComparison result
7All othersFALSEFALSETRUEFALSEFALSEComparison resultComparison result
Legend:
+0: Positive 0
-0: Negative 0
+INF: Positive infinity
-INF: Negative infinity
NaN: Not a number
Comparison result: Result of the comparison is determined according to Table 1-68.

Table 1-73 Results of comparing xs:double types (le operator)

No.First operand
(value converted to xs:double type)
Second operand (value converted to xs:double type)
+0-0+INF-INFNaNOther negative numbersAll others
1+0TRUETRUETRUEFALSEFALSEFALSETRUE
2-0TRUETRUETRUEFALSEFALSEFALSETRUE
3+INFFALSEFALSETRUEFALSEFALSEFALSEFALSE
4-INFTRUETRUETRUETRUEFALSETRUETRUE
5NaNFALSEFALSEFALSEFALSEFALSEFALSEFALSE
6Other negative numbersTRUETRUETRUEFALSEFALSEComparison resultComparison result
7All othersFALSEFALSETRUEFALSEFALSEComparison resultComparison result
Legend:
+0: Positive 0
-0: Negative 0
+INF: Positive infinity
-INF: Negative infinity
NaN: Not a number
Comparison result: Result of the comparison is determined according to Table 1-68.

Table 1-74 Results of comparing xs:double types (gt operator)

No.First operand
(value converted to xs:double type)
Second operand (value converted to xs:double type)
+0-0+INF-INFNaNOther negative numbersAll others
1+0FALSEFALSEFALSETRUEFALSETRUEFALSE
2-0FALSEFALSEFALSETRUEFALSETRUEFALSE
3+INFTRUETRUEFALSETRUEFALSETRUETRUE
4-INFFALSEFALSEFALSEFALSEFALSEFALSEFALSE
5NaNFALSEFALSEFALSEFALSEFALSEFALSEFALSE
6Other negative numbersFALSEFALSEFALSETRUEFALSEComparison resultComparison result
7All othersTRUETRUEFALSETRUEFALSEComparison resultComparison result
Legend:
+0: Positive 0
-0: Negative 0
+INF: Positive infinity
-INF: Negative infinity
NaN: Not a number
Comparison result: Result of the comparison is determined according to Table 1-68.

Table 1-75 Results of comparing xs:double types (ge operator)

No.First operand
(value converted to xs:double type)
Second operand (value converted to xs:double type)
+0-0+INF-INFNaNOther negative numbersAll others
1+0TRUETRUEFALSETRUEFALSETRUEFALSE
2-0TRUETRUEFALSETRUEFALSETRUEFALSE
3+INFTRUETRUETRUETRUEFALSETRUETRUE
4-INFFALSEFALSEFALSETRUEFALSEFALSEFALSE
5NaNFALSEFALSEFALSEFALSEFALSEFALSEFALSE
6Other negative numbersFALSEFALSEFALSETRUEFALSEComparison resultComparison result
7All othersTRUETRUEFALSETRUEFALSEComparison resultComparison result
Legend:
+0: Positive 0
-0: Negative 0
+INF: Positive infinity
-INF: Negative infinity
NaN: Not a number
Comparison result: Result of the comparison is determined according to Table 1-68.

Compares XQuery sequences of any length to each other.

Comparisons are performed in the following sequence:

  1. Each comparison term is atomized.
  2. In the combination of atomic values for each atomized comparison term from among the XQuery sequences, if there is any set of atomic values for which the value comparison performed as shown below results in a TRUE evaluation, the result will be TRUE. If there is no such set, the result will be FALSE.
    • For two atomic values, the value comparison corresponding to the specified general comparison is performed as shown in Table 1-76. However, if the atomic value is xs:untypedAtomic type, the value comparison is performed after the type conversion shown in Table 1-77 is performed (determined by the XQuery data type of the other atomic value).

      Table 1-76 General comparisons and their corresponding value comparisons

      No.General comparison operatorCorresponding value comparison
      1=eq
      2!=, <>ne
      3<lt
      4<=le
      5>gt
      6>=ge

      Table 1-77 Type conversion of atomic values of the xs:untypedAtomic type

      No.XQuery data type of the other atomic valueXQuery data type after type conversion
      1Numeric data typexs:double
      2xs:untypedAtomicxs:string
      3xs:stringxs:string
      4Other than the aboveSame XQuery data type as the other atomic value

Compares nodes to each other based on node ID and document order.

The node ID, which is bound to all nodes in the tree representation of the XQuery data model, acts as a unique identifier within the tree.

The following table describes the ways nodes can be compared.

Table 1-78 Node comparisons

No.Node comparison operatorConditions under which TRUE
1isThe node IDs of the two nodes being compared are the same.
2<<The first node being compared appears before the second node being compared in the document order.
3>>The first node being compared appears after the second node being compared in the document order.

Comparisons are performed according to the following procedures:

  1. Each comparison term is either a single node XQuery sequence or an empty XQuery sequence.
  2. If either is an empty XQuery sequence, the result will be an empty XQuery sequence.
  3. If both are single node XQuery sequences, compare the nodes that make up each comparison term XQuery sequence.
  4. If the nodes specified for each comparison term belong to different trees in the XQuery data model, the node specified as the first comparison term will be first in the ordering.
(d) Example

The following is an example of an XQuery comparison expression.

Element represented by the XQuery variable book:

<bookinfo book_id="452469630​">
 <title>Relational Databases Explained</title>
 <category>database </category>
 <author>Jeff Jones</author>
 <author>Bob Adams</author>
 <price>3000</price>
</bookinfo>

XQuery comparison expression examples and results:

No.ExampleResultExplanation
1$book[@book_id
eq "452469630​" ]
bookinfo element nodeBecause the typed-value of the book_id attribute node of the bookinfo element node in the XQuery variable book equals 452469630​, the result is the bookinfo element node from the XQuery variable book.
2(1, 2) = (2, 3)TRUEThe result is TRUE because the eq relation holds between the second XQuery item in the first comparison term and the first XQuery item in the second comparison term.
3(1, 2) != (2, 3)TRUEThe result is TRUE because the ne relation holds between the first XQuery item in the first comparison term and the first XQuery item in the second comparison term.

(9) XQuery range expression

(a) Function

Returns an XQuery sequence consisting of atomic values that are consecutive integer values (xs:int type).

Under certain conditions, the evaluation result from the XQuery arithmetic expression is returned unchanged.

(b) Format

XQuery-range-expression ::= XQuery-arithmetic-expression [to XQuery-arithmetic-expression]

(c) Operands

If to is specified, compares the results of evaluating the XQuery arithmetic expressions.

If you specify the XQuery arithmetic expression by itself, the evaluation result from that XQuery arithmetic expression is returned unchanged.

If to is specified, the XQuery sequence generated according to the following rules is returned.

  1. Atomize the results of evaluating each XQuery arithmetic expression.
  2. The atomization result must be an empty XQuery sequence or a single XQuery sequence consisting of a single atomic value that can be converted into xs:int type.
  3. If any of the results of evaluating the XQuery arithmetic expression is an empty XQuery sequence, the result will be an empty XQuery sequence.
  4. If the integer value resulting from converting the atomic value of the result of evaluating the first XQuery arithmetic expression into xs:int type is greater than the integer value resulting from converting the atomic value of the result of evaluating the second XQuery arithmetic expression into xs:int type, the result will be an empty XQuery sequence.
  5. If neither rule 2 nor 3 above is applicable, the result is the XQuery sequence consisting of all integer values between (greater than or equal to the first integer, less than or equal to the second integer) the two integer values resulting from converting the atomic value of the results of evaluating the XQuery arithmetic expressions into xs:int type.
(d) Example

The following example shows an XQuery range expression.

No.ExampleResultExplanation
1( 1 to 10 )(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)Generates the XQuery sequence consisting of integer values from 1 to 10.

(10) XQuery arithmetic expression

(a) Function

Returns the result of addition, subtraction, multiplication, division, or modulo calculations.

Under certain conditions, the evaluation result from the XQuery sequence operation expression is returned unchanged.

(b) Format

XQuery-arithmetic-expression ::= XQuery-addition-or-subtraction-expression
XQuery-addition-or-subtraction-expression ::= XQuery-multiplication-or-division-expression [{+| -} XQuery-multiplication-or-division-expression]...
XQuery-multiplication-or-division-expression ::= XQuery-sequence-operation-expression
   [{*| div| idiv| mod} XQuery-sequence-operation-expression]...

(c) Operands

If an operator (+, -) is specified, the results of evaluating the XQuery multiplication or division expression are added or subtracted.

If you specify the XQuery multiplication or division expression by itself, the evaluation result from that XQuery multiplication or division expression is returned unchanged.

The following table describes the meaning and function of each operator.

Table 1-79 Functions of operators in XQuery addition or subtraction expressions

No.OperatorMeaningFunction
1+AdditionAdd the second operand to the first operand.
2-SubtractionSubtract the second operand from the first operand.

If an operator (*, div, idiv, mod) is specified, multiplication, division, or modulo calculations are performed on the results of evaluating the XQuery sequence operation expressions.

If you specify the XQuery sequence operation expression by itself, the evaluation result from that XQuery sequence operation expression is returned unchanged.

The following table describes the meaning and function of each operator.

Table 1-80 Functions of operators in XQuery multiplication or division expressions

No.OperatorMeaningFunction
1*MultiplicationMultiply the first operand by the second operand.
2divDivisionDivide the first operand by the second operand.
3idivInteger divisionTruncate the result of dividing the first operand by the second operand to an integer value.
4modModuloCalculate the remainder when the first operand a is divided by the second operand b. The result of this operation is a - (a idiv b)* b.
(d) Evaluation of XQuery arithmetic expressions

Addition, subtraction, multiplication, division, and modulo calculations are performed on XQuery arithmetic expressions in the following sequence:

  1. Each operand is atomized.
  2. If any atomized operand is an empty XQuery sequence, the result will be an empty XQuery sequence.
  3. An error results if any atomized operand is an XQuery sequence with two or more atomic values.
  4. If the XQuery data type of the atomized operand is the xs:untypedAtomic type, it is converted to xs:double type. An error results if this conversion is not possible.
  5. Operations are performed on the two XQuery sequences obtained according to the steps above. The following tables list the XQuery data type of the result based on the XQuery data type of each operator.

    Table 1-81 Relationship between the XQuery data types of the operands and the XQuery data type of the result (+ operator)

    No.First operandSecond operand
    xs:intxs:decimalxs:double
    1xs:intxs:intxs:decimalxs:double
    2xs:decimalxs:decimalxs:decimalxs:double
    3xs:doublexs:doublexs:doublexs:double

    Table 1-82 Relationship between the XQuery data types of the operands and the XQuery data type of the result (- operator)

    No.First operandSecond operand
    xs:intxs:decimalxs:double
    1xs:intxs:intxs:decimalxs:double
    2xs:decimalxs:decimalxs:decimalxs:double
    3xs:doublexs:doublexs:doublexs:double

    Table 1-83 Relationship between the XQuery data types of the operands and the XQuery data type of the result (* operator)

    No.First operandSecond operand
    xs:intxs:decimalxs:double
    1xs:intxs:intxs:decimalxs:double
    2xs:decimalxs:decimalxs:decimalxs:double
    3xs:doublexs:doublexs:doublexs:double

    Table 1-84 Relationship between the XQuery data types of the operands and the XQuery data type of the result (div operator)

    No.First operandSecond operand
    xs:intxs:decimalxs:double
    1xs:intxs:decimalxs:decimalxs:double
    2xs:decimalxs:decimalxs:decimalxs:double
    3xs:doublexs:doublexs:doublexs:double

    Table 1-85 Relationship between the XQuery data types of the operands and the XQuery data type of the result (idiv operator)

    No.First operandSecond operand
    xs:intxs:decimalxs:double
    1xs:intxs:intxs:intxs:int
    2xs:decimalxs:intxs:intxs:int
    3xs:doublexs:intxs:intxs:int

    Table 1-86 Relationship between the XQuery data types of the operands and the XQuery data type of the result (mod operator)

    No.First operandSecond operand
    xs:intxs:decimalxs:double
    1xs:intxs:intxs:decimalxs:double
    2xs:decimalxs:decimalxs:decimalxs:double
    3xs:doublexs:doublexs:doublexs:double
  6. An error results if an operand meets either of the following conditions:
    • If the operands are not both xs:double type, and 0 is specified as the second operand of the div or mod operations.
    • If 0 is specified as the second operand of the idiv operation.
  7. The following tables list the results for each operator when at least one of the operands is xs:double type.

    Table 1-87 Result when at least one of the operands is xs:double type (+ operator)

    No.First operand
    (value converted to xs:double type)
    Second operand (value converted to xs:double type)
    +0-0+INF-INFNaNOther negative numbersAll others
    1+0+0+0+INF-INFNaNResult of additionResult of addition
    2-0+0-0+INF-INFNaNResult of additionResult of addition
    3+INF+INF+INF+INFNaNNaN+INF+INF
    4-INF-INF-INFNaN-INFNaN-INF-INF
    5NaNNaNNaNNaNNaNNaNNaNNaN
    6Other negative numbersResult of additionResult of addition+INF-INFNaNResult of additionResult of addition
    7All othersResult of additionResult of addition+INF-INFNaNResult of additionResult of addition
    Legend:
    +0: Positive 0
    -0: Negative 0
    +INF: Positive infinity
    -INF: Negative infinity
    NaN: Not a number
    Result of addition: The result of the operation is determined according to the functions shown in Table 1-79.

    Table 1-88 Result when at least one of the operands is xs:double type (- operator)

    No.First operand
    (value converted to xs:double type)
    Second operand (value converted to xs:double type)
    +0-0+INF-INFNaNOther negative numbersAll others
    1+0+0+0-INF+INFNaNResult of subtractionResult of subtraction
    2-0-0+0-INF+INFNaNResult of subtractionResult of subtraction
    3+INF+INF+INFNaN+INFNaN+INF+INF
    4-INF-INF-INF-INFNaNNaN-INF-INF
    5NaNNaNNaNNaNNaNNaNNaNNaN
    6Other negative numbersResult of subtractionResult of subtraction-INF+INFNaNResult of subtractionResult of subtraction
    7All othersResult of subtractionResult of subtraction-INF+INFNaNResult of subtractionResult of subtraction
    Legend:
    +0: Positive 0
    -0: Negative 0
    +INF: Positive infinity
    -INF: Negative infinity
    NaN: Not a number
    Result of subtraction: Result of the operation is determined according to the functions shown in Table 1-79.

    Table 1-89 Result when at least one of the operands is xs:double type (* operator)

    No.First operand
    (value converted to xs:double type)
    Second operand (value converted to xs:double type)
    +0-0+INF-INFNaNOther negative numbersAll others
    1+0+0-0NaNNaNNaNResult of multiplicationResult of multiplication
    2-0-0+0NaNNaNNaNResult of multiplicationResult of multiplication
    3+INFNaNNaN+INF-INFNaN-INF+INF
    4-INFNaNNaN-INF+INFNaN+INF-INF
    5NaNNaNNaNNaNNaNNaNNaNNaN
    6Other negative numbersResult of multiplicationResult of multiplication-INF+INFNaNResult of multiplicationResult of multiplication
    7All othersResult of multiplicationResult of multiplication+INF-INFNaNResult of multiplicationResult of multiplication
    Legend:
    +0: Positive 0
    -0: Negative 0
    +INF: Positive infinity
    -INF: Negative infinity
    NaN: Not a number
    Result of multiplication: Result of the operation is determined according to the functions shown in Table 1-80.

    Table 1-90 Result when at least one of the operands is xs:double type (div operator)

    No.First operand
    (value converted to xs:double type)
    Second operand (value converted to xs:double type)
    +0-0+INF-INFNaNOther negative numbersAll others
    1+0NaNNaN+0-0NaNResult of divisionResult of division
    2-0NaNNaN-0+0NaNResult of divisionResult of division
    3+INF+INF-INFNaNNaNNaN-INF+INF
    4-INF-INF+INFNaNNaNNaN+INF-INF
    5NaNNaNNaNNaNNaNNaNNaNNaN
    6Other negative numbers-INFINF-0+0NaNResult of divisionResult of division
    7All othersINF-INF+0-0NaNResult of divisionResult of division
    Legend:
    +0: Positive 0
    -0: Negative 0
    +INF: Positive infinity
    -INF: Negative infinity
    NaN: Not a number
    Result of division: Result of the operation is determined according to the functions shown in Table 1-80.

    Table 1-91 Result when at least one of the operands is xs:double type (idiv operator)

    No.First operand
    (value converted to xs:double type)
    Second operand (value converted to xs:double type)
    +0-0+INF-INFNaNOther negative numbersAll others
    1+0Divide by zero errorDivide by zero error00Type conversion errorResult of integer divisionResult of integer division
    2-0Divide by zero errorDivide by zero error00Type conversion errorResult of integer divisionResult of integer division
    3+INFDivide by zero errorDivide by zero errorType conversion errorType conversion errorType conversion errorType conversion errorType conversion error
    4-INFDivide by zero errorDivide by zero errorType conversion errorType conversion errorType conversion errorType conversion errorType conversion error
    5NaNDivide by zero errorDivide by zero errorType conversion errorType conversion errorType conversion errorType conversion errorType conversion error
    6Other negative numbersDivide by zero errorDivide by zero error00Type conversion errorResult of integer divisionResult of integer division
    7All othersDivide by zero errorDivide by zero error00Type conversion errorResult of integer divisionResult of integer division
    Legend:
    0: xs:int type 0
    Result of integer division: Result of the operation is determined according to the functions shown in Table 1-80.
    Divide by zero error: Error because the divisor is 0.
    Type conversion error: Error because the result of the div operation cannot be represented as xs:int type.

    Table 1-92 Result when at least one of the operands is xs:double type (mod operator)

    No.First operand
    (value converted to xs:double type)
    Second operand (value converted to xs:double type)
    +0-0+INF-INFNaNOther negative numbersAll others
    1+0NaNNaN+0+0NaNResult of modulo operationResult of modulo operation
    2-0NaNNaN-0-0NaNResult of modulo operationResult of modulo operation
    3+INFNaNNaNNaNNaNNaNNaNNaN
    4-INFNaNNaNNaNNaNNaNNaNNaN
    5NaNNaNNaNNaNNaNNaNNaNNaN
    6Other negative numbersNaNNaNFirst operand (value converted to xs:double type)First operand (value converted to xs:double type)NaNResult of modulo operationResult of modulo operation
    7All othersNaNNaNFirst operand (value converted to xs:double type)First operand (value converted to xs:double type)NaNResult of modulo operationResult of modulo operation
    Legend:
    +0: Positive 0
    -0: Negative 0
    +INF: Positive infinity
    -INF: Negative infinity
    NaN: Not a number
    First operand: Value of first operand
    Result of modulo operation: Result of the operation is determined according to the functions shown in Table 1-80.
(e) Notes

The subtraction operator (-) must be preceded by either a space (X'20'), TAB (X'09'), NL (X'0a') or CR (X'0d'). Without one of these characters, it is considered part of the name specified before the subtraction operator. For example, a-b is interpreted as a name, whereas a - b and a -b are interpreted as XQuery arithmetic expressions.

(f) Example

The following are examples of XQuery arithmetic expressions.

No.ExampleResult (XQuery data type)
14 - 22 (xs:int)
25 div 22.5 (xs:decimal)
35 idiv 22 (xs:int)
45 mod 21 (xs:int)

(11) XQuery sequence operation expression

(a) Function

Treats XQuery sequences consisting of nodes as ordered sets and returns the XQuery sequence that results from performing the union, intersection, or difference operations.

Under certain conditions, the evaluation result from the XQuery unary expression is returned unchanged.

(b) Format

XQuery-sequence-operation-expression ::= XQuery-union-expression
XQuery-union-expression ::= {XQuery-intersection-expression| XQuery-difference-expression}
  [{union| vertical-bar} {XQuery-intersection-expression| XQuery-difference-expression}]...
XQuery-intersection-expression ::= XQuery-unary-expression [intersect XQuery-unary-expression]...
XQuery-difference-expression ::= XQuery-unary-expression [except XQuery-unary-expression]...

(c) Operands

If union or a vertical bar (|) is specified, this derives an XQuery sequence consisting of all the different nodes that are contained in the two XQuery sequences that are the results of evaluating the XQuery intersection expressions or XQuery difference expressions.

If you specify the XQuery intersection expression or the XQuery difference expression by itself, the evaluation result from that XQuery intersection expression or XQuery difference expression is returned unchanged.

If intersect is specified, this derives an XQuery sequence consisting of all the different nodes that are contained in both of the two XQuery sequences that are the results of evaluating the XQuery unary expressions.

If you specify the XQuery unary expression by itself, the evaluation result from that XQuery unary expression is returned unchanged.

If except is specified, this derives an XQuery sequence consisting of all the different nodes that are contained in the XQuery sequence that is the result of evaluating the first XQuery unary expression, but that are not contained in the XQuery sequence that is the result of evaluating the second XQuery unary expression.

If you specify the XQuery unary expression by itself, the evaluation result from that XQuery unary expression is returned unchanged.

(d) Rules
  1. Duplicate nodes are removed from the result XQuery sequence. Also, the order of the nodes in the result XQuery sequence will be their order in the document.
  2. In the result XQuery sequence, if there are nodes that belongs to different trees, the order of the tree is determined according to the rules below. Within any given tree, the order of nodes in the result is determined by their order in the document. Here, let (T1-1, T1-2, ..., T1-n) be the trees to which each node in the first operand's XQuery sequence belong, and let (T2-1, T2-2, ..., T2-m) be the trees to which each node in the second operand's XQuery sequence belong.
    • The order of trees T1-1 to T1-n will be order in which they appeared in the first operand's XQuery sequence.
    • Of the trees T2-1 to T2-m, that are different than any trees T1-1 to T1-n, they will be in the order in which the trees first appeared in the second operand's XQuery sequence. Also, they will follow any trees T1-1 to T1-n in the final order.
(e) Note

Note that the order of evaluation is different for the XQuery sequence operation expressions (union, intersect, and except) and the set operations (UNION and EXCEPT) in the body of an SQL query.

(f) Example

The following are examples of XQuery sequence operation expressions.

XQuery sequence operation expression examples and results:

No.ExampleResult
1(A,B) union (A,B)(A, B)
2(A,B) union (B,C)(A, B, C)
3(A,B) intersect (A,B)(A, B)
4(A,B) intersect (B,C)(B)
5(A,B) except (A,B)()
6(A,B) except (B,C)(A)
Note
A, B, and C represent element nodes in the same XML document.
Document order is bound to the order A, B, C.

(12) XQuery unary expression

(a) Function

Returns the result of performing a unary operation on an XQuery value expression.

Under certain conditions, the evaluation result from the XQuery value expression is returned unchanged.

(b) Format

XQuery-unary-expression ::= [{-| +}]... XQuery-value-expression

(c) Operands

If a unary operator (-, +) is specified, the result of performing the unary operation on the XQuery value expression is returned.

If you specify the XQuery value expression by itself, the evaluation result from that XQuery value expression is returned unchanged.

The following table describes the meaning and function of each operator.

Table 1-93 Functions of operators in XQuery unary expressions

No.OperatorMeaningFunction
1-Minus signReverses the sign.
2+Plus signDoes not reverse the sign.
(d) Evaluation of XQuery unary expressions

In XQuery unary expressions, unary operations are performed the following sequence:

  1. The operand is atomized.
  2. If the atomized operand is an empty XQuery sequence, the result will be an empty XQuery sequence.
  3. An error results if the atomized operand is an XQuery sequence of two or more atomic values.
  4. If the XQuery data type of the atomized operand is xs:untypedAtomic type, it is converted to xs:double type. An error results if this conversion is not possible.
  5. The operation is performed on the XQuery sequence obtained according to the steps above. The XQuery data type of the result will be the same as the XQuery data type at the end of step 4.

(13) XQuery value expression

(a) Function

Returns the result of evaluating an XQuery path expression.

(b) Format

XQuery-value-expression ::= XQuery-path-expression

(c) Operands

Specify the value to be evaluated in an XQuery path expression.

(14) XQuery path expression

(a) Function

Returns the result of evaluating the selected nodes in the XQuery data model.

(b) Format

XQuery-path-expression ::= {/[relative-path-expression]|//relative-path-expression|relative-path-expression}
relative-path-expression ::= step-expression [{/|//}step-expression]...
step-expression ::= {axis-step-expression| filter-expression}
axis-step-expression ::= axis-step [XQuery-predicate]...
axis-step ::= {axis {name-test |kind-test}| optional-axis-step}
name-test ::= {qualified-name|*|prefix:*|*:local-name}
optional-axis-step ::= {[@]{name-test|kind-test}| ..}
filter-expression ::= XQuery-primary-expression [XQuery-predicate]...
XQuery-predicate ::= left-bracket XQuery-expression right-bracket

(c) Operands

Selects a node at the root of the tree in which the context item belongs.

An error results if the context item is not a node.

Selects a node at the root of the tree node in which the context item belongs, and all of the descendant nodes of that node.

An error results if the context item is not a node.

A forward slash (/) specified other than at the beginning of an XQuery path expression is treated as a separator for a step expression. The XQuery sequence obtained by evaluating the step expressions is evaluated from left to right.

For example, the relative path expression E1/E2 is evaluated as follows:

  1. Step expression E1 is evaluated.
  2. Assuming each XQuery item in the XQuery sequence obtained by evaluating step expression E1 is a context item in the evaluation of step expression E2, step expression E2 is evaluated for each of those context items. If the XQuery sequence obtained by evaluating step expression E1 is empty, E2 will not be evaluated, and the result of evaluating E1/E2 will be an empty XQuery sequence.
  3. All of the XQuery sequences obtained by evaluating step expression E2 for each context item in step 2 are joined sequentially. The resulting concatenated XQuery sequence becomes the result of evaluating E1/E2.

The XQuery sequence resulting from evaluating the step expression on the left side of the forward slash cannot contain atomic values.

This is a short form of the expression /descendant-or-self::node()/.

For each context item from the results of evaluating the step expression specified on the left side, evaluates the step expression (the context item itself and its descendant nodes) specified on the right side of the XQuery sequence.

The XQuery sequence resulting from evaluating the step expression on the left side of the double forward slash cannot contain atomic values.

The axis step expression returns an XQuery sequence consisting of result nodes that have been narrowed down according to the name, kind of node, and other criteria you specify. They are obtained by traversing the specified tree starting from the context item. The nodes in this XQuery sequence become the next context items.

An error results if the context item is not a node.

The axis step returns an XQuery sequence consisting of those nodes that meet the name test or kind test out of all of the nodes that are reachable along the specified axis starting from the context item. The nodes in this XQuery sequence become the next context items.

axis
Specifies how the tree is to be traversed from the context item.
The following table and figures shows the items that can be specified as the axis and how the position of the context items being returned is assigned.

Table 1-94 List of axes

No.AxisExplanationAssigning context positions
1child::Indicates child nodes.Document order
2descendant::Indicates descendant nodes.
3attribute::Indicates the attribute nodes belonging to the context item node.
4self::Indicates the context item node itself.
5descendant-or-selfIndicates the context item node itself and all of its descendant nodes.
6following-sibling::Indicates the sibling nodes that follow the context item node, in document order. If the context item node is an attribute node, the result is an empty XQuery sequence.
7following::Indicates nodes that are not descendants of the context item node and that follow it in document order. Attribute nodes are not selected.
8parent::Indicates the parent node. If the context item node has no parent node, the result is an empty XQuery sequence.
9ancestor::Indicates the ancestor nodes.Reverse document order
10preceding-sibling::Indicates the sibling nodes that precede the context item node, in document order. If the context item node is an attribute node, the result is an empty XQuery sequence.
11preceding::Indicates nodes that are not ancestors of the context item node and that precede it in document order. Attribute nodes are not selected.
12ancestor-or-self::Indicates the context item node itself and all of its ancestor nodes.
Legend:
Document order: Context position is assigned according to document order.
Reverse document order: Context position is assigned according to reverse document order.

Figure 1-17 Axes

[Figure]
[Figure]
[Figure]

Specify that only nodes that match the specified name are to be selected from all of the nodes that can be reached by traversing the specified axis.

qualified-name
From among the nodes that are the target of the name test, selects only those nodes for which the expanded qualified name is equivalent to the specified qualified name.
*
Specified when not using the name test to narrow down the nodes being targeted.
prefix:*
From among the nodes that are the targets of the name test, selects only those nodes for which the URI of the prefix specified here is equal to the XML namespace URI for the expanded qualified name. Does not depend on the local name.
*:local-name
From among the nodes that are the targets of the name test, selects only those nodes that are equal to the local name specified here as local-name. Does not depend on the XML namespace URI.

Specify that only nodes that match the specified kind are selected from among all of the nodes that can be reached by traversing the specified axis.

The following table lists the kind tests.

Table 1-95 List of kind tests

No.CategoryHow specifiedExplanation
1Document testdocument-node()Matches any document node.
2document-node(element-test)Among document nodes that have only one element node as a child node (all their other child nodes being comment nodes or processing instruction nodes only), matches any document nodes in which the child element node matches the element test.
3Element testelement()Matches any element node.
4element(*)
5element(qualified-name)Among XML elements, matches element nodes whose expanded qualified name is equivalent to the post-expand qualified name of the specified qualified name.
6Attribute testattribute()Matches any attribute node.
7attribute(*)
8attribute(qualified-name)Among XML attributes, matches element nodes whose expanded qualified name is equivalent to the specified qualified name.
9Processing instruction testprocessing-instruction()Matches any processing instruction node.
10processing-instruction(processing-instruction-target)Among processing instruction nodes, matches processing instruction nodes with values for the processing instruction target property that match the specified processing instruction target.
11Comment testcomment()Matches any comment node.
12Text testtext()Matches any text node.
13Any kind testnode()Matches any kind of node.

Describes an axis step in a simplified manner.

[@]{name-test|kind-test}
@ is a short form of the expression attribute::.
If @ is omitted, attribute:: is assumed if the kind test is an attribute test, otherwise, child:: is assumed to precede the name test or kind test.
..
.. is a short form of the expression parent::node().
The following table provides examples of specifying an optional axis step.

Table 1-96 Optional axis step examples

No.Optional axis stepFull notation with the same meaningMeaning
1/elem/ElemName/elem/child::ElemNameDenotes the child node ElemName from among the child nodes of the context item after the evaluation of elem.
2/elem/@AttrName/elem/attribute::AttrNameDenotes the attribute node AttrName from among the attribute nodes of the context item after the evaluation of elem.
3/elem/attribute()/elem/attribute::attribute()Denotes all the attribute nodes of the context item after the evaluation of elem.
4/elem/../elem/parent::node()Denotes the parent node of the context item after the evaluation of elem.

Evaluates XQuery-primary-expression. If XQuery-predicate is specified, the condition in XQuery-predicate is used to narrow down the XQuery items in the XQuery sequence that is the result of evaluating XQuery-primary-expression.

The condition used to narrow down the context items is specified in XQuery expression.

The specified XQuery expression is evaluated for each context item in the XQuery predicate.

The results of evaluating the XQuery predicate are as follows:

(d) Rules
  1. When evaluating an XQuery path expression that contains more than one forward slash (/), the resulting XQuery sequence is as follows:
    • If the result of the step expression at the end of the XQuery path expression is an XQuery sequence that contains only nodes, the result is an XQuery sequence whose nodes are sorted in document order and with duplicate nodes excluded.
    • If the result of the step expression at the end of the XQuery path expression is an XQuery sequence that contains only atomic values, that XQuery sequence is returned unchanged.
    • An error results if the result of the step expression at the end of the XQuery path expression is an XQuery sequence that contains nodes and atomic values.
(e) Example

The following is an example of an XQuery path expression.

No.ExampleExplanation
1child::bookinfoSelects the bookinfo element node that is the child node of the context item.
2bookinfoShort for child::bookinfo.
3attribute::book_idSelects the book_id attribute node of the context item.
4@book_idShort for attribute::book_id.
5attribute::*Selects all the attribute nodes belonging to the context item.
6parent::element()Selects the element node whose parent node is the context item.
7bookinfo[category="programming"]Selects the bookinfo element node that has a child node with a category element node for which the value of the typed-value property is programming.
8bookinfo[@book_id = "12345"]Selects the bookinfo element node whose child node is the context item for which the value of its book_id attribute is 12345.
9author[2]Select the second author element node whose child node is the context item.

(15) XQuery primary expressions

(a) Function

The basic components that constitute XQuery path expressions.

XQuery primary expressions include the following:

(b) Format

XQuery-primary-expression ::= {XQuery-literal| XQuery-variable-reference|
  parenthesized-XQuery-expression| context-item-expression| XQuery-function-invocation}

Each XQuery primary expression is described below.

(c) XQuery literal
#
If you use integer literal notation to specify a literal that is beyond the range of XQuery integer literal values, HiRDB assumes a decimal point to the right of the literal and interprets it as a decimal literal.
(d) XQuery variable reference
(e) Parenthesized XQuery expression
(f) Context item expression
(g) XQuery function invocation