XQuery FLWOR + HTML

XML Sample Document

We will continue to use this "books.xml" document in the following examples (the same as the file in the previous section).

View the "books.xml" file in your browser.

Submit the results in an HTML list

Please see the following XQuery FLWOR expression:

for $x in doc("books.xml")/bookstore/book/title
order by $x
return $x

The above expression selects all title elements under the book elements under the bookstore element and returns them in alphabetical order.

Now, we want to use an HTML list to display all the books in our bookstore. We add <ul> and <li> tags to the FLWOR expression:

<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{$x}</li>
}
</ul>

The result of the above code:

<ul>
<li><title lang="en">Everyday Italian</title></li>
<li><title lang="en">Harry Potter</title></li>
<li><title lang="en">Learning XML</title></li>
<li><title lang="en">XQuery Kick Start</title></li>
</ul>

Now we want to remove the title element and only display the data within the title element.

<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{data($x)}</li>
}
</ul>

The result will be an HTML list:

<ul>
<li>Everyday Italian</li>
<li>Harry Potter</li>
<li>Learning XML</li>
<li>XQuery Kick Start</li>
</ul>