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

w3pop.com :: ÍøÂçѧԺ :: Xquery :: XQuery FLWOR ±í´ïʽ

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

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

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

XQuery FLWOR ±í´ïʽ


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

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

We will use the "books.xml" document in the examples below (same XML file as in the previous chapter).
ÏÂÊö°¸ÀýÖУ¬ÎÒÃÇ»áÓõ½ "books.xml" Îĵµ£¨ÉÏÒ»ÕÂʹÓõÄXMLÎļþ£©¡£

View the "books.xml" file in your browser.
ÔÚÄãµÄä¯ÀÀÆ÷ÖÐä¯ÀÀ“books.xml”¡£


How to Select Nodes From "books.xml" With FLWOR
ÔõÑùʹÓÃFLWOR±í´ïʽ´Ó"books.xml"ÎļþÖÐÑ¡Ôñ½Úµã

Look at the following path expression:
ÏÈ¿´¿´ÏÂÃæµÄ·¾¶±í´ïʽ£º

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

The expression above will select all the title elements under the book elements that are under the bookstore element that have a price element with a value that is higher than 30.
ÉÏÊö±í´ïʽ½«Ñ¡Ôñ bookstore ÔªËØÏ嵀 book ÔªËØÏµÄËùÓÐprice ÔªËØÖµ´óÓÚ30µÄ title ÔªËØ¡£

The following FLWOR expression will select exactly the same as the path expression above:
ÏÂÊöFLWOR±í´ïʽºÍÉÏÊö·¾¶±í´ïʽѡÔñµÄÖµÏàͬ£º

for $x in doc("books.xml")/bookstore/book
where $x/price>30
return $x/title

The result will be:
½á¹ûÈçÏ£º

<title lang="en">XQuery Kick Start</title>

<title lang="en">Learning XML</title>

With FLWOR you can sort the result:
Äã¿ÉÒÔʹÓÃFLWOR¶Ô½á¹û½øÐзÖÀàÅÅÐò£º

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

FLWOR is an acronym for "For, Let, Where, Order by, Return".
FLWORÊÇ "For¡¢Let¡¢Where¡¢Order by¡¢Return" µÄÊ××ÖĸËõд¡£

The for clause selects all book elements under the bookstore element into a variable called $x.
For×Ó¾ä°Ñ bookstore ÔªËØÖеÄËùÓÐ book ÔªËØ·¢Ë͵½ÃûΪ $x µÄ±äÁ¿ÄÚ¡£

The where clause selects only book elements with a price element with a value greater than 30.
Where ×Ó¾ä½öÑ¡ÔñpriceÔªËØÖµ¸ßÓÚ30µÄbookÔªËØ¡£

The order by clause defines the sort-order. Will be sort by the title element.
order by ×Ӿ䶨ÒåÁË“ ·ÖÀàÃüÁî ”¡£¸ù¾Ý title ÔªËØ½øÐзÖÀà¡£

The return clause specifies what should be returned. Here it returns the title elements.
Return ×Ó¾äÖ¸¶¨ÁË·µ»ØµÄÊý¾Ý¡£ÕâÀï·µ»ØµÄÊÇ title ÔªËØ¡£

The result of the XQuery expression above will be:
ÉÏÊö XQuery ±í´ïʽµÄ½á¹ûÈçÏ£º

<title lang="en">Learning XML</title>

<title lang="en">XQuery Kick Start</title>

ÆÀÂÛ (0) All