XQuery পছন্দ করা এবং ফিল্টার
- 上一页 XQuery 添加
- 下一页 XQuery ফাংশন
XML instance document
We will continue to use this "books.xml" document in the following examples (the same XML file as used in the previous chapters).
Select and filter elements
As seen in the previous chapters, we use path expressions or FLWOR expressions to select and filter elements.
Please see the following FLWOR expression:
for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title
- for
- (Optional) Bind a variable to each item returned by an in expression
- let
- (Optional)
- where
- (Optional) Set a condition
- order by
- (Optional) Set the order of the results
- return
- Specify the content to be returned in the result
for statement
The for statement binds a variable to each item returned by an in expression. The for statement can produce iteration. Multiple for statements can exist in the same FLWOR expression.
If you want to loop a specified number of times in a for statement, you can usekeywords to :
for $x in (1 to 5) return <test>{$x}</test>
পরিণাম:
<test>1</test> <test>2</test> <test>3</test> <test>4</test> <test>5</test>
关键词 at 可用于计算迭代:
for $x at $i in doc("books.xml")/bookstore/book/title return <book>{$i}. {data($x)}</book>
পরিণাম:
<book>1. Everyday Italian</book> <book>2. Harry Potter</book> <book>3. XQuery Kick Start</book> <book>4. Learning XML</book>
在 for 语句中同样允许多个 in 表达式。请使用逗号来分割每一个 in 表达式:
for $x in (10,20), $y in (100,200) return <test>x={$x} and y={$y}</test>
পরিণাম:
<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>
let 语句
let 语句可完成变量分配,并可避免多次重复相同的表达式。let 语句不会导致迭代。
let $x := (1 to 5) return <test>{$x}</test>
পরিণাম:
<test>1 2 3 4 5</test>
where 语句
where 语句用于为结果设定一个或多个条件(criteria)。
where $x/price>30 and $x/price<100
order by 语句
order by 语句用于规定结果的排序次序。在这里,我们要根据 category 和 title 来对结果进行排序:
for $x in doc("books.xml")/bookstore/book order by $x/@category, $x/title return $x/title
পরিণাম:
<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>
return 语句:
return 语句规定要返回的内容。
for $x in doc("books.xml")/bookstore/book return $x/title
পরিণাম:
<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>
- 上一页 XQuery 添加
- 下一页 XQuery ফাংশন