Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

04/04/2013

Ni dung
 Gii thiu
 V d
 Biu thc FLWOR

XQuery

 Hin th kt qu lng trong danh


sch HTML
 Thut ng XQuery
 C php XQuery

Nguyn Hng Phng


Email: phuong.nguyenhong@hust.edu.vn
Site: http://is.hut.edu.vn/~phuongnh
B mn H thng thng tin
Vin Cng ngh thng tin v Truyn thng
i hc Bch Khoa H Ni
1

Gii thiu

V d books.xml

 c xy dng da trn biu thc


XPath.
 c h tr bi hu ht CSDL
 L khuyn co ca W3C
 XQuery c thit k truy vn d
liu XML, tng t SQL vi cc bng
 XQuery l ngn ng tm kim v trch
rt cc phn t v thuc tnh t ti liu
XML

<?xml version="1.0" encoding="ISO-8859-1"?>


<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
3

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>

 XQuery s dng hm trch rt d liu


t ti liu XML
 S dng hm doc() m file xml
doc("books.xml")

 S dng biu thc ng dn duyt


qua cc phn t
 v d: chn tt c cc phn t title:

<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

Biu thc FLWOR


 XQuery s dng v t gii hn d liu
c trch rt ra

 Biu thc ng dn:


doc("books.xml")/bookstore/book[price>30]/title

 v d:

 Biu thc FLWOR:

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>

 Cng cho kt qu:


<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>

Hin th kt qu lng trong danh sch HTML

 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>

 FLWOR: For, Let, Where, Order by,


Return

<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

Hin th kt qu lng trong danh sch HTML






<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)

 L nt khng c nt cha v nt con.


 V d:
J K. Rowling
"en"

 Item
 l cc gi tr nguyn t hoc cc nt

 Coi ti liu XML l cy. Nt gc l nt


ti liu
13

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

 mi phn t v thuc tnh c 1 nt cha

 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

Quy tc c php c bn ca XQuery

Biu thc iu kin

 Phn bit ch hoa ch thng


 Phn t, thuc tnh, bin phi l tn
XML hp l
 Gi tr chui k t c th trong cp
nhy n hoc nhy kp
 Bin c t tn bt u bi k t $,
theo sau l tn bin.
 Ch thch c t trong cp du hai
chm :

 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

Biu thc so snh

Thm phn t v thuc tnh vo kt qu

 So snh chung: =, !=, <, <=, >, >=


 So snh gi tr: eq, ne, lt, le, gt, ge
 V d:

 C th thm phn t v thuc tnh vo kt


qu
for $x in doc("books.xml")/bookstore/book/title
order by $x
return $x

 biu thc tr v true nu bt k thuc tnh q


no c gi tr >10
$bookstore//book/@q > 10

 biu thc sau tr v true khi ch c duy nht 1


thuc tnh q c tr v bi biu thc, v gi
tr ca n phi ln hn 10. Nu c nhiu hn 1
q c tr v, th s c li

<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

 Thm phn t HTML v text

20

 Biu thc trn s sinh ra kt qu sau:

<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

 Thm thuc tnh vo phn t HTML

22

 Biu thc trn sinh kt qu sau:


<html>
<body>
<h1>Bookstore</h1>

<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

 Quan st biu thc FLWOR:

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>

 for (ty chn) bind mt bin vi mt item tr v


bi biu thc
 let (ty chn)
 where (ty chn): xc nh tiu chun
 order by (ty chn): xc nh th t sp xp ca
kt qu
 return: xc nh ci g c tr v

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

 Cho php khi to bin v trnh lp


cng biu thc nhiu ln
let $x := (1 to 5)
return <test>{$x}</test>
<test>1 2 3 4 5</test>

y=100</test>
y=200</test>
y=100</test>
y=200</test>

27

28

Mnh where

Mnh order by

 Dng xc nh 1 hoc nhiu tiu chun

 Xc nh th t sp xp kt qu

where $x/price>30 and $x/price<100

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

 C trn 100 hm tch hp sn


 Hm cho gi tr chui, s, so snh ngy
thi gian, thao tc nt v QName, thao tc
tun t, gi tr boolean,...

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>

 C th t nh ngha hm trn XQuery

31

32

V d gi hm

Hm ngi dng nh ngha

 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>

 Trong v t ca mt biu thc ng dn

declare function local:minPrice($p as xs:decimal?,$d as xs:decimal?)


AS xs:decimal?
{
let $disc := ($p * $d) div 100
return ($p - $disc)
}

doc("books.xml")/bookstore/book[substring(title,1,5)='Harry']

 Trong mnh let


let $name := (substring($booktitle,1,4))

Goi hm:
<minPrice>{local:minPrice($book/price,$book/discount)}</minPrice>
33

34

Li hay p

"Hy yu s tht nhng cng bit tha th li lm"


Voltaire

35

36

You might also like