xquery使用函数从xml文档中提取数据。doc()函数用来打开文档
doc(“books.xml“)
eg:
doc("books.xml")/bookstore/book/title
doc("books.xml")/bookstore/book[price<30]
FLWOR表达式 For, Let, Where, Order by, Return
doc(“books.xml“)
eg:
doc("books.xml")/bookstore/book/title
doc("books.xml")/bookstore/book[price<30]
FLWOR表达式 For, Let, Where, Order by, Return
for $x in doc("books.xml")/bookstore/book where $x/price>30 return $x/title
其效果等同于doc("books.xml")/bookstore/book[price<30]
添加排序功能:
for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title
比较运算符
1. General comparisons: =, !=, <, <=, >, >=
2. Value comparisons: eq, ne, lt, le, gt, ge
for子句:循环指定次数 for $x in (1 to 5)
利用at计算循环次数 for $x at $i in doc("books.xml")/bookstore/book/title
循环嵌套 for $x in (10,20), $y in (100,200)
return <test>x={$x} and y={$y}</test>
$y是内循环,参考结果:<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 $x := (1 to 5) return <test>{$x}</test>
结果: <test>1 2 3 4 5</test>
where子句:指定过滤条件
where $x/price>30 and $x/price<100
order by 子句:指定结果的排序
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子句: 返回结果
xquery函数介绍页面(w3school提供)
http://www.w3schools.com/xquery/xquery_functions.asp