XQuery Syntax
XQuery is case-sensitive and XQuery elements, attributes, and variables must be valid XML names.
XQuery Syntax Rules
- XQuery is case-sensitive
- XQuery elements, attributes, and variables must be valid XML names
- An XQuery string value can be in single or double quotes
- An XQuery variable is defined with a
$
followed by a name. For example:$bookstore
- XQuery comments are delimited by
(:
and:)
. For example:(: XQuery Comment :)
XQuery Conditional Expressions
The "If-Then-Else" conditional statement is allowed in XQuery.
For example:
for $x in doc("books.xml")/bookstore/book
return if ($x/@category="children")
then <child>{data($x/title)}</child>
else <adult>{data($x/title)}</adult>
note
Parentheses around the if
expression are required.
Also, else
is required, but it can be just else ()
.
XQuery Comparisons
There are two types for comparing values in XQuery.
- General Comparison:
=
,!=
,<
,<=
,>
,>=
- Value Comparison:
eq
,ne
,lt
,le
,gt
,ge
There is a difference between general comparison and value comparison, and it's explained below with an example.
The following expression returns true if any attr
attributes have a value greater than 100.
$bookstore//book/@attr > 100
The following expression returns true if there is only one attr
attribute returned by the expression, and its value is greater than 10. If more than one attr
is returned, an error occurs:
$bookstore//book/@attr gt 100