eXist Xquery Examples
Copyright@ ♂猜猜♂. 2005. All rights reserved
以下 ex:> 代表输入表达式 re:> 表示输出结果
_ -- _ _ 插入 [ _ _ 使用 exist _ _ 的 xquery _ _ 数据库检索工具 ] _ _ 说明 -- _
** [ 基础操作 ] **
** 1 ** ** 条件表达式 **
ex:>if (3 < 4) then "yes!" else "no!"
re:>yes!
ex:>
for $x in (-1.5, 0.4, 1.7)
return
1<amount>
2
3{
4
5if ($x < 0)
6
7then concat("(", -$x, ")")
8
9else $x
10
11} </amount>
re:>
1<amount>(1.5)</amount>
1<amount>0.4</amount>
1<amount>1.7</amount>
说明:注意第一行的输出和其它两行有所不同
-$x 对 $x 取反
** 2 ** ** 定义本地变量 **
ex:>let $x := 5 let $y := 6 return 10*$x+$y
re:>56
** 3 ** ** 使用 "," ** ** ,其功能相当于数组 **
ex:>3,4,5
re:>3
4
5
扩展加入其它语句
ex:>3,4,5,let $t:=56 return $t
re:>3
4
5
56
** 4 ** ** 在 return ** ** 中使用 {} {expression} ** ** 执行出来表达式的结果 **
ex:>
let $i := 2 return
let $r :=
1<em>Value </em>
return
1<p>{$r} of 10*{$i} is {10*$i}.</p>
re:>
1<p><em>Value </em> of 10*2 is 20.</p>
** 5 ** ** 简单循环语句 **
ex:>for $x in (1 to 3) return ($x,10+$x)
re:>1
11
2
12
3
13
ex:>for $speech in document("/db/tonybooks/Catalog.xml")//categoryName
return {$speech}
re:> 略
ex:>
1<html>{
2
3let $book := document("mybook.xml")/book
4
5for $ch in $book/chapter
6
7return <h2>{$ch/title)</h2>
8
9}</html>
re:> 略
** 6 ** ** 使用 text(), ** ** 返回内容 **
ex:>let $book:=document("examples.xml")/example-queries/query
return $book/code/text()
ex:>let $doc := document("examples.xml")//query/code/text()
return $doc
[ 函数的定义与调用 ]
ex:>
declare namespace jnb = "http://ociweb.com/jnb";
declare variable $jnb:pi as xs:decimal { 3.1416 };
declare function jnb:fib($i as xs:integer) as xs:integer {
if ($i = 0 or $i = 1)
then 1
else jnb:fib($i - 1) + jnb:fib($i - 2)
};
jnb:fib(3), jnb:fib(4), jnb:fib(5), $jnb:pi
re:>
3
5
8
3.1416
[ 节点和节点类型 ]
一下两个作用相同
ex:>
1<greeting from="weiqi">Hello, World!</greeting>
re:>
1<greeting from="weiqi">Hello, World!</greeting>
ex:> document {
element { "greeting" } {
attribute { "from " } { "weiqi" },
"Hello, World!"
}
}
re:>
1<greeting from="weiqi">Hello, World!</greeting>
[flwor 表达式 / 数据库检索命令 ]
标准类型 for, let, where, order by, return
** 1 for ** ** 语句 **
ex:>
for $x in (1, 2, 3)
return
1<number>{ $x }</number>
re:>
1<number>1</number>
1<number>2</number>
1<number>3</number>
** 2 let ** ** 语句 **
ex:>
let $a := (1, 2, 3)
return
1<numbers>{ $a }</numbers>
re:>
1<numbers>1 2 3</numbers>
** 3 where ** ** 语句 **
ex:>
for $x in (1, 2, 3)
where $x >= 2
return
1<number>{ $x }</number>
re:>
1<number>2</number>
1<number>3</number>
** 4 order by ** ** 语句 **
ex:>
for $x in (
1<greeting></greeting>
,
1<greeting from="weiqi"></greeting>
,
1<greeting from="brian"></greeting>
)
order by $x/@from ascending empty least
return $x
re:>
1<greeting></greeting>
1<greeting from="brian"></greeting>
1<greeting from="weiqi"></greeting>
说明 order by 有以下几个属性:
descending , ascending , empty greatest , empty least
分别测试一下
[ 数量操作 ] 该操作非常有用该操作非常有用
** 1 some ** ** 和 every ** ** 例子 **
ex:>some $x in (1, 2, 3) satisfies $x >= 2
re:>true
ex:>every $x in (1, 2, 3) satisfies $x >= 2
re:>false
ex:>some $x in (1, 2, 3), $y in (3, 4, 5) satisfies $x = $y
re:>true
ex:>every $x in (1, 2, 3), $y in (3, 4, 5) satisfies $x = $y
re:>false
** 2 count($a) **
ex:>let $a:=collection("tonybooks")//category
return count($a)
re:> 返回 40 个 1
ex:>for $a in collection("tonybooks")//category
return count($a)
re:> 返回 40
区别以上例子
** 3 ** ** 多重循环语句 **
ex:>
for $c in customers
for $o in orders
where $c.cust_id=$o.cust_id and $o.part_id="xx"
return $c.name
以上语句等价于 sql 语句
select customers.name from customers, orders
where customers.cust_id=orders.cust_id and orders.part_id="xx"
ex:>
for $book in bib.xml//book
let $title := $book/title
where $book/publisher = 'Addison-Wesley'
return
1<bookinfo>{ $title }</bookinfo>
[xpath 例子 ]
greetings.xml:
1<greetings>
2<greeting from="weiqi">Nihao!</greeting>
3<greeting from="brian">Hi!</greeting>
4<greeting from="luc">Bonjour!</greeting>
5</greetings>
ex:>doc("greetings.xml")/greetings
re:>
1<greetings>
2<greeting from="weiqi">Nihao!</greeting>
3<greeting from="brian">Hi!</greeting>
4<greeting from="luc">Bonjour!</greeting>
5</greetings>
ex:>doc("greetings.xml")//greeting
re:>
1<greeting from="weiqi">Nihao!</greeting>
1<greeting from="brian">Hi!</greeting>
1<greeting from="luc">Bonjour!</greeting>
说明:注意以上两个例子的区别 /greetings 和 //greetings ,
前者表示从根节点开始逐级遍历,后者表示任何级的节点都遍历
ex:>doc("greetings.xml")//greeting[@from="weiqi"]
re:>
1<greeting from="weiqi">Nihao!</greeting>
说明: @from 用来引用节点中的属性名称
引用节点名直接输入例如: greeting 或者 greetings 就可以
ex:>doc("greetings.xml")//greeting/@from
re:>from="weiqi" from="brian" from="luc"
ex:>doc("greetings.xml")//greeting[1]
re:>
1<greeting "[n]="" $a="" $a)="" $b="" $c="" $d="" (2,="" (3,4,3,4).="" (count($a),="" 0)="" 1,="" 4,="" :="()" \--="" as="" because="" children(<p="" count($b),="" count($c),="" count($d))="" evaluates="" from="weiqi>Nihao!</greeting>
2
3说明:使用 " is="" let="" return="" same="" the="" to="" 待查:="" 节点名="" 表示输出第几个节点内容="" 错误="">This is <em>very</em> cool.)
4
5returns this sequence of 3 values:
6
7"This is ", <em>very</em>, " cool."
8
9================ 以下为未整理部分 =============
10
11[Modules]
12
13You can put functions and variables declarations into library modules. A library module is a file that starts with a module namespace declaration and contains declarations of functions, variables, etc., but does not contain an expression at the end. A main module contains an expression at the end. Both library modules and main modules can import other library modules to access variables and functions declared in the imported module.
14
15(: libfib.xq :)
16
17module namespace jnb = "http://ociweb.com/jnb";
18
19declare function jnb:fib($i as xs:integer) as xs:integer {
20
21if ($i <= 1)
22
23then 1
24
25else jnb:fib($i - 1) + jnb:fib($i - 2)
26
27};
28
29(: mainfib.xq: )
30
31import module namespace jnb = "http://ociweb.com/jnb" at "libfib.xq";
32
33jnb:fib(6)
34
35[weiqi@gao] $ xquery mainfib.xq # Saxon
36
3713
38
39Qexo supports compiled modules. A library module is compiled to a Java class whose name is derived from the module namespace URI. A main module is compiled to a Java class whose name is derived from the module file name.
40
41[weiqi@gao] $ qexo -C libfib.xq # Compile to Java class com.ociweb.jnb
42
43(compiling libfib.xq)
44
45[weiqi@gao] $ qexo --main -C mainfib.xq # Compile to Java class mainfib
46
47(compiling mainfib.xq)
48
49[weiqi@gao] $ java mainfib
50
5113
52
53\-------------------------------------------------------------------------
54
55Type Specification
56
57XQuery is a strongly typed programming language. Like Java and C#, for example, it's a mix of static typing (type consistency checked at compile-time) and dynamic typing (run-time type tests). However, the types in XQuery are different from the classes familiar from object-oriented programming. Instead, it has types to match XQuery's data model, and it allows you to import types form XML Schema.
58
59if ($child instance of element section)
60
61then process-section($child)
62
63else ( ) {--nothing--}
64
65This invokes the process-section function if the value of $child is an element whose tag name is section. XQuery has a convenient typeswitch shorthand for matching a value against a number of types. The following converts a set of tag names to a different set.
66
67define function convert($x) {
68
69typeswitch ($x)
70
71case element para return <p>{process-children($x)}</p>
72
73case element emph return <em>{process-children($x)}</em>
74
75default return process-children($x)
76
77}
78
79define function process-children($x) {
80
81for $ch in children($x)
82
83return convert($ch)
84
85}
86
87Hello,
88
89I am a beginner in Xquery.
90
91In my schema I have element which has IDREF attribute.I want to extract id value to which points and check whether that value is equivalent to some value.
92
93eg:
94
95<author emailid="[email protected]">
96<fname>smith</fname>
97<mi>j</mi>
98<lname>jen</lname>
99</author>
100<paper emailid="[email protected]" paperid="10">
101<mainathr>SHARMA</mainathr>
102<title>ABC</title>
103</paper>
104<paper emailid="[email protected]" paperid="10">
105<mainathr>SHARMA</mainathr>
106<title>ABC</title>
107</paper>
108
109let $id := "[email protected]",
110
111for $paper in $doc/PAPER[@EMAILID = $id],
112
113for $author in $doc/AUTHOR[@EMAILID = $id]
114
115return
116
117<result paperid="{$paper/@PAPERID}">
118
119{$author/FNAME}
120
121<result>
122
123for $paper in $doc/PAPER,
124
125for $author in $doc/AUTHOR
126
127where $paper/@EMAILID = $auther/$EMAILID
128
129return
130
131<result paperid="{$paper/@PAPERID}">
132
133{$author/FNAME}
134
135<result>
136
137如上例程序,在 xquery 中引用节点和节点属性名称的时候稍有不同。
138
139节点 paper 属性 @paper
140
141I am completely new to XQuery..
142
143I have the following which I would like to run on an .xml file...
144
145for $book in bib.xml//book
146
147let $title := $book/title
148
149where $book/publisher = 'Addison-Wesley'
150
151return
152
153<bookinfo>
154
155{ $title }
156
157</bookinfo>
158
159and the bib.xml file
160
161<bib>
162<book year="1994">
163<title>TCP/IP Illustrated</title>
164<author><last>Stevens</last><first>W.</first></author>
165<publisher>Addison-Wesley</publisher>
166<price> 65.95</price>
167</book>
168<book year="1992">
169<title>Advanced Programming in the Unix environment</title>
170<author><last>Stevens</last><first>W.</first></author>
171<publisher>Addison-Wesley</publisher>
172<price>65.95</price>
173</book>
174<book year="2000">
175<title>Data on the Web</title>
176<author><last>Abiteboul</last><first>Serge</first></author>
177<author><last>Buneman</last><first>Peter</first></author>
178<author><last>Suciu</last><first>Dan</first></author>
179<publisher>Morgan Kaufmann Publishers</publisher>
180<price>39.95</price>
181</book>
182<book year="1999">
183<title>The Economics of Technology and Content for Digital TV</title>
184<editor>
185<last>Gerbarg</last><first>Darcy</first>
186<affiliation>CITI</affiliation>
187</editor>
188<publisher>Kluwer Academic Publishers</publisher>
189<price>129.95</price>
190</book>
191</bib>
192
193\-----------------------------------------------
194
195最后附上一个好的教材:
196
197**http://monetdb.cwi.nl/XQuery/Demo/index.html**
198**http://www.xml.com/pub/a/2005/03/02/xquery.html**
199**http://www.xml.com/pub/a/2005/03/23/xquery-2.html**
200**http://www.w3schools.com/xquery/default.asp**
201
202
203Copyright@ ♂猜猜♂. 2005. All rights reserved</result></result></result></result></greeting>