ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: Xquery :: XQuery ʵÀý

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

Xquery
XQuery ½éÉÜ
XQuery ʵÀý
XQuery FLWOR ±í´..
XQuery FLWOR + H..
XQuery ÊõÓï
XQuery Óï·¨
XQuery Ìí¼ÓÔªËØº..
XQuery Ñ¡ÔñºÍ¹ýÂ..
XQuery º¯Êý
XQuery ÕªÒª
XQuery ²Î¿¼

XQuery ʵÀý


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-29 ÐÞ¸Ä:2007-08-22 ä¯ÀÀ:2791 :: ::

Let's try to learn some basic XQuery syntax by looking at an example.
ÈÃÎÒÃÇͨ¹ýÏÂÃæÕâ¸ö°¸ÀýÀ´Ñ§Ï°Ò»Ð©¹ØÓÚXQuery µÄ»ù±¾Óï·¨¡£


The XML Example Document
XML °¸ÀýÎĵµ

We will use the following XML document in the examples below.
ÎÒÃǽ«ÔÚÏÂÃæµÄ°¸ÀýÖÐʹÓÓbooks.xml”Îĵµ£º

"books.xml":
“books.xml”ÎļþÄÚÈÝÈçÏ£º

<?xml version="1.0" encoding="ISO-8859-1"?>
<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>

View the "books.xml" file in your browser.
ÔÚä¯ÀÀÆ÷ÖÐä¯ÀÀ“books.xml”Îļþ¡£


How to Select Nodes From "books.xml"?
ÈçºÎ´Ó"books.xml"ÎļþÖÐÑ¡Ôñ½Úµã£¿

Functions
º¯Êý

XQuery uses functions to extract data from XML documents.
XQuery ʹÓú¯Êý´ÓXMLÎĵµÖлñÈ¡Êý¾Ý¡£

The doc() function is used to open the "books.xml" file:
doc() º¯ÊýÓÃÓÚ´ò¿ª"books.xml"Îļþ£º

doc("books.xml")

Path Expressions
·¾¶±í´ïʽ

XQuery uses path expressions to navigate through elements in an XML document.
XQuery ͨ¹ý·¾¶±í´ïʽ²Ù×÷XMLÎĵµÖеÄÔªËØ¡£

The following path expression is used to select all the title elements in the "books.xml" file:
ÏÂÊö·¾¶±í´ïʽÓÃÓÚÔÚ"books.xml"ÎļþÖÐÑ¡Ôñ title ÔªËØ£º

doc("books.xml")/bookstore/book/title

(/bookstore selects the bookstore element, /book selects all the book elements under the bookstore element, and /title selects all the title elements under each book element)
£¨/bookstore Ñ¡Ôñ bookstoreÔªËØ£»/book Ñ¡Ôñ bookstore ÔªËØÏµÄËùÓÐ book ÔªËØ£»/title Ñ¡Ôñÿ¸öbook ÔªËØÏµÄËùÓÐtitleÔªËØ¡£

The XQuery above will extract the following:
ÉÏÊöµÄXQuery»á»ñÈ¡ÏÂÊöÄÚÈÝ£º

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

Predicates
Ìõ¼þνÓïÏî

XQuery uses predicates to limit the extracted data from XML documents.
XQuery ʹÓÃÌõ¼þνÓïÏî¸ø´ÓXMLÎļþÖÐժȡµÄÊý¾Ý¸½¼ÓÒ»¸öÌõ¼þ¡£

The following predicate is used to select all the book elements under the bookstore element that have a price element with a value that is less than 30:
ÏÂÃæµÄÌõ¼þνÓïÏîÊÇÓÃÀ´Ñ¡Ôñ bookstore ÔªËØÏ price ÔªËØÖµÐ¡ÓÚ30µÄËùÓÐ book ÔªËØµÄ±í´ïʽ£º

doc("books.xml")/bookstore/book[price<30]

The XQuery above will extract the following:
ÉÏÊöXQueryÓï¾ä»á»ñÈ¡ÏÂÃæµÄÄÚÈÝ£º

<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>

<year>2005</year>
<price>29.99</price>
</book>

ÆÀÂÛ (0) All