XQuery
FLWOR + HTML
This section introduces you to
XQuery FLWOR expression plus HTML usage.
XML instance documentation
We will continue to use this "books.xml" document in the following example (the same as the files in the previous section).
View the "books" file .xml your browser.
Submit the results in an HTML list
Take a look at the following XQuery FLWOR expression:
for $x in doc("books.xml")/bookstore/book/title
order by $x
return $x
The above expression picks all title elements under the book element under the bookstore element and returns the title element in alphabetical order.
Now we want to use the HTML list to list all the books in our bookstore.
Let's add the
hashtags slt;ul>
and
slt;li> to the
FLWOR expression:
<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return
<li>{
$x
}</li>
}
</ul>
The above code output results:
<ul>
<li><title >Everyday Italian</title></li>
<li><title >Harry Potter</title></li>
<li><title >Learning XML</title></li>
<li><title >XQuery Kick Start</title></li>
</ul>
Now we want to remove the title element and show only 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>
Related tutorials
HTML tutorial