Home >>XQuery Tutorial >XQuery HTML Format
XQuery may also be used to convert an XML document into an HTML file with ease. To understand how XQuery does that, take a look at the following instances.
We are going to be using the same books.xml file. The following example utilizes books.xml data extract from XQuery and generates an Xml table comprising all the book titles together with their respective prices.
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>70.50</price>
</book>
<book category="XML">
<title lang="en">Learn XQuery in 24 hours</title>
<author>Max</author>
<author>Sweden</author>
<year>2018</year>
<price>50.00</price>
</book>
<book category="XML">
<title lang="en">Learn XPath in 24 hours</title>
<author>charlie</author>
<year>2008</year>
<price>16.50</price>
</book>
</books>
Given below is the Xquery expression that is to be executed on the above XML document.
books.xqy
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>
}
</table>
</results>
Result
<table>
<tr>
<th>Title</th>
<th>Price</th>
</tr>
<tr>
<td>Learn XPath in 24 hours</td>
<td>16.50</td>
</tr>
<tr>
<td>Learn Java in 24 Hours</td>
<td>30.00</td>
</tr>
<tr>
<td>Learn XQuery in 24 hours</td>
<td>50.00</td>
</tr>
<tr>
<td>Learn .Net in 24 hours</td>
<td>70.50</td>
</tr>
</table>
XQuery Expression: books.xqy:
<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{$x}</li>
}
</ul>
This example would select all the title elements below the book elements below the bookstore element, and return the title elements in alphabetical order.
Now, build a Java based XQuery executor program to interpret the books.xqy, transfer it over to the processor for XQuery input, and execute the message. After that it shows the output.
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("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));
}
}
}
Place the three files above to the same location. We placed them in a folder called XQuery4 on your desktop. Use terminal compile XQueryTester.java. You require JDK 1.5 or later to be enabled on your device, and optimized classpaths.
Compile:
javac XQueryTester.java
Execute:
java XQueryTester