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

snippet
<?xml version="1.0" encoding="UTF-8"?>
<courses>   
   <course category="JAVA">
      <title lang="en">Learn Java in 3 Months.</title>
      <trainer>Sonoo Jaiswal</trainer>
      <year>2008</year>
      <fees>10000.00</fees>
   </course>  
    <course category="Dot Net">
      <title lang="en">Learn Dot Net in 3 Months.</title>
      <trainer>Vicky Kaushal</trainer>
      <year>2008</year>
      <fees>10000.00</fees>
   </course>
    <course category="C">
      <title lang="en">Learn C in 2 Months.</title>
      <trainer>Ramesh Kumar</trainer>
      <year>2014</year>
      <fees>3000.00</fees>
   </course>
    <course category="XML">
      <title lang="en">Learn XML in 2 Months.</title>
      <trainer>Ajeet Kumar</trainer>
      <year>2015</year>
      <fees>4000.00</fees>
   </course>  
</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

snippet
(: read the entire xml document :)
let $courses := doc("courses.xml")

for $x in $courses/courses/course
where $x/fees > 2000
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

snippet
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.xml.xquery.XQConnection;
import javax.xml.xquery.XQDataSource;
import javax.xml.xquery.XQException;
import javax.xml.xquery.XQPreparedExpression;
import javax.xml.xquery.XQResultSequence;

import com.saxonica.xqj.SaxonXQDataSource;

public class XQueryTester {
   public static void main(String[] args){
      try {
         execute();
      }
      
      catch (FileNotFoundException e) {
         e.printStackTrace();
      }
      
      catch (XQException e) {
         e.printStackTrace();
      }
   }

   private static void execute() throws FileNotFoundException, XQException{
      InputStream inputStream = new FileInputStream(new File("courses.xqy"));
      XQDataSource ds = new SaxonXQDataSource();
      XQConnection conn = ds.getConnection();
      XQPreparedExpression exp = conn.prepareExpression(inputStream);
      XQResultSequence result = exp.executeQuery();
       while (result.next()) {
         System.out.println(result.getItemAsString(null));
      }
   }	
}

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

snippet
(: read all courses :)
let $courses := doc("courses.xml")/courses/course

for $x in $courses
where $x/fees > 2000
return $x/title

Follow the same procedure specified above:

Output:

XQUERY Xpath 2

XQuery Type 3

snippet
(: read courses with fees > 2000 :)
let $courses := doc("courses.xml")/courses/course[fees > 20]

for $x in $courses
return $x/title

Follow the same procedure specified above:

Output:

XQUERY Xpath 3
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +