w3pop.com :: ÍøÂçѧԺ :: Xquery :: XQuery FLWOR ±í´ïʽ
XQuery FLWOR ±í´ïʽ
::
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”¡£
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
|
The result will be:
½á¹ûÈçÏ£º
<title lang="en">XQuery Kick Start</title> |
With FLWOR you can sort the result:
Äã¿ÉÒÔʹÓÃFLWOR¶Ô½á¹û½øÐзÖÀàÅÅÐò£º
for $x in doc("books.xml")/bookstore/book
|
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> |
ÆÀÂÛ (0)
All