当前位置: 首页 > 网络学院 > XML相关教程 > Xquery > XQuery 选择和过滤

Xquery
XQuery 介绍
XQuery 实例
XQuery FLWOR 表达式
XQuery FLWOR + HTML
XQuery 术语
XQuery 语法
XQuery 添加元素和属性
XQuery 选择和过滤
XQuery 函数
XQuery 摘要
XQuery 参考

Xquery 中的 XQuery 选择和过滤


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 598 ::
收藏到网摘: n/a

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

登陆 | 还没注册?