You are on page 1of 64

XPATH & XSLT

Cng ngh XML v ng dng:


Ni dung
XPATH
- Cc biu thc XPATH thng dng
- Axes
XSLT
Ni dung
XPATH
- L cc biu thc dng truy xut n mt hoc
nhiu th trong ti liu XML
- c s dng trong XSLT chuyn i cu trc
ni dung ti liu XML
Document
/
PI
<?xml version=1.0?>
Element
FitnessCenter
Element
Member
Element
Member
Element
Member
Element
Name
Element
FavoriteColor
Text
Jeff
Text
lightgrey
Element
Name
Element
FavoriteColor
Text
David
Text
lightblue
Element
Name
Element
FavoriteColor
Text
Roger
Text
lightyellow
<?xml version="1.0"?>
<FitnessCenter>
<Member>
<Name>Jeff</Name>
<FavoriteColor>lightgrey</FavoriteColor>
</Member>
<Member>
<Name>David</Name>
<FavoriteColor>lightblue</FavoriteColor>
</Member>
<Member>
<Name>Roger</Name>
<FavoriteColor>lightyellow</FavoriteColor>
</Member>
</FitnessCenter>
CU TRC CY CA VN BN XML
Node
Document
/
PI
<?xml version=1.0?>
Element
FitnessCenter
Element
Member
Element
Member
Element
Member
Element
Name
Element
FavoriteColor
Text
Jeff
Text
lightgrey
Element
Name
Element
FavoriteColor
Text
David
Text
lightblue
Element
Name
Element
FavoriteColor
Text
Roger
Text
lightyellow
Document node
Processing Instruction (PI) node
Element nodes
Text nodes
Document
/
PI
<?xml version=1.0?>
Element
FitnessCenter
Element
Member
Element
Member
Element
Member
Element
Name
Element
FavoriteColor
Text
Jeff
Text
lightgrey
Element
Name
Element
FavoriteColor
Text
David
Text
lightblue
Element
Name
Element
FavoriteColor
Text
Roger
Text
lightyellow
With
respect to
this node,
these are
its
children
Document
/
PI
<?xml version=1.0?>
Element
FitnessCenter
Element
Member
Element
Member
Element
Member
Element
Name
Element
FavoriteColor
Text
Jeff
Text
lightgrey
Element
Name
Element
FavoriteColor
Text
David
Text
lightblue
Element
Name
Element
FavoriteColor
Text
Roger
Text
lightyellow
These are
its
descenda
nt nodes
Document
/
PI
<?xml version=1.0?>
Element
FitnessCenter
Element
Member
Element
Member
Element
Member
Element
Name
Element
FavoriteColor
Text
Jeff
Text
lightgrey
Element
Name
Element
FavoriteColor
Text
David
Text
lightblue
Element
Name
Element
FavoriteColor
Text
Roger
Text
lightyellow
This is the
context
node
Document
/
PI
<?xml version=1.0?>
Element
FitnessCenter
Element
Member
Element
Member
Element
Member
Element
Name
Element
FavoriteColor
Text
Jeff
Text
lightgrey
Element
Name
Element
FavoriteColor
Text
David
Text
lightblue
Element
Name
Element
FavoriteColor
Text
Roger
Text
lightyellow
That's
its parent
Document
/
PI
<?xml version=1.0?>
Element
FitnessCenter
Element
Member
Element
Member
Element
Member
Element
Name
Element
FavoriteColor
Text
Jeff
Text
lightgrey
Element
Name
Element
FavoriteColor
Text
David
Text
lightblue
Element
Name
Element
FavoriteColor
Text
Roger
Text
lightyellow
Those are
its
ancestors
Document
/
PI
<?xml version=1.0?>
Element
FitnessCenter
Element
Member
Element
Member
Element
Member
Element
Name
Element
FavoriteColor
Text
Jeff
Text
lightgrey
Element
Name
Element
FavoriteColor
Text
David
Text
lightblue
Element
Name
Element
FavoriteColor
Text
Roger
Text
lightyellow
It has 2
siblings
Document
/
PI
<?xml version=1.0?>
Element
FitnessCenter
Element
Member
Element
Member
Element
Member
Element
Name
Element
FavoriteColor
Text
Jeff
Text
lightgrey
Element
Name
Element
FavoriteColor
Text
David
Text
lightblue
Element
Name
Element
FavoriteColor
Text
Roger
Text
lightyellow
They are
following-
siblings
Document
/
PI
<?xml version=1.0?>
Element
FitnessCenter
Element
Member
Element
Member
Element
Member
Element
Name
Element
FavoriteColor
Text
Jeff
Text
lightgrey
Element
Name
Element
FavoriteColor
Text
David
Text
lightblue
Element
Name
Element
FavoriteColor
Text
Roger
Text
lightyellow
It has no
preceding-
siblings
XPATH
XPATH: C php c bn
/: ng dn tuyt i bt u t node gc ca ti liu
n 1 node c th
<AAA>
<BBB/>
<CCC/>
<DDD>
<CCC/>
</DDD>
</AAA>

- /AAA: nt gc
- /AAA/BBB: nt B l con ca A
- /AAA/DDD/CCC: C con D con A (gc)
//: Nt su bt k
//CCC: nt CCC su bt k
XPATH
XPATH: C php c bn
<AAA>
<BBB>
<CCC/>
<BBB>
<CCC/>
</BBB>
</BBB>
</AAA>

- /AAA/*: Tt c cc nt con trc tip ca AAA
*: Chn tt c cc node vi tn bt k
- /*/BBB: Tt c cc nt B cp th 2
//*: TT C CC NODE
XPATH
XPATH: C php c bn
<AAA>
<BBB/>
<BBB/>
<BBB>
<CCC>
content
</CCC>
</BBB>
<BBB/>
</AAA>

- /AAA/BBB[1]: Nt BBB th 1 (con ca AAA)
[ ]: Truy xut n cc node theo th t no hoc theo ch mc.
Ngoi ra, c th dng ch nh biu thc iu kin chn la node
- /AAA/BBB[2]: Nt BBB th 2 (con ca AAA)
- /AAA/BBB[last()]: Nt BBB cui cng
- /AAA/BBB[CCC=content]: Nt BBB th
3 (c nt con CCC vi ni dung l content )
XPATH
XPATH: C php c bn
<AAA>
<BBB id=b1/>
<BBB id=b2/>
<CCC name=ccc/>
<CCC/>
</AAA>

- /AAA/BBB[@id]:
@: Truy xut n thuc tnh
- /AAA/CCC[@name=ccc]:
- /AAA/CCC[@*]:
Nhng node BBB c thuc tnh id
Nhng node CCC c gi tr thuc tnh
name = ccc
Nhng node CCC c thuc tnh
- /AAA/CCC[not(@*)]: Nhng node CCC KHNG c thuc tnh
XPATH
XPATH: C php c bn
<AAA>
<BBB>
<DDD/>
<DDD/>
<BBB/>
<CCC>
<DDD/>
<CCC/>
</AAA>

- //*[count(DDD)=2]:
count: hm m
- //*[count(*)=1]:
Tt c cc node c ng 2 th con DDD
Tt c cc node c ng 1 th con (tn
th con l g cng c)
XPATH
XPATH: C php c bn
<AAA>
<BBB>
<DDD/>
<BBB/>
<ECC>
<EEE/>
<ECC/>
<CFB/>
</AAA>

- //*[name()=DDD]:
name(): Ly tn th, starts-with(), contains(): x l chui
- //*[starts-with(name(), E)]:
- //*[contains(name(), B)]
Nhng node c tn l DDD
Nhng node c tn bt u bng E
Nhng node m tn c cha B
string-length(): chiu di chui
- //*[string-length(name())=3]
Nhng node c chiu di tn th l 3
XPATH
XPATH: C php c bn
<AAA>
<BBB>
<DDD/>
<DDD>
<EEE/>
</DDD>
</BBB>
</AAA>

Chn ra tt c th con chu ca
BBB ?
child:: : Chn tt c node con ca node hin thi
XPATH
XPATH: Axis dng chn cc node trong mi quan h
vi node hin thi
<AAA>
<BBB>
<DDD/>
<BBB/>
<ECC>
<EEE/>
</ECC>
<CFB/>
</AAA>

- /AAA/child::ECC
/AAA/ECC
- //BBB/child::*
/AAA/BBB/DDD
XPATH
XPATH: Axis

<AAA>
<BBB>
<DDD>
<EEE/>
</DDD>
</BBB>
</AAA>

descendant:: : Tt c cc th con chu

<AAA>
<BBB>
<DDD>
<EEE/>
</DDD>
</BBB>
</AAA>

/AAA/child::* /AAA/descendant::*
descendant-or-self:: = descendant:: + context node
XPATH
XPATH: Axis
parent:: : Th cha
<AAA>
<BBB>
<DDD/>
<BBB/>
<ECC>
<EEE/>
<ECC/>
<CFB/>
</AAA>

- //BBB/parent::*
Th cha ca BBB
- //EEE/parent::ECC
Th cha ca ECC
XPATH
XPATH: Axis

<AAA>
<BBB>
<DDD>
<EEE/>
</DDD>
</BBB>
</AAA>

ancestor:: : Tt c cc th cha ng

<AAA>
<BBB>
<DDD>
<EEE/>
</DDD>
</BBB>
</AAA>

/AAA/BBB/DDD/EEE/parent::* /AAA/BBB/DDD/EEE/ancestor::*
ancestor-or-self:: = ancestor:: + context node
XPATH
XPATH: Axis
following-sibling:: : Tt c cc th em
preceding-sibling:: : Tt c cc th anh

<AAA>
<BBB>
<DDD/>
<EEE/>
<FFF/>
<GGG/>
</BBB>
</AAA>

//EEE/following-sibling::*
<AAA>
<BBB>
<DDD/>
<EEE/>
<FFF/>
<GGG/>
</BBB>
</AAA>


<AAA>
<BBB>
<DDD/>
<EEE/>
<FFF/>
<GGG/>
</BBB>
</AAA>

//EEE/preceding-sibling::*
XPATH
XPATH: Axis
following:: : Tt c cc th c th m xut hin sau th ng ca
th hin hnh
preceding::: Tt c cc th c th ng xut hin trc th m ca thh
//BBB/following::*
<AAA>
<BBB>
<DDD/>
</BBB>
<CCC>
<EEE/>
</CCC>
</AAA>

//EEE/preceding::*
<AAA>
<BBB>
<DDD/>
</BBB>
<CCC>
<EEE/>
</CCC>
</AAA>

XPATH
XPATH: Axis
<AAA>
<BBB>
<DDD/>
</BBB>
<CCC>
<EEE>
<FFF/>
</EEE>
<GGG/>
</CCC>
</AAA>

- //EEE/ancestor::*
- //EEE/descendant::*
- //EEE/following::*
- //EEE/preceding::*
- //EEE/self::*
5 axis trn to thnh 5 phn
hoch ca ti liu xml
Khng overlap nhau
Or ( | ) li s cho kt qu l
tt c cc node ca ti liu
Ni dung
XPATH
- Cc biu thc XPATH thng dng
- Axes
XSLT
XSLT
XSLT:
Chuyn i cu trc ca ti liu xml (thnh 1 ti liu xml khc, html)
Khc ngn ng lp trnh
- XSLT ch nh ngha cc qui tc chuyn i, vic thc hin chuyn i
phi nh mt chng trnh khc
Chng trnh (C#, Java)
XML
XSLT
XML
HTML
Cch thc hot ng
- Chng trnh s duyt ti liu xml (duyt cy)
- Vi mi node khp vi khun mu nh ngha trong file XSLT, s
c x l nh nh ngha trong file XSLT
L mt tp cck khun mu (template) v x l tng ng
20.05.2014 30
M hnh iu hnh XSLT
20.05.2014 31
XSLT Processor
Mt phn ca software
c mt XSLT stylesheet v nhp liu ti liu XML
Chuyn i ti liu nhp vo trong ti liu xut
Qua cu trc c s stylesheet c a
Thnh thong gi stylesheet processor
20.05.2014 32
XSL Stylesheet
Ti liu XML ch thc
c trng ca thnh phn gc:
stylesheet hay bin i
C hai c nh ngha trong mu khng gian
tn XSLT
http://www.w3c.org/XSL/Transform
xsl nh tin t thng l
XSLT processor nn phi hiu c hai
20.05.2014 33
V d XSLT 0
20.05.2014 34
Ti liu XML
20.05.2014 35
XSLT stylesheet n gin
20.05.2014 36
Kt qu s thc thi ca XSLT
20.05.2014 37
Gii thch kt qu
a stylesheet rng vo bt k t liu XML no:
Nhng thnh phn c t ngang lin tip
Ni dung ca mi thnh phn c t trong xut
Thuc tnh khng c t ngang
X l mc nh
Khng c bt k template c bit no
XSLT processor quay li x l mc nh
Cn cho nhng template
20.05.2014 38
Cu trc c s thc thi xml-stylesheet
Bao gm nh phn ca ti liu XML
Bo cho XML-aware browser ni tm thy stylesheet
kt hp

<?xml version=1.0>
<?xml-stylesheet
type=text/xml
href=http://www.oreilly.com/styles/people.xsl?>
<people>
......
20.05.2014 39
Templates
Theo di vic xut c to t vic nhp
xsl:template dng thnh phn
Thuc tnh match bao gm mt Xpath
expression
Xpath expression ng nht input node set
m n so khp
i vi mi node trong input node set, ni
dung template (nhng th gia th
xsl:template) c minh d ha v c
nhp vo cy xut
XSLT
<xsl:template>: dng nh ngha cc template
<xsl:stylesheet version = '1.0
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match=">
<h1>
<xsl:value-of select="//title"/>
</h1>
<h2>
<xsl:value-of select="//author"/>
</h2>
</xsl:template>
<xsl:template match=">
..
</xsl:template>
</xsl:stylesheet>
Khi gp nhng th tha k th
thc hin nhng ch th sau
Biu thc XPATH
20.05.2014 41
Thnh phn xsl:value-of
Rt trch string value ca mt thnh phn hay
mt thuc tnh v vit n n ch xut
Ni dung vn bn ca nhng thnh phn sau tt c
nhng th c chuyn i v thc th tham chiu
c gii quyt
Thuc tnh select cha trong Xpath expression
ng nht mt thnh phn hay mt thuc tnh
Nu n c th l mt node set, trong trng hp ny,
string value ca nt u tin s c ly
XSLT
<xsl:value-of> Rt trch ni dung nm trong cc th XML v
kt xut ra file kt qu
<hocsinh>
<hoten>Nguyen Van A</hoten>
<mssv>0412341</mssv>
</hocsinh>
<xsl:stylesheet.>
<xsl:template match=/>
<h1><xsl:value-of select=//hoten/></h1>
<h2><xsl:value-of select=//mssv/></h2>
</xsl:template>
</xsl:stylesheet>
<h1>
</h1>
<h2> </h2>
Nguyen Van A
0412341
Nu b du // ??
Cc th c rt trch ni dung c xc nh thng qua thuc
tnh select
Nu select tr v mt tp cc th th ni dung tt c cc th ny
u c kt xut ra file kt qu
XSLT
<xsl:template>
<source>
<a1>a1</a1>
<a2>a2</a2>
</source>

<xsl:stylesheet>



</xsl:stylesheet>
-Khng c template p dng x l mc
nh: tip tc so
khp cho tt c th
con ca th hin
hnh
-Khng c template
Mc nh: tr ra ni
dung ca th
a1
a2
<xsl:template match=/>

</xsl:template>
<xsl:apply-templates/>
20.05.2014 44
xsl:apply-templates
XSLT processor c (i ngang) ti liu
XML nhp lin tc t u ti cui
Templates c kch hot theo quy nh
chng so khp vi nhng thnh phn bt
gp
Template cho cha s c kch hot trc
con
20.05.2014 45
xsl:apply-templates
Quy c i ngang c th c thay i
bi apply-templates
N c th ch ra thnh phn hay nhng thnh
phn nn c thc thi k tip
N c th ch ra mt thnh phn hay nhiu
thnh phn nn c thc thi trong phn
gia ca thnh phn khc trong qu trnh
thc thi
N c th trnh mt phn nhng thnh phn
t qu trnh ang thc thi
20.05.2014 46
xsl:apply-templates
xsl:apply-templates c dng trong mt
tp mu gi nhng tp mu khc
Thuc tnh select cha Xpath expression
c dng lm mt ng cnh m sau
cc tp mu khc s c kim tra da
trn n
apply-templates khng c thuc tnh select th
mt ng cnh hin hnh s c dng
20.05.2014 47
V d xsl:apply-template
Xut mt ti liu theo nh yu cu sau:
Last name sau n First name
Ch name khng profession khng hobby

<?xml version=1.0 encoding=utf-8>
Turing
Alan

Feyman
Richard
20.05.2014 48
V d xsl:apply-template
20.05.2014 49
V d ti liu XML
20.05.2014 50
Kt qu
20.05.2014 51
Gii thch
Hai thnh phn <name> trong node
set
Nhng ni dung <xsl:value-of> ca
hai thnh phn <name> s trong
cy xut
Nhng thnh phn khc c hin
th trong mode mc nh
20.05.2014 52
V d xsl:apply-templates
20.05.2014 53
Kt qu
20.05.2014 54
xsl:apply-template
Rt tin ch khi nhng thnh phn con c nhng
template ca ring n

<xsl:template match=people>
<html>
<head><title>Famous Scientist</title></head>
<body>
<xsl:apply-template select=person/>
</body>
</html>
</xsl>
20.05.2014 55
xsl:apply-template
Thay th mi thnh phn people vi
thnh phn html
Thc thi tt c con person ca thnh
phn people hin ti
Thm vo vic xut ca bt k thnh
phn so khp vo trong thnh phn
body ca ti liu xut
20.05.2014 56
V d
20.05.2014 57
Kt qu
XSLT
<xsl:for-each> Duyt v x l thng tin tng th trong tp th kt
qu c chn ra t thuc tnh select
<root>
<hoten id=1>Nguyen Van A</hoten>
<hoten id=2>Nguyen Thi B</hoten>
</root>
<xsl:stylesheet.>
<xsl:template match=/>
<xsl:for-each select = //hoten>
<xsl:text>id=</xsl:text>
<xsl:value-of select=@id/>
<xsl:text>hoten=</xsl:text>
<xsl:value-of select=./>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
id=1hoten=Nguyen Van A
Id=2hoten=Nguyen Thi B
XSLT
<xsl:sort>: Thc hin sp xp trn file output
<root>
<hoten>Tran Van A</hoten>
<hoten>Nguyen Thi B</hoten>
</root>
<xsl:stylesheet.>
<xsl:template match=/>
<xsl:for-each select = //hoten>
<xsl:sort order=ascending select=./>
<xsl:value-of select=./>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Nguyen Thi B
Tran Van A
<root>
<value>1</value>
<value>9</value>
<value>20</value>
</root>
?????
XSLT
<xsl:sort data-type>
<root>
<value>1</value>
<value>20</value>
<value>9</value>
</root>
<xsl:stylesheet.>
<xsl:template match=/>
<xsl:for-each select = //value>
<xsl:sort data-type=number select=./>
<xsl:value-of select=./>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Kt qu:
1
9
20
Nu khng xc lp
th data-type = text
XSLT
<xsl:if test>
<root>
<value>1</value>
<value>20</value>
<value>9</value>
</root>
<xsl:stylesheet.>
<xsl:template match=/>
<xsl:for-each select = //value>
<xsl:sort data-type=number select=./>
<xsl:value-of select=./>
<xsl:if test=not (position()=last())>
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Kt qu:
1,9,20
XSLT
<xsl:choose>
<root>
<value>A</value>
<value>B</value>
</root>
<xsl:stylesheet.>
<xsl:template match=/>
<xsl:for-each select = //hoten>
<xsl:choose>
<xsl:when test = A/>
<xsl:text>30</xsl:text>
</xsl:when>

</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Kt qu:
30
XSLT
<xsl:element>, <xsl:attribute>
<root>
<sinhvien id=1>Nguyen Van A</sinhvien >
<sinhvien id=2>Tran Thi B</sinhvien >
</root>
<xsl:stylesheet.>
<xsl:template match=/root>
<xsl:element name=StudentList>
<xsl:for-each select = sinhvien>
<xsl:element name=Student>
<xsl:attribute name=StudentId >
<xsl:value-of select=@id />
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:element >
</xsl:template>
</xsl:stylesheet>
Kt qu:
<StudentList>
<Student StudentId=1 />
<Student StudentId=2 />
</StudentList>
XSLT
XSLT:
Cc hm tnh ton v format s
To cc node (th) v to thuc tnh
Khai bo v s dng bin
Cc hm boolean
Cc hm x l chui
Cc hm copy node
http://www.zvon.org/

You might also like