XQuery Syntax
XQuery is case-sensitive; XQuery elements, attributes, and variables must be valid XML names.
Basic XQuery syntax rules:
Some basic syntax rules:
- XQuery is case-sensitive
- XQuery elements, attributes, and variables must be valid XML names.
- XQuery string values can use either single or double quotes.
- XQuery variables are defined by a "$" followed by a name, for example, $bookstore
- XQuery comments are delimited by (: and :), for example, (: XQuery comment :)
XQuery Conditional Expressions
"If-Then-Else" can be used in XQuery.
See the example below:
for $x in doc("books.xml")/bookstore/book
return
if (
$x/@category="CHILDREN"
)
then
else
Note the syntax of "If-Then-Else": the parentheses after the if expression are required. else is also required, but writing just "else ()" is acceptable.
The result of the example above:
XQuery Comparison
In XQuery, there are two ways to compare values.
- General comparisons: =, !=, <, <=, >, >=
- Value comparisons: eq, ne, lt, le, gt, ge
The differences between these two comparison methods are as follows:
See the XQuery expression below:
$bookstore//book/@q > 10
If the value of the q attribute is greater than 10, the expression above returns true.
For example, if only one q is returned and its value is greater than 10, the expression returns true. If more than one q is returned, an error occurs:
$bookstore//book/@q gt 10