Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XQuery Syntax

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 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

{data($x/title)}

else

{data($x/title)}

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:

Everyday Italian

Harry Potter

Learning XML

XQuery Kick Start


XQuery Comparison

In XQuery, there are two ways to compare values.

  1. General comparisons: =, !=, <, <=, >, >=
  2. 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