XQuery Syntax
- Previous Page XQuery Terminology
- Next Page XQuery Add
XQuery is case-sensitive, and XQuery elements, attributes, and variables must be valid XML names.
Basic grammar rules of XQuery:
Some basic grammar rules:
- XQuery is case-sensitive
- XQuery elements, attributes, and variables must be valid XML names.
- XQuery string values can be enclosed in single or double quotes.
- XQuery variables are defined by the "$" symbol followed by a name, for example, $bookstore
- XQuery comments are separated by (: and :), for example, (: XQuery comment :)
XQuery Conditional Expression
"If-Then-Else" can be used in XQuery.
See the following 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>
Please note the syntax of "If-Then-Else": the parentheses after the if expression are required. else is also required, but you can also write "else ()".
The result of the above example:
<adult>Everyday Italian</adult> <child>Harry Potter</child> <adult>Learning XML</adult> <adult>XQuery Kick Start</adult>
XQuery Comparison
There are two methods to compare values in XQuery.
- General comparison: =, !=, <, <=, >, >=
- Value comparison: eq, ne, lt, le, gt, ge
The differences between these two comparison methods are as follows:
Please see the following XQuery expression:
$bookstore//book/@q > 10
If the value of the q attribute is greater than 10, the return value of the above expression is true.
$bookstore//book/@q gt 10
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 will occur.
- Previous Page XQuery Terminology
- Next Page XQuery Add