XQuery HTML Format

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

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:

  1. <bookstore>  
  2. <book category="web">  
  3. <title lang="en">HTML is Fun.title>  
  4. <author>Ajeet Kumarauthor>  
  5. <year>2012year>  
  6. <price>200.00price>  
  7. book>  
  8. <book category="children">  
  9. <title lang="en">Harry Pottertitle>  
  10. <author>J K. Rowlingauthor>  
  11. <year>2005year>  
  12. <price>600.00price>  
  13. book>  
  14. <book category="programming">  
  15. <title lang="en">Learn Java Programmingtitle>  
  16. <author>James Gowslingauthor>  
  17. <author>Peter Brownauthor>  
  18. <author>Sri Balaauthor>  
  19. <author>James Linnauthor>  
  20. <author>Vaidyanathan Nagarajanauthor>  
  21. <year>2003year>  
  22. <price>400.00price>  
  23. book>  
  24. <book category="web" cover="paperback">  
  25. <title lang="en">Learning XMLtitle>  
  26. <author>Aryan Tomarauthor>  
  27. <year>2013year>  
  28. <price>150.00price>  
  29. book>  
  30. bookstore>  

XQuery Expression:

books.xqy:

  1. <ul>  
  2. {  
  3. for $x in doc("books.xml")/bookstore/book/title  
  4. order by $x  
  5. return <li>{$x}li>  
  6. }  
  7. 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

  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("books.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 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:

  1. <ul>  
  2. {  
  3. for $x in doc("books.xml")/bookstore/book/title  
  4. order by $x  
  5. return <li>{data($x)}li>  
  6. }  
  7. ul>  

Output:

XQUERY Html format 2