Professional Documents
Culture Documents
07 XQuery
07 XQuery
Ni dung
Gii thiu
V d
Biu thc FLWOR
XQuery
Gii thiu
V d books.xml
V d books.xml (tip)
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
doc("books.xml")/bookstore/book/title
trch rt ra
</bookstore>
5
<title
<title
<title
<title
lang="en">Everyday Italian</title>
lang="en">Harry Potter</title>
lang="en">XQuery Kick Start</title>
lang="en">Learning XML</title>
6
04/04/2013
v d:
doc("books.xml")/bookstore/book[price<30]
for $x in doc("books.xml")/bookstore/book
where $x/price>30
return $x/title
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
Sp xp kt qu:
for $x in doc("books.xml")/bookstore/book/title
order by $x
return $x
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{$x}</li>
}
</ul>
<ul>
<li><title
<li><title
<li><title
<li><title
</ul>
lang="en">Everyday Italian</title></li>
lang="en">Harry Potter</title></li>
lang="en">Learning XML</title></li>
lang="en">XQuery Kick Start</title></li>
10
Thut ng XQuery
<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{data($x)}</li>
}
</ul>
Nt
Gi tr nguyn t
Item
Mi quan h gia cc nt
<ul>
<li>Everyday Italian</li>
<li>Harry Potter</li>
<li>Learning XML</li>
<li>XQuery Kick Start</li>
</ul>
11
12
04/04/2013
Nt
C 7 loi nt trong XQuery
Gi tr nguyn t
phn t
thuc tnh
text
khng gian tn
ch th x l
comment
ti liu (gc)
Item
l cc gi tr nguyn t hoc cc nt
14
Mi quan h gia cc nt
C php XQuery
Nt cha
Quy tc c php c bn
Biu thc iu kin
Biu thc so snh
Nt con
mi nt c th c 0, 1 hoc nhiu nt con
Nt anh em
c cng nt cha
Nt t tin
nt cha, nt cha ca nt cha
Nt hu du
nt con, nt con ca nt con
15
16
if then else
v d
for $x in doc("books.xml")/bookstore/book
return if ($x/@category="CHILDREN")
then <child>{data($x/title)}</child>
else <adult>{data($x/title)}</adult>
kt qu:
<adult>Everyday Italian</adult>
<child>Harry Potter</child>
<adult>Learning XML</adult>
<adult>XQuery Kick Start</adult>
:ch thch:
17
18
04/04/2013
<title
<title
<title
<title
lang="en">Everyday Italian</title>
lang="en">Harry Potter</title>
lang="en">Learning XML</title>
lang="en">XQuery Kick Start</title>
$bookstore//book/@q gt 10
19
20
<html>
<body>
<h1>Bookstore</h1>
<ul>
{
for $x in doc("books.xml")/bookstore/book
order by $x/title
return <li>{data($x/title)}. Category: {data($x/@category)}</li>
}
</ul>
</body>
</html>
<html>
<body>
<h1>Bookstore</h1>
<ul>
<li>Everyday Italian. Category: COOKING</li>
<li>Harry Potter. Category: CHILDREN</li>
<li>Learning XML. Category: WEB</li>
<li>XQuery Kick Start. Category: WEB</li>
</ul>
</body>
</html>
21
22
<html>
<body>
<h1>Bookstore</h1>
<ul>
<li class="COOKING">Everyday Italian</li>
<li class="CHILDREN">Harry Potter</li>
<li class="WEB">Learning XML</li>
<li class="WEB">XQuery Kick Start</li>
</ul>
<ul>
{
for $x in doc("books.xml")/bookstore/book
order by $x/title
return <li class="{data($x/@category)}">{data($x/title)}</li>
}
</ul>
</body>
</html>
</body>
</html>
23
24
04/04/2013
Tm kim v lc phn t
Mnh for
for $x in (1 to 5)
return <test>{$x}</test>
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
<test>1</test>
<test>2</test>
<test>3</test>
<test>4</test>
<test>5</test>
Dng to
xc nh s
ln lp
for $x at $i in doc("books.xml")/bookstore/book/title
return <book>{$i}. {data($x)}</book>
<book>1.
<book>2.
<book>3.
<book>4.
Everyday Italian</book>
Harry Potter</book>
XQuery Kick Start</book>
Learning XML</book>
Dng t kha at
m vng lp
25
26
Mnh let
C th c nhiu
biu thc, phn
cch nhau bi du
phy
for $x in (10,20), $y in (100,200)
return <test>x={$x} and y={$y}</test>
<test>x=10
<test>x=10
<test>x=20
<test>x=20
and
and
and
and
y=100</test>
y=200</test>
y=100</test>
y=200</test>
27
28
Mnh where
Mnh order by
Xc nh th t sp xp kt qu
for $x in doc("books.xml")/bookstore/book
order by $x/@category, $x/title
return $x/title
<title
<title
<title
<title
29
lang="en">Harry Potter</title>
lang="en">Everyday Italian</title>
lang="en">Learning XML</title>
lang="en">XQuery Kick Start</title>
30
04/04/2013
Mnh return
Hm XQuery
Xc nh kt qu tr v
for $x in doc("books.xml")/bookstore/book
return $x/title
<title
<title
<title
<title
lang="en">Everyday Italian</title>
lang="en">Harry Potter</title>
lang="en">XQuery Kick Start</title>
lang="en">Learning XML</title>
31
32
V d gi hm
Mt li gi hm c th xut hin ch
mt biu thc c th xut hin
C php
declare function prefix:function_name($parameter AS datatype)
AS returnDatatype
{
...function code here...
}
Trong mt phn t
<name>{upper-case($booktitle)}</name>
doc("books.xml")/bookstore/book[substring(title,1,5)='Harry']
Goi hm:
<minPrice>{local:minPrice($book/price,$book/discount)}</minPrice>
33
34
Li hay p
35
36