ÍøÂçѧԺ 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 ä¯ÀÀ:1977 :: ::

Selecting and Filtering Elements
Ñ¡ÔñºÍ¹ýÂËÔªËØ

As we have seen in the previous chapters, we are selecting and filtering elements with either a Path expression or with a FLWOR expression.
ÈçͬÔÚǰ¼¸ÕÂÀïËù¿´µ½µÄÄÇÑù£¬ÎÒÃÇʹÓ÷¾¶±í´ïʽ»òFLWOR±í´ïʽÀ´Ñ¡ÔñºÍ¹ýÂËÔªËØ¡£

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

for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
  • for - (optional) binds a variable to each item returned by the in expression
    for -£¨¿ÉÑ¡µÄ£©¸ø´Ó “in ±í´ïʽ” ÄÚ·µ»ØÃ¿Ò»¸öÏî°ó¶¨Ò»¸ö±äÁ¿
  • let - (optional)
    let -£¨¿ÉÑ¡µÄ£©
  • where - (optional) specifies a criteria
    where -£¨¿ÉÑ¡µÄ£©Ö¸¶¨ÁËÒ»¸ö±ê×¼
  • order by - (optional) specifies the sort-order of the result
    order by - £¨¿ÉÑ¡µÄ£©Ö¸¶¨Á˽á¹ûµÄÅÅÁдÎÐò
  • return - specifies what to return in the result
    return - Ö¸¶¨ÁËÔÚ½á¹ûÖзµ»ØµÄÄÚÈÝ

The for Clause
For ×Ó¾ä

The for clause binds a variable to each item returned by the in expression. The for clause results in iteration. There can be multiple for clauses in the same FLWOR expression.
¸ø´Ó “in ±í´ïʽ” Öзµ»ØÃ¿Ò»¸öÏî°ó¶¨Ò»¸ö±äÁ¿¡£For×Ó¾ä»áµ¼ÖÂÖØ¸´¡£ÔÚÏàͬµÄFLWOR±í´ïʽÖУ¬¿ÉÒÔ°üº¬¶à¸öfor×Ӿ䡣

To loop a specific number of times in a for clause, you may use the to keyword:
ÔÚfor×Ó¾äÖжÔÒ»¸öÖ¸¶¨µÄÊý×Ö½øÐжà´ÎÑ­»·£¬Äã¿ÉÒÔʹÓùؼü´Ê “ to ”£º

for $x in (1 to 5)
return <test>{$x}</test>

Result:
½á¹û£º

<test>1</test>
<test>2</test>
<test>3</test>
<test>4</test>

<test>5</test>

The at keyword can be used to count the iteration:
¹Ø¼ü×Ö “at” ¿ÉÓÃÓÚ¼ÆËãÖØ¸´´ÎÊý£º

for $x at $i in doc("books.xml")/bookstore/book/title
return <book>{$i}. {data($x)}</book>

Result:
½á¹ûÈçÏ£º

<book>1. Everyday Italian</book>
<book>2. Harry Potter</book>
<book>3. XQuery Kick Start</book>

<book>4. Learning XML</book>

It is also allowed with more than one in expression in the for clause. Use comma to separate each in expression:
ÔÚfor×Ó¾äÖУ¬ÔÊÐí³öÏÖÒ»Ïî»ò¶àÏî “in ±í´ïʽ”£»Ê¹ÓöººÅ½«±í´ïʽÖеÄÿÏî·Ö¿ª£º

for $x in (10,20), $y in (100,200)
return <test>x={$x} and y={$y}</test>

Result:
½á¹ûÈçÏ£º

<test>x=10 and y=100</test>
<test>x=10 and y=200</test>
<test>x=20 and y=100</test>
<test>x=20 and y=200</test>

The let Clause
Let ×Ó¾ä

The let clause allows variable assignments and it avoids repeating the same expression many times. The let clause does not result in iteration.
Let ×Ó¾äÔÊÐí±äÁ¿·ÖÅ䣬ĿµÄÊÇΪÁ˱ÜÃâ¶à´ÎÖØ¸´Ö´ÐÐÏàͬµÄ±í´ïʽ¡£Let×Ӿ䲻»áÒýÆð·´¸´£º

let $x := (1 to 5)
return <test>{$x}</test>

Result:
½á¹ûÈçÏ£º

<test>1 2 3 4 5</test>

The where Clause
Where ×Ó¾ä

The where clause is used to specify one or more criteria for the result:
Where ×Ó¾äµÄ×÷ÓÃÊǸø½á¹ûÖ¸¶¨Ò»¸ö»ò¶à¸ö±ê×¼¡£

where $x/price>30 and $x/price<100

The order by Clause
order by ×Ó¾ä

The order by clause is used to specify the sort order of the result. Here we want to order the result by category and title:
order by ×Ó¾äÓÃÓÚΪÊä³öµÄ½á¹ûÖ¸¶¨ÅÅÁеĴÎÐò¡£ÔÚÕâÀÎÒÃÇÏ£ÍûʹÓÓĿ¼”ºÍ“±êÌâ”¶Ô½á¹û½øÐÐÅÅÁУº

for $x in doc("books.xml")/bookstore/book
order by $x/@category, $x/title
return $x/title

Result:
½á¹ûÈçÏ£º

<title lang="en">Harry Potter</title>

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

The return Clause
Return ×Ó¾ä

The return clause specifies what is to be returned.
Return ×Ó¾äÖ¸¶¨ÁË·µ»ØµÄÖµ¡£

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

Result:
½á¹ûÈçÏ£º

<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>

ÆÀÂÛ (0) All