以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XQuery/XLink/XPointer/ 』  (http://bbs.xml.org.cn/list.asp?boardid=14)
----  我的第一帖,写一些总结的内容  (http://bbs.xml.org.cn/dispbbs.asp?boardid=14&rootid=&id=24306)


--  作者:杨一
--  发布时间:11/16/2005 11:15:00 AM

--  我的第一帖,写一些总结的内容
XQuery是一种新兴的,正在发展之中的规范。可以认为是"XML Query",是W3C为使用简单和统一的语法来访问分布式的信息存储系统例如内容仓库,文件,关系型数据库,和对象仓库而制订的XML标准。XQuery使用XML结构的优势能表达跨越多种类型数据查询的能力.XQuery对以任何形式存在的XML进行查询。
XQuery提供了对于任何形式的数据的转换,与关系型数据库的衔接。通过他的衔接,就可以把关系型的数据库作为一种XML的形式来查询,并解析,包括输出。
XQuery不仅局限为服务端的使用,已经有成型的技术和产品在客户端的功能中应用了这种技术XQuery已经有一些大公司提供了相应的成型的产品(在下面的内容中进行讨论),并且这些产品所遵循的规范并不相同,这是需要注意的地方。
XQuery是构建于XPath之上的,XPath能够迅速的提取XML的某一具体部分,比如元素,属性等等,它可以是一个集合,也可以是一个元素。在现有的解析XML的DOM Level3实现中(开源),DOM4J很好的支持了XPath,在DOM4J的官方网站上可以找到一个quick guide,里面有介绍XPath内容的部分。


Dom4J的官方网站是:
http://www.dom4j.org/guide

XPath的中文实例说明:
http://www.zvon.org/xxl/XPathTutorial/General/examples.html

下面是一些XPath应用的例子:
/html/body/h1. Selects all <h1> elements that are children of a <body> element that is the child of an <html> element that is the root element in a document. The result may be multiple <h1> elements.
//h1. Selects all <h1> elements that appear anywhere within a document. The double-slash indicates arbitrary depth.
count(//book). Returns the number of <book> elements that appear within a document.
//book[author = "Hunter"]. Returns all <book> elements that have an <author> child element whose string value is "Hunter." The square brackets surround a "predicate" that acts as a filter on the match results.
//book[@year > 1999]. Returns all <book> elements that have an attribute "year with a value greater than 1999." The @ sign marks year as an attribute. Notice how an attribute value can be treated as an integer for comparison within XPath.
//book[@pages]. Returns all <book> elements that have a pages attribute with any value.
//book/@pages. Returns all the pages attributes that are attached to <book> elements.
(i | b). Returns all <i> or <b> child elements from the current context node. XPath expressions work like file system lookups; if there's no leading slash, the path is relative to the "current context node" (specified outside the expression).
(//servlet | //servlet-mapping) [servlet-name = $servlet]. Returns all <servlet> or <servlet-mapping> elements that have a <servlet-name> child element whose value equals the $servlet variable.
//key[. = "Total Time"]. Returns all <key> elements that have a value of "Total Time." The "." in the expression represents the context node, which is similar to a "this" pointer in object-oriented languages.
(//key)[1]/text(). Returns the text nodes of the first <key> element within the document.

在XQuery中,XPath只能用于简单的查询, 尽管它们通常也作为复杂查询的一部分而存在. 同志们可以在前面提到的XPath网站上找到相关的用XPath查询的例子。


FLWOR Expressions (这个名字是不是听上去怪怪的?没错,它的读音是flower)


它的名称的由来如下:
For, Let, Where, Order by, Return
For提供了遍历的方法,Let提供了赋值的方法
http://www.w3.org/ XML/Query提供了一个应用这些命令查询的例子:
<books-with-prices>
{
for $b in document("http://www.bn.com/bib.xml")//book,
$a in document("http://www.amazon.com/reviews.xml")//entry

where $b/title = $a/title
return
<book-with-prices>
{ $b/title }
<price-amazon>{ $a/price/text() }</price-amazon>
<price-bn>{ $b/price/text() }</price-bn>

</book-with-prices>
}
</books-with-prices>

Executing this query returns a result like this:

<books-with-prices>

<book-with-prices>
<title>TCP/IP Illustrated</title>
<price-amazon>65.95</price-amazon>
<price-bn>65.95</price-bn>
</book-with-prices>
<book-with-prices>
<title>Advanced Programming in the Unix environment</title>
<price-amazon>65.95</price-amazon>

<price-bn>65.95</price-bn>
</book-with-prices>
<book-with-prices>
<title>Data on the Web</title>
<price-amazon>34.95</price-amazon>
<price-bn>39.95</price-bn>
</book-with-prices>
</books-with-prices>


可以看到这种查询语句和SQL十分相似,区别就是数据的来源和去向都只是XML。
下面是加入了条件得查询:


for $b in document("books.xml")/bib/book
return
if (count($b/author) <= 2) then $b
else <book> { $b/@*, $b/title, $b/author[position() <= 2], <et-al/>,
$b/publisher, $b/price } </book>

XQuery支持很多的操作符和函数,你也可以定制自己的函数。

在网站:

http://www.w3.org/XML/Query/

可以找到更多的关于XQuery的资料

在网址:

http://www.w3.org/XML/Query#implementations

提供了大量XQuery提供商的相关资料

各个提供商的链接

一些XQuery的好的实现:

Mark Logic's Content Interaction Server

Combines a search engine with a database to produce a content database. Scales past a Terabyte. For learning, you can get a free, non-expiring Community Licensed version.

BEA's Liquid Data

Lets you write a single query to access data from Web services, relational databases, flat files, XML files, applications, and Web sites. Time-limited evaluation license.

Ipedo's XML Intelligence Platform

Provides access to XML and relational data sources for Enterprise Information Integration. Time-limited evaluation license.

X-Hive

A native XML database. Includes an online demo. Time-limited evaluation license.

Other interesting XQuery implementations.

Qizx/open

A Java-based, open source XQuery engine.

IPSI-XQ

A self-described "prototype" engine. Free for non-commercial use.

Qexo

Open source under a license similar to the GPL, managed by Per Bothner.

Saxon

Open source from Michael Kay, with a commercial variety from Saxonica Limited.


同时,在xquery网站上提供了一个BumbleBee供下载,它可以测试各种xquery引擎的性能。

http://www.xquery.com/bumblebee/download.html


参考文章:
http://www.oracle.com/technology/oramag/oracle/03-may/o33devxml.html

http://publishblog.blogchina.com/blog/tb.b?diaryID=1169160

http://www.yesky.com/429/1952429.shtml


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
46.875ms