Home >>XQuery Tutorial >XQuery Environment Setup

XQuery Environment Setup

XQuery Environment Setup

Let's see how to create an environment for local development. Here we use Saxon XQuery Processor jar file. The Saxon XQuery processor, built on Java, is used to check the.xqy document, a file that includes XQuery expression against our XML sample document.

To run the java application, you need to load jar files from Saxon XQuery Server.

Add build-path to certain jar files for the eclipse mission. Or, if you use the command prompt to run java, you need to set the classpath to such jar files, or place the jar files in the JRE / lib / ext directory.

We use a separate open source XQuery processor, Saxon Home Edition (Saxon-HE), which is commonly used. This processor supports XSLT 2.0, XQuery 3.0, and XPath 3.0 and the performance is highly configured. Saxon XQuery processor is usable without any XML database. In our instances, we will use a basic XML document as our database.


For test books.xqy, a file containing XQuery expression against our sample XML text, i.e. books.xml, we'll use the Saxon XQuery Processor centered on Java.

In this case, we can see how to write and process a query in order to get the title elements of the books whose price exceeds 30.


<?xml version="1.0" encoding="UTF-8"?>
   <book category="JAVA">
      <title lang="en">Learn Java Here</title>
   <book category="DOTNET">
      <title lang="en">Learn </title>
   <book category="XML">
      <title lang="en">Learn XQuery </title>
   <book category="XML">
      <title lang="en">Learn XPath </title>


for $x in doc("books.xml")/books/book
where $x/price>70
return $x/title


package com.tutorialspoint.xquery;

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 {
      catch (FileNotFoundException e) {
      catch (XQException e) {

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

Steps to Execute XQuery against XML

Step 1 − Copy XQueryTester.java to any location, say, E: > java

Step 2 − Copy books.xml to the same location, E: > java

Step 3 − Copy books.xqy to the same location, E: > java

Step 4 - Use console compile XQueryTester.java. Make sure you've enabled JDK 1.5 or later on your machine, and setup classpaths. See our JAVA Tutorial for more information about how to use JAVA

E:\java\javac XQueryTester.java

Step 5 − Execute XQueryTester

E:\java\java XQueryTester

You'll get the following result −

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

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