Home >>XQuery Tutorial >XQuery XPath

XQuery XPath

XQuery XPath

XQuery reaches XPath. The search results on XML sets are limited using XPath expressions. See our XPath Guide for more details about how to use XPath.

Mind the following XPath phrase we used to get the list of books from earlier.

doc("books.xml")/books/book

XQuery XPath Example

Let's take an XML document that has the details regarding course selection. We can use the term XQuery to recover the names of certain courses.

books.xml


<?xml version="1.0" encoding="UTF-8"?>
<books>
   
   <book category="JAVA">
      <title lang="en">Learn Java in 24 Hours</title>
      <author>Max</author>
      <year>2008</year>
      <price>30.00</price>
   </book>
   
   <book category="DOTNET">
      <title lang="en">Learn .Net in 24 hours</title>
      <author>Sweden</author>
      <year>2012</year>
      <price>40.50</price>
   </book>
   
   <book category="XML">
      <title lang="en">Learn XQuery in 24 hours</title>
      <author>Max</author>
      <author>Sweden</author> 
      <year>2012</year>
      <price>50.00</price>
   </book>
   
   <book category="XML">
      <title lang="en">Learn XPath in 24 hours</title>
      <author>Charlie</author>
      <year>2010</year>
      <price>16.50</price>
   </book>
   
</books>

Here we have provided three versions of an XQuery statement which fulfill the same purpose of displaying book titles with a price value greater than 30.

XQuery – Version 1


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

for $x in $books/books/book
where $x/price > 30
return $x/title

Output:

<title lang="en">Learn .Net in 24 hours</title>

<title lang="en">Learn XQuery in 24 hours</title>

XQuery – Version 2


(: read all books :)
let $books := doc("books.xml")/books/book

for $x in $books
where $x/price > 30
return $x/title

Output:

<title lang="en">Learn .Net in 24 hours</title>

<title lang="en">Learn XQuery in 24 hours</title>

XQuery – Version 3


(: read books with price > 30 :)
let $books := doc("books.xml")/books/book[price > 30]

for $x in $books
return $x/title

Output:

<title lang="en">Learn .Net in 24 hours</title>

<title lang="en">Learn XQuery in 24 hours</title>

How to run

To read the courses.xqy, build a Java-based XQuery executor application, pass it to the XQuery Expression Processor, and execute the word. After that it displays the result.

XQueryTester.java


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 three files above to the same location. We put them in a folder called XQuery3 on your desktop. Use terminal compile XQueryTester.java. You require JDK 1.5 or later to be enabled on your computer, and optimized classpaths.

Compile:

javac XQueryTester.java

Execute:

java XQueryTester