条件与循环
FLWOR(读作 flower)是 XQuery 的核心,对应 SQL 的 SELECT-FROM-WHERE:For 遍历、Let 绑定、Where 过滤、Order 排序、Return 返回。
五个子句
- for $x in 序列:迭代遍历(像 foreach)
- let $x := 值:绑定变量(一次计算复用)
- where 条件:过滤
- order by:排序(ascending/descending)
- return 表达式:对每个结果返回什么
代码示例
(: 查询价格大于 30 的书,按价格降序 :)
for $book in doc("books.xml")//book
where $book/price > 30
order by $book/price descending
return $book/title/text()
(: 带 let 的用法 :)
for $book in doc("books.xml")//book
let $discount := $book/price * 0.9
where $discount < 50
return concat($book/title, " 折后 ", $discount)