XQuery Syntax
XQuery is case-sensitive, and elements, attributes, and variables in XQuery must be valid XML names.
Basic Syntax Rules of XQuery:
Some basic syntax rules:
- XQuery is case-sensitive.
- Elements, attributes, and variables in XQuery must be valid XML names.
- XQuery string values can use single or double quotes.
- XQuery variables are defined with 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: if ($x/@category="CHILDREN")
then
<child>{data($x/title)}</child>
else
<adult>{data($x/title)}</adult>
Note the syntax of "If-Then-Else": parentheses after the if expression are required. The else part is also required, but writing "else ()" is acceptable.
Result of the example above:
XQuery Comparisons
In XQuery, there are two ways to compare values.
- General comparison: =, !=, <, <=, >, >=
- Value comparison: eq, ne, lt, le, gt, ge
Differences between these comparison methods:
Consider the following XQuery expression:
If the value of the q attribute is greater than 10, the expression returns true.
In this 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: