当前位置: 首页 > 网络学院 > XML相关教程 > Xquery > XQuery FLWOR + HTML

Xquery
XQuery 介绍
XQuery 实例
XQuery FLWOR 表达式
XQuery FLWOR + HTML
XQuery 术语
XQuery 语法
XQuery 添加元素和属性
XQuery 选择和过滤
XQuery 函数
XQuery 摘要
XQuery 参考

Xquery 中的 XQuery FLWOR + HTML


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 606 ::
收藏到网摘: n/a

The XML Example Document
XML 案例文档

We will use the "books.xml" document in the examples below (same XML file as in the previous chapters).
下述案例中,我们会用到 "books.xml" 文档(上一章使用的XML文件)。

View the "books.xml" file in your browser.
在你的浏览器中浏览“books.xml”


Present the Result In an HTML List
在HTML列表中显示结果

Look at the following XQuery FLWOR expression:
先看看下面的Query FLWOR表达式:

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

The expression above will select all the title elements under the book elements that are under the bookstore element, and return the title elements in alphabetical order.
上述表达式会选择在 bookstore 元素下的 book 元素下所有 title 元素,并按字母顺序排列 title 元素后输出。

Now we want to list all the book-titles in our bookstore in an HTML list. We add <ul> and <li> tags to the FLWOR expression:
现在,我们希望在 HTML列表中列出 bookstore 的所有book-titles元素,因此,我们需要在 FLWOR 表达式中加入<ul>和<li>标签:

<ul>
{

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

}
</ul>

The result of the above will be:
上述的结果显示如下:

<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 eliminate the title element, and show only the data inside the title element:
现在,我们要除去 title 元素,只显示 title 元素里的数据:

<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):
结果(一份HTML列表)显示如下:

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

</ul>

评论 (0) All

登陆 | 还没注册?