Latest web development tutorials

XQuery syntax

XQuery is case-sensitive, XQuery elements, attributes, and variables must be legal XML names.


XQuery's basic grammar rules:

Some basic grammar rules:

  • XQuery is case sensitive
  • XQuery elements, attributes, and variables must be legal XML names.
  • XQuery string value can be used single or double quotes.
  • XQuery variable from "$" followed by a name and is defined, for example, $ bookstore
  • XQuery comments are (: :) and segmentation, for example, (: XQuery Comment :)

XQuery conditional expression

"If-Then-Else" can be used in XQuery.

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

Note that the "If-Then-Else" syntax: parentheses after the if expression is required. else it is also required, but write-only "else ()" can be.

The example above results:

<adult>Everyday Italian</adult>
<child>Harry Potter</child>
<adult>Learning XML</adult>
<adult>XQuery Kick Start</adult>


XQuery comparison

In XQuery, there are two ways to compare values.

  1. General comparison: =, =, <, <=,>,> =!
  2. Comparative values: eq, ne, lt, le, gt, ge

Compare differences between the two methods are as follows:

Consider the following XQuery expression:

$bookstore//book/@q > 10

If the property value q is greater than 10, the above expression returns a value of true.

The following examples, if the return is only a q, and its value is greater than 10, then the expression returns true. If more than one q is returned, an error occurs:

$bookstore//book/@q gt 10