XQuery HTML Format

XQuery can also be used to transform an XML document into an HTML page.

Let's take an example to see how it works:

XQuery HTML Format Example

Let's take an XML file named "books.xml", having the following code.

XML statement:

books.xml:

snippet
<bookstore>
<book category="web">
<title lang="en">HTML is Fun.</title>
<author>Ajeet Kumar</author>
<year>2012</year>
<price>200.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>600.00</price>
</book>
<book category="programming">
<title lang="en">Learn Java Programming</title>
<author>James Gowsling</author>
<author>Peter Brown</author>
<author>Sri Bala</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>400.00</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Aryan Tomar</author>
<year>2013</year>
<price>150.00</price>
</book>
</bookstore>

XQuery Expression:

books.xqy:

snippet
<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{$x}</li>
}
</ul>

This example will select all the title elements under the book elements that are under the bookstore element, and return the title elements in alphabetical order.

Now, 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

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("books.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 XQuery4. 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 Html format 1

If you want to eliminate the title element and show only the data inside the title element, use the following XQuery expression:

books.xqy:

snippet
<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{data($x)}</li>
}
</ul>

Output:

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