XQuery Tutoriallogin
XQuery Tutorial
author:php.cn  update time:2022-04-21 16:43:44

XQuery FLWOR + HTML



XML Example Document

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

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


Submitting results in an HTML list

See 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 title elements under the book element under the bookstore element, in alphabetical order Returns the title element.

Now, we want to use an HTML list to list all the titles in our bookstore. We add the <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 above code output result:

<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 a HTML list:

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