Latest web development tutorials

XQuery FLWOR + HTML

XML instance documents

We will continue to use the "books.xml" document (on a file in the same) in the following examples.

See "books.xml" file in your browser .


Present the results in an HTML list

Consider the following XQuery FLWOR expression:

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

The above expression will select all the title elements of the book elements under the bookstore element under, in alphabetical order and return the title elements.

Now, we want to use the HTML table lists all our bookstore bibliography. We added <ul> and <li> tags to FLWOR expression:

<ul>
{

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

The above code output:

<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, but only display 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>