Skip to main content

XQuery HTML Format

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

Example

Consider the following example that contains information about a bookstore.

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

<bookstore>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Tips</title>
<author>Tom Nolan</author>
<year>2022</year>
<price>39.99</price>
</book>
<book category="web">
<title lang="en">Learn XML</title>
<author>Tutorial Reference</author>
<year>2022</year>
<price>9.99</price>
</book>
</bookstore>

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

books.xqy
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>
}
</table>
</results>

The result of the XQuery expression above is:

<table>
<tr>
<th>Title</th>
<th>Author</th>
<th>Price</th>
</tr>
<tr>
<td>Harry Potter</td>
<td>J K. Rowling</td>
<td>29.99</td>
</tr>
<tr>
<td>Learn Java in 24 Hours</td>
<td>Tom Nolan</td>
<td>39.99</td>
</tr>
<tr>
<td>Learn XML</td>
<td>Tutorial Reference</td>
<td>9.99</td>
</tr>
</table>

Table of Contents