XQuery Syntax

XQuery elements, attributes, and variables must be valid XML names and follow case sensitivity.

XQuery Syntax Rules

  • XQuery follows case-sensitivity.
  • XQuery elements, attributes, and variables must be valid XML names.
  • An XQuery string value can be in single or double quotes.
  • An XQuery variable is defined with a $ followed by a name, for example:. $bookstore
  • XQuery comments are delimited by (: and :), e.g. (: XQuery Comment :)

Example

Let's take an XML file 'books.xml' having the following data:


XQuery Conditional Expressions

The "If-Then-Else" conditional statement is allowed in XQuery.

XQuery statement:

books.xqy


How to run

Create a Java based XQuery executor program to read the books.xqy, passes it to the XQuery expression processor, and executes the expression. After that the result will be displayed.

XQueryTester.java


Execute XQuery against XML

Put the above three files to a same location. We put them on desktop in a folder name XQuery6.

Compile XQueryTester.java using console. You must have JDK 1.5 or later installed on your computer and classpaths are configured.

Compile:

javac XQueryTester.java

Execute:

java XQueryTester

Output:

XQUERY Syntax 1

XQuery Comparisons

There are two types for comparing values in XQuery.

  1. General Comparison: =, !=, <, <=, >, >=
  2. Value Comparison: eq, ne, lt, le, gt, ge

Difference between general comparison and value comparison

It can be explained by an example. The following expression returns true if any q attributes have a value greater than 100.

The following expression returns true if there is only one q attribute returned by the expression, and its value is greater than 10. If more than one q is returned, an error occurs:

Next TopicXQuery Add




Latest Courses