当前位置: 首页 > 网络学院 > XML相关教程 > Xquery > XQuery 添加元素和属性

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

Xquery 中的 XQuery 添加元素和属性


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 609 ::
收藏到网摘: 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”


Adding Elements and Attributes to the Result
向结果中添加元素和属性

As we have seen in a previous chapter, we may include elements and attributes from the input document ("books.xml) in the result:
就像在前几章中所看到的,我们可以把来自输入文档("books.xml)的元素和属性添加到结果中:

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

The XQuery expression above will include both the title element and the lang attribute in the result, like this:
上述 XQuery 表达式会将 title 元素和 lang 属性添加到结果中,如下所示:

<title lang="en">Everyday Italian</title>

<title lang="en">Harry Potter</title>
<title lang="en">Learning XML</title>
<title lang="en">XQuery Kick Start</title>

The XQuery expression above returns the title elements the exact same way as they are described in the input document.
上述 XQuery 表达式获取了 title 元素,它们和原来在输入文档中被描述的一样。

We now want to add our own elements and attributes to the result!
现在,我们希望把我们自己的元素和属性添加到结果中。

Add HTML Elements and Text
添加 HTML 元素和文本

Now, we want to add some HTML elements to the result. We will put the result in an HTML list - together with some text:
现在,我们希望将一些HTML元素添加到结果中。我们将把结果连同一些文本内容放在一个HTML列表中:

<html>
<body>
<h1>Bookstore</h1>
<ul>
{
for $x in doc("books.xml")/bookstore/book
order by $x/title
return <li>{data($x/title)}. Category: {data($x/@category)}</li>

}
</ul>
</body>
</html>

The XQuery expression above will generate the following result:
上述XQuery表达式将会输出下面的结果:

<html>
<body>
<h1>Bookstore</h1>
<ul>
<li>Everyday Italian. Category: COOKING</li>

<li>Harry Potter. Category: CHILDREN</li>
<li>Learning XML. Category: WEB</li>
<li>XQuery Kick Start. Category: WEB</li>
</ul>
</body>
</html>

Add Attributes to HTML Elements
向HTML元素中添加属性

Next, we want to use the category attribute as a class attribute in the HTML list:
接下来,我们希望把 category 属性作为 HTML 列表的一个类属性:

<html>

<body>
<h1>Bookstore</h1>
<ul>
{
for $x in doc("books.xml")/bookstore/book
order by $x/title
return <li class="{data($x/@category)}">{data($x/title)}</li>
}

</ul>
</body>
</html>

The XQuery expression above will generate the following result:
上述XQuery表达式将会输出下面的结果:

<html>
<body>
<h1>Bookstore</h1>
<ul>
<li class="COOKING">Everyday Italian</li>

<li class="CHILDREN">Harry Potter</li>
<li class="WEB">Learning XML</li>
<li class="WEB">XQuery Kick Start</li>
</ul>
</body>
</html>

评论 (0) All

登陆 | 还没注册?