XQuery - HTML Format

XQuery can also be easily used to transform an XML document into an HTML page. Take a look at the following example to understand how XQuery does it.


We will use the same books.xml file. The following example uses XQuery extract data from books.xml and create an HTML table containing the titles of all the books along with their respective prices.


<?xml version="1.0" encoding="UTF-8"?>
   <book category="JAVA">
      <title lang="en">Learn Java in 24 Hours</title>
   <book category="DOTNET">
      <title lang="en">Learn .Net in 24 hours</title>
   <book category="XML">
      <title lang="en">Learn XQuery in 24 hours</title>
   <book category="XML">
      <title lang="en">Learn XPath in 24 hours</title>
      <author>Jay Ban</author>

Given below is the Xquery expression that is to be executed on the above XML document.


let $books := (doc("books.xml")/books/book)
return <table><tr><th>Title</th><th>Price</th></tr>
   for $x in $books   
   order by $x/price
   return <tr><td>{data($x/title)}</td><td>{data($x/price)}</td></tr>


      <td>Learn XPath in 24 hours</td>
      <td>Learn Java in 24 Hours</td>
      <td>Learn XQuery in 24 hours</td>
      <td>Learn .Net in 24 hours</td>

Verify Result

To verify the result, replace the contents of books.xqy (given in the Environment Setup chapter) with the above XQuery expression and execute the XQueryTester java program.

XQuery Expressions

Here we've used the following XQuery expressions −

  • data() function to evaluate the value of the title element, and

  • {} operator to tell the XQuery processor to consider data() as a function. If {} operator is not used, then data() will be treated as normal text.