XQuery Syntax

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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:

 
  1. <bookstore>  
  2. <book category="Romantic">  
  3. <title lang="en">If she loves metitle>  
  4. <author>Ajeet Kumarauthor>  
  5. <year>2014year>  
  6. <price>150price>  
  7. book>  
  8. <book category="children">  
  9. <title lang="en">Harry Pottertitle>  
  10. <author>J K. Rowlingauthor>  
  11. <year>2005year>  
  12. <price>700price>  
  13. book>  
  14. <book category="programming">  
  15. <title lang="en">Let's Ctitle>  
  16. <author>Yashwant Kanetkarauthor>  
  17. <year>2003year>  
  18. <price>200price>  
  19. book>  
  20. bookstore>  

XQuery Conditional Expressions

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

XQuery statement:

books.xqy

 
  1. for $x in doc("books.xml")/bookstore/book  
  2. return if ($x/@category="CHILDREN")  
  3. then {data($x/title)}child>  
  4. else {data($x/title)}adult>  

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

 
  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.InputStream;  
  5. import javax.xml.xquery.XQConnection;  
  6. import javax.xml.xquery.XQDataSource;  
  7. import javax.xml.xquery.XQException;  
  8. import javax.xml.xquery.XQPreparedExpression;  
  9. import javax.xml.xquery.XQResultSequence;  
  10. import com.saxonica.xqj.SaxonXQDataSource;  
  11. public class XQueryTester {  
  12.    public static void main(String[] args){  
  13.       try {  
  14.          execute();  
  15.       }  
  16.       catch (FileNotFoundException e) {  
  17.          e.printStackTrace();  
  18.       }        
  19.       catch (XQException e) {  
  20.          e.printStackTrace();  
  21.       }  
  22.    }  
  23.    private static void execute() throws FileNotFoundException, XQException{  
  24.       InputStream inputStream = new FileInputStream(new File("courses.xqy"));  
  25.       XQDataSource ds = new SaxonXQDataSource();  
  26.       XQConnection conn = ds.getConnection();  
  27.       XQPreparedExpression exp = conn.prepareExpression(inputStream);  
  28.       XQResultSequence result = exp.executeQuery();  
  29.        while (result.next()) {  
  30.          System.out.println(result.getItemAsString(null));  
  31.       }  
  32.    }      
  33. }  

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.

 
  1. $bookstore//book/@q > 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:

 
  1. $bookstore//book/@q gt 100