Skip to main content

XQuery HTML Format

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


Consider the following example that contains information about a bookstore.

<?xml version="1.0" encoding="UTF-8"?>

<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<book category="web">
<title lang="en">XQuery Tips</title>
<author>Tom Nolan</author>
<book category="web">
<title lang="en">Learn XML</title>
<author>Tutorial Reference</author>

We want to create an HTML table containing the titles of all books with their authors and prices:

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

The result of the XQuery expression above is:

<td>Harry Potter</td>
<td>J K. Rowling</td>
<td>Learn Java in 24 Hours</td>
<td>Tom Nolan</td>
<td>Learn XML</td>
<td>Tutorial Reference</td>

Table of Contents