XQuery XPath

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

XQuery XPath

XQuery uses XPath expressions to restrict the search results on XML collections.


XQuery XPath Example

Let's take an XML document having the information on the collection of courses. We will use XQuery expression to retrieve the titles of those courses.

courses.xml

  1. xml version="1.0" encoding="UTF-8"?>  
  2. <courses>     
  3.    <course category="JAVA">  
  4.       <title lang="en">Learn Java in 3 Months.title>  
  5.       <trainer>Sonoo Jaiswaltrainer>  
  6.       <year>2008year>  
  7.       <fees>10000.00fees>  
  8.    course>    
  9.     <course category="Dot Net">  
  10.       <title lang="en">Learn Dot Net in 3 Months.title>  
  11.       <trainer>Vicky Kaushaltrainer>  
  12.       <year>2008year>  
  13.       <fees>10000.00fees>  
  14.    course>  
  15.     <course category="C">  
  16.       <title lang="en">Learn C in 2 Months.title>  
  17.       <trainer>Ramesh Kumartrainer>  
  18.       <year>2014year>  
  19.       <fees>3000.00fees>  
  20.    course>  
  21.     <course category="XML">  
  22.       <title lang="en">Learn XML in 2 Months.title>  
  23.       <trainer>Ajeet Kumartrainer>  
  24.       <year>2015year>  
  25.       <fees>4000.00fees>  
  26.    course>    
  27. courses>  

Here, we use three different types of XQuery statement that will display the same result having fees is greater than 2000.


XQuery Type 1

courses.xqy

  1. (: read the entire xml document :)  
  2. let $courses :doc("courses.xml")  
  3.   
  4. for $x in $courses/courses/course  
  5. where $x/fees > 2000  
  6. return $x/title  

How to run

Create a Java based XQuery executor program to read the courses.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.   
  6. import javax.xml.xquery.XQConnection;  
  7. import javax.xml.xquery.XQDataSource;  
  8. import javax.xml.xquery.XQException;  
  9. import javax.xml.xquery.XQPreparedExpression;  
  10. import javax.xml.xquery.XQResultSequence;  
  11.   
  12. import com.saxonica.xqj.SaxonXQDataSource;  
  13.   
  14. public class XQueryTester {  
  15.    public static void main(String[] args){  
  16.       try {  
  17.          execute();  
  18.       }  
  19.         
  20.       catch (FileNotFoundException e) {  
  21.          e.printStackTrace();  
  22.       }  
  23.         
  24.       catch (XQException e) {  
  25.          e.printStackTrace();  
  26.       }  
  27.    }  
  28.   
  29.    private static void execute() throws FileNotFoundException, XQException{  
  30.       InputStream inputStream = new FileInputStream(new File("courses.xqy"));  
  31.       XQDataSource ds = new SaxonXQDataSource();  
  32.       XQConnection conn = ds.getConnection();  
  33.       XQPreparedExpression exp = conn.prepareExpression(inputStream);  
  34.       XQResultSequence result = exp.executeQuery();  
  35.        while (result.next()) {  
  36.          System.out.println(result.getItemAsString(null));  
  37.       }  
  38.    }      
  39. }  

Execute XQuery against XML

Put the above three files to a same location. We put them on desktop in a folder name XQuery3. 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 Xpath 1


XQuery Type 2

  1. (: read all courses :)  
  2. let $courses :doc("courses.xml")/courses/course  
  3.   
  4. for $x in $courses  
  5. where $x/fees > 2000  
  6. return $x/title  

Follow the same procedure specified above:

Output:

XQUERY Xpath 2


XQuery Type 3

  1. (: read courses with fees > 2000 :)  
  2. let $courses :doc("courses.xml")/courses/course[fees > 20]  
  3.   
  4. for $x in $courses  
  5. return $x/title  

Follow the same procedure specified above:

Output:

XQUERY Xpath 3