XQuery FLWOR + HTML
XML 实例文档
我们将在下面的例子中继续使用这个 "books.xml" 文档(与上一节中的文件相同)。
在您的浏览器中查看 "books.xml" 文件。
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!-- Edited with XML Spy v2007 (http://www.altova.com) -->
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
在一个 HTML 列表中提交结果
请看下面的 XQuery FLWOR 表达式:
for $x in doc("books.xml")/bookstore/book/title order by $x return $x
上面的表达式会选取 bookstore 元素下的 book 元素下的所有 title 元素,并以字母顺序返回 title 元素。
现在,我们希望使用 HTML 列表列出我们的书店中所有的书目。我们向 FLWOR 表达式添加 <ul> 和 <li> 标签:
<ul> { for $x in doc("books.xml")/bookstore/book/title order by $x return <li>{$x}</li> } </ul>
以上代码的结果:
<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>
现在我们希望去除 title 元素,而仅仅显示 title 元素内的数据。
<ul> { for $x in doc("books.xml")/bookstore/book/title order by $x return <li>{data($x)}</li> } </ul>
结果将是一个 HTML 列表:
<ul> <li>Everyday Italian</li> <li>Harry Potter</li> <li>Learning XML</li> <li>XQuery Kick Start</li> </ul>
发表评论 取消回复