XQuery FLWOR + HTML

XML 实例文档

我们将在下面的例子中继续使用这个 "books.xml" 文档(与上一节中的文件相同)。

在您的浏览器中查看 "books.xml" 文件

在一个 HTML 列表中提交结果

请看下面的 XQuery FLWOR 表达式:

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

上面的表达式会选取 bookstore 元素下的 book 元素下的所有 title 元素,并以字母顺序返回 title 元素。

Now, we want to list all the books in our bookstore using an HTML list. 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>