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:
- XQuery comments are delimited by
:). For example:
(: XQuery Comment :)
XQuery Conditional Expressions
The "If-Then-Else" conditional statement is allowed in XQuery.
for $x in doc("books.xml")/bookstore/book
return if ($x/@category="children")
Parentheses around the
if expression are required.
else is required, but it can be just
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