You are on page 1of 59

CN BN V XML

MC LC
Chng m u................................................................................................................ 2
1 XML l g? ................................................................................................................ 2
2 Cc ni dung s trnh by ........................................................................................ 2
Chng 1........................................................................................................................... 4
XML (eXtensible Markup Language) ............................................................................... 4
1 Phn l thuyt........................................................................................................... 4
1.1 Cc quy tc cn lu ....................................................................................... 4
1.2 Ch th x l (Processing Instructions) v li ch thch (Comments) ................ 5
1.3 Khng gian tn (namespace). ........................................................................... 6
1.3.1 Khai bo khng gian tn (namespace) ...................................................... 6
1.3.2 Khng gian tn mc nh (namespace default) ......................................... 6
1.4 CDATA .............................................................................................................. 8
1.5 Thc th nh ngha sn trong XML.................................................................. 8
2 Phn v d ................................................................................................................ 8
Chng 2........................................................................................................................... 9
DTD (Document Type Definition)..................................................................................... 9
1 DTD l g? ................................................................................................................ 9
2 nh ngha mt ti liu DTD ..................................................................................... 9
2.1 Phn t <!DOCTYPE>...................................................................................... 9
2.1.1 nh ngha DTD tham chiu ni............................................................... 10
2.1.2 nh ngha DTD tham chiu ngoi........................................................... 10
2.2 Phn t <!ELEMENT> .................................................................................... 12
2.3 Phn t <!ATTLIST>....................................................................................... 13
2.4 Thc th(Entity) .............................................................................................. 18
2.4.1 Thc th l g?......................................................................................... 18
2.4.1.1 Thc th tng qut .............................................................................. 18
2.4.1.1.1 Thc th tng qut ni .................................................................. 18
2.4.1.1.2 Thc th tng qut ngoi .............................................................. 19
2.4.1.2 Thc th tham s................................................................................. 19
2.4.1.2.1 Thc th tham s ni .................................................................... 20
2.4.1.2.2 Thc th tham s ngoi ................................................................ 20
Chng 3......................................................................................................................... 23
Xpath (XML Path Language) .......................................................................................... 23
1 Gii thiu................................................................................................................ 23
2 C php ca XPath ................................................................................................ 24
2.1 ng dn tuyt i ....................................................................................... 24

i
2.2 ng dn tng i ..................................................................................... 25
2.3 Chn cc phn t bng k t i din ............................................................ 25
2.4 Chn cc phn t theo iu kin .................................................................... 25
2.5 Mt s hm thng dng................................................................................ 25
2.6 Mt s ton t thng dng ........................................................................... 26
3 Mt s v d............................................................................................................ 27
Chng 4......................................................................................................................... 39
XSL (eXtensible style sheet) .......................................................................................... 39
1 XSL l g?............................................................................................................... 39
2 Qui tc chung ......................................................................................................... 39
3 Mt s phn t(element) thng dng ca XSL.................................................... 40
3.1 Phn t value-of.............................................................................................. 40
3.2 Phn t attribute ............................................................................................. 41
3.3 Phn t attribute-set ....................................................................................... 41
3.4 Phn t element.............................................................................................. 42
3.5 Phn t apply-templates ................................................................................. 43
3.6 Phn t call-template ...................................................................................... 44
3.7 Phn t for-each ............................................................................................. 45
3.8 Phn t if......................................................................................................... 46
3.9 Phn t iu khin choose ............................................................................. 46
3.10 Phn t variable.............................................................................................. 47
3.11 Phn t param ................................................................................................ 48
3.12 Phn t include ............................................................................................... 49
3.13 Phn t import ................................................................................................ 49
Chng 5......................................................................................................................... 51
XLink v XPointer............................................................................................................ 51
1 XLink ...................................................................................................................... 51
1.1 XLink l g? ..................................................................................................... 51
1.2 Cch to lin kt trong XLink .......................................................................... 51
1.2.1 Lin kt n gin (simple) ....................................................................... 52
1.2.2 Lin kt m rng (extended).................................................................... 53
1.2.3 Cung lin kt ............................................................................................ 54
1.2.3.1 Cung kt ni......................................................................................... 54
1.2.3.2 Cung kt ni nhiu nh ....................................................................... 54
1.2.3.3 Cung kt ni t hp ............................................................................. 55
2 XPointer(XML Pointer Language) .......................................................................... 56
2.1 XPointer l g?................................................................................................. 56
2.2 nh v v tr d liu ......................................................................................... 56

ii
iii
http://www.ebook.edu.vn Cn bn v XML

Chng m u
Trong thi i Cng ngh Thng tin hin nay XML (eXtensible Markup Language)
chim v tr s mt v rt quan trng trong vic chuyn ti, trao i d liu v lin lc gia
cc ng dng. iu ny cng c khn nh khi trong cc h iu hnh t WindowsXP
tr i, bn trong n cha y XML. Hn na khi b .Net ra i th cng lm cho XML tr
nn thnh hnh.
S dng k thut XML khng ch c tp on Microsoft m ngay c Sun, IBM, Oracles
iu h tr XML v dng n trong cc ng dng.
1 XML l g?
XML l ngn ng xy dng cu trc ti liu vn bn, da theo chun SGML
(Standard Generalized Markup Language: siu ngn ng c kh nng sinh
ngn ng khc). SGML c pht trin cho vic nh cu trc v ni dung ti
liu in t, do t chc ISO (International Organization for Standards) chun
ho nm 1986.
SGML l do IBM a ra, song khng th khng k n nhng ng gp ca
cc cng ty khc. XML c W3C (World Wide Web Consortium: t chc c
lp nh ra tiu chun cho trnh duyt Web, my ch v ngn ng) pht trin,
nhng c t XML li do Netscape, Microsoft v cc thnh vin ca d n
Text Encoding Initiative (TEI) xy dng. T chc W3C XML Special Interest
Group c i din t hn 100 cng ty cng nhiu chuyn gia c mi khc.
L do ra i ca XML v SGML rt rc ri, v HTML c nhiu gii hn nn
nm 1996 t chc W3C thit k XML. XML version 1.0 c nh ngha trong
h s February 1998 W3C Recommendation.
im quan trng ca k thut XML l n khng thuc ring v mt cng ty
no, n l mt sn phm m tr tu ca n thuc v c th gi, n l mt tiu
chun c mi ngi cng nhn v c son ra bi World Wide Web
Consortium - W3C (mt ban son tho vi s hin din ca tt c cc chuyn
gia Tin hc) v nhng kin ng gp bng cch trao i qua Email.
Bn thn ca XML rt l n gin, nhng cc cng c chun c nh ra
lm vic vi XML nh Document Object Model - DOM, XPath, XSL, v.v.. th
rt hu hiu, v chnh cc chun ny c pht trin khng ngng.
XML cng ging nh HTML u l ngn ng nh du, nhng iu cn ni
y l s ra i ca XML khc phc cho mt s yu km ca HTML.
HTML v XML u s dng cc tag nhng cc tag ca HTML l mt b d
liu tag c xy dng v nh ngha trc, tc l ngi lp trnh phi tun
th theo cc th nh ngha ca HTML, hin HTML c khon hn 400 tag,
nh ht 400 tag ny cng khng c g kh khn i vi ngi lp trnh web
chuyn nghip nhng tht kh i vi nhng ngi khng chuyn. Hn na
cc tag ca HTML khng ni ln c m t d liu trong . Nhng i vi
XML th hon ton khc bi v tag trong XML l do ngi lp trnh nh ngha
v mi tag l mt m t d liu m ngi lp trnh mun truyn t.
2 Cc ni dung s trnh by
Khi XML ra i th c hng lot cc ngn ng chun c a ra lm vic
vi XML, nhng trong ti liu ny ti ch xin c trnh by cc phn chnh
sau:
1. Ngn ng XML(eXtensible Markup Language )

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 2 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Ngn ng xy dng cu trc ti liu vn bn, da theo chun SGML


(Standard Generalized Markup Language
2. Ngn ng DTD (Document Type Definition)
Ngn ng dng nh ngha kiu d liu cho cc phn t trong ti
liu XML
3. Ngn ng Xpath (XML Path Language)
Ngn ng dung duyt ti liu XML
4. XSL (eXtensible style sheet)
Ngn ng dng chuyn i ti liu XML thnh mt nh dng khc
5. Ngn ng Xlink(XML Link Language) v Xpointer(XML Pointer Language)

Ngn ng lien kt v nh v ti liu

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 3 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Chng 1
XML (eXtensible Markup Language)
1 Phn l thuyt
1.1 Cc quy tc cn lu
vit c mt trang XML cng rt n gin, chng ta ch cn tun th
nhng quy tc sau:
Phi c mt Phn t gc duy nht, n cha tt c cc Phn t khc trong
ti liu.

<Catalog>
<Product ProductID="1">Chair</Product>
<Product ProductID="2">Desk</Product>
</Catalog>

Mi Tag m phi c mt Tag ng ging nh n.

1 <Order>
2 <OrderDate>2002-6-14</OrderDate>
3 <Customer>Helen Mooney</Customer>
4 <Item>
5 <ProductID>2</ProductID>
6 <Quantity>1</Quantity>
7 <Item>
8 <ProductID>4</ProductID>
9 <Quantity>3</Quantity>
10 </Item>
11 </Order>
V d trn khng tha qui tc 2 v thiu Tag ng </Item>, ta cn thm Tag
ng </item> vo sau dng th 6

Trong mt cp Tag ng v m phi c nh vn nh nhau, c ngha l


cc cp k t ca cp Tag ng m ny phi ging nhau hon ton.

1 <Order>
2 <OrderDate>2001-01-01</Orderdate>
3 <Customer>Graeme Malcolm</Customer>
4 </Order>
V d ny khng tha quy tc 3 v Tag m <OrderDate> v Tag ng
</Orderdate> nh vn khng ging nhau, ta phi sa Tag ng
</Orderdate> thnh </OrderDate> hoc sa th <OrderDate> thnh
<Orderdate>

Mi Phn t con phi nm trn bn trong Phn t cha ca


n.
TRUNG TM CNG NGH PHN MM THA THIN HU Trang 4 / 59
a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

1 <Catalog>
2 <Category CategoryName="Beverages">
3 <Product ProductID="1">
4 Coca-Cola
5 </Category>
6 </Product>
7 </Catalog>
V d ny khng tha quy tc 4 v Tag ng </Product> t khng ng v tr, ta
cn i v tr ca dng 5 cho dng 6

Gi tr ca thuc tnh phi c t trong cp du nhy kp hoc cp du


nhy n.

1 <Catalog>
2 <Product ProductID=1>Chair</Product>
3 <Product ProductID='2">Desk</Product>
4 </Catalog>
V d ny khng ng quy tc 5 v gi tr ca thuc tnh ProductID dng 3
t trong cp du nhy khng ng, ta cn sa li ProductID='2" thnh
ProductID=2" hoc ProductID='2

1.2 Ch th x l (Processing Instructions) v li ch thch (Comments)


Chng ta thng thy dng lnh
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
nm u file XML. y chnh l ch th x l, ch th x l c t trong cp
Tag <? v ?>. N cho bit phin bn c t XML m b phn tch cn lm
theo, ngoi ra n cho php ngi lp trnh cho bit d liu trong XML dng
encoding no, cn thuc tnh standalone s cho bit ti liu XML c cn n
mt ti liu khc khng (c hai gi tr cho thuc tnh ny l yes nu khng
cn n mt ti liu khc v no nu cn).
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Order>
<OrderDate>2002-6-14</OrderDate>
<Customer>Helen Mooney</Customer>
<Item>
<ProductID>1</ProductID>
<Quantity>2</Quantity>
</Item>
<Item>
<ProductID>4</ProductID>
<Quantity>1</Quantity>
</Item>
</Order>
Trong mi ti liu XML c th c hoc khng c phn ny

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 5 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

file XML tr nn d hiu v d chnh sa sau ny th cc dng ch thch l


khng th thiu, cc dng ch thch uc t trong cp tags <!-- v -->.
1.3 Khng gian tn (namespace).
XML cho php chng ta t do nh ngha cc th, nh n cho chng ta dng
cng mt tn nhng li ni n nhiu loi d liu khc nhau trong cng mt
ti liu XML. Xem v d sau:
<?xml version="1.0"?>
<BookOrder OrderNo="1234">
<OrderDate>2001-01-01</OrderDate>
<Customer>
<Title>Mr.</Title>
<FirstName>Graeme</FirstName>
<LastName>Malcolm</LastName>
</Customer>
<Book>
<Title>Treasure Island</Title>
<Author>Robert Louis Stevenson</Author>
</Book>
</BookOrder>

Ta thy trong v d trn c phn t Title ni n hai loi d liu khc nhau,
mt ni v tn tc gi mt ni v tiu sch, iu ny lm cho ta nhm ln
gia hai loi d liu. Hn th na nu ti liu ca chng ta c s dng ch
cho mt mc ch ring r th khng c vn g nhng khi ti liu ca chng
ta kt hp vi mt ti liu khc khc th ti liu kt hp ny s c vn v
chng ta u chc chn rng ti liu khc m chng ta mun kt hp khng
c s dng th trng vi th ca chng ta nh ngha hay khng.
V d nh khi chng ta tch hp ti liu XML ca chng ta vi ng dng khc
nh VML hay MathML m chng may gia cc ti liu ny c cng nh ngha
th NAME chng hn. Lc ny trnh phn tch s khng bit nn hiu th
NAME ca ti liu ca bn hay ca VML hay ca MathML.
V vy chng ta cn phi khai bo khng gian tn khc phc iu ny.
1.3.1 Khai bo khng gian tn (namespace)
khai bo mt khng gian tn ta ch cn a thm thuc tnh xmlns:prefix
vo bn trong phn t gc, prefix l tn ca khng gian tn, mi khng gian
tn cn mang mt nh danh duy nht. Mt khng gian tn c th l mt a
ch internet hoc mt a ch no min l a ch ny phi duy nht. V d
sau y s to ra mt khng gian tn hs v p dng cho tt c cc phn t
con:
<?xml version="1.0"?>
<BookOrder xmlns:hs="http://www.northwindtraders.com/customer">
<hs:Customer >
<hs:Title>Mr.</Title>
<hs:FirstName>Graeme</FirstName>
<hs:LastName>Malcolm</LastName>
</hs:Customer>
</BookOrder>

1.3.2 Khng gian tn mc nh (namespace default)

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 6 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Nu ti liu ca chng ta cc phn t ch s dng duy nht mt khng gian


tn th chng ta c th khai bo khng gian tn mc nh cho cc phn t con
ca mt phn t cha bng cch ch ghi thuc tnh xmlns v b i prefix

<?xml version="1.0"?>
<BookOrder xmlns ="http://www.northwindtraders.com/customer">
< Customer >
< Title>Mr.</Title>
< FirstName>Graeme</FirstName>
< LastName>Malcolm</LastName>
</ Customer>
</BookOrder>

Chng ta xem tip v d sau:


<?xml version="1.0"?>
<BookOrder >
<OrderDate>2001-01-01</OrderDate>
<Customer xmlns="http://www.northwindtraders.com/customer">
<Title>Mr.</Title>
<FirstName>Graeme</FirstName>
<LastName>Malcolm</LastName>
</Customer>
<Book xmlns="http://www.northwindtraders.com/book">
<Title>Treasure Island</Title>
<Author>Robert Louis Stevenson</Author>
</Book>
</BookOrder>

V d trn chng ta thy c hai khng gian tn mc nh, mt cho phn t


Customer v mt cho phn t Book. Nhng iu ng ni y l nu nh
trong ti liu c nhiu Customer v nhiu Book th chng ta khng th vit i
vit li khng gian tn mi c, rt mt thi gian.
Cch gii quyt tt nht l ta khai bo cc khng gian tn ny ngay u ti
liu v mi khng gian tn c phn bit bi cc nh danh.
<?xml version="1.0"?>
<BookOrder xmlns="http://www.northwindtraders.com/order"
xmlns:cus="http://www.northwindtraders.com/customer"
xmlns:bok ="http://www.northwindtraders.com/book >
<OrderDate>2001-01-01</OrderDate>
<cus:Customer >
<cus:Title>Mr.</cus:Title>
<cus:FirstName>Graeme</cus:FirstName>
<cus:LastName>Malcolm</cus:LastName>
</cus:Customer>
<bok:Book >
<bok:Title>Treasure Island</bok:Title>
<bok:Author>Robert Louis Stevenson</bok:Author>
</bok:Book>
</BookOrder>

V d trn dng 3 khng gian tn, mt khng gian tn mc nh v hai khng


gian tn c nh danh l cus v bok. Trong v d trn nhng phn t khng
c nh danh ca khng gian tn i trc th c hiu l s dng khng gian

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 7 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

tn mc nh http://www.northwindtraders.com/order, nh phn t
<OrderDate>2001-01-01</OrderDate>
1.4 CDATA
on d liu ca CDATA l on d liu nm gia <![CDATA [ v ]]>. Nhng
on d liu nm trong CDATA khi i qua trnh phn tch s c gi nguyn
nh ban u, tc l khi gp CDATA th trnh phn tch s b qua. iu ny rt
cn thit khi chng ta vit nhng on m script trong ti liu.

<script language=javascript>
<![CDATA[
function mag(){
alert(This is CDATA! );
}
]]
</script>

1.5 Thc th nh ngha sn trong XML


Trong ngn ng nh dng XML c s dng mt s k t nh dng c bit:
<, >, , , &. V vy gip cho chng ta th hin ti liu ng theo nguyn
mu bng cch nh ngha 5 thc th ny nh sau:

Thc M t
th

&apos; Tng ng vi du nhy n ()

&amp; Tng ng vi du &

&gt; Tng ng vi du >

&lt; Tng ng vi du <

&quot; Tng ng vi du nhy kp ()

V d:
<?xml version="1.0"?>
<LINK-TO>
&lt; a href=&quot; index.txt &quot; &gt; OPEN FILE INDEX.TXT &lt; /a &gt;
</LINK-TO>

(&lt; a href=&quot; index.txt &quot; &gt; OPEN FILE INDEX.TXT &lt; /a &gt;
tng ng vi <a href=index.txt> OPEN FILE INDEX.TXT </a>)
hiu r hn v thc th l g, chng ta s tm hiu trong chng 2.
2 Phn v d

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 8 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Chng 2
DTD (Document Type Definition)
1 DTD l g?
DTD (Document Type Definition) l kiu ti liu dng nh ngha kiu d
liu cho cc phn t trong ti liu XML. Khi chng ta nh ngha cc phn t
trong XML l ty thch, min sao cho n hp quy tc ca ti liu XML. Tuy
nhin tng minh hn th ta nn nh ngha kiu d liu cho tng phn t
trong ti liu XML.
Trong chng trc chng ta hc cch vit mt ti liu hp khun dng.
Tuy nhin mt ti liu XML c xem l hp khun dng v c gi tr khi ton
b cc phn t trong ti liu c c nh ngha kiu d liu m n cha.
Vi cch nh ngha kiu t liu (DTD) khi chng ta c mt ti liu XML no
th ch cn c phn DTD th chng ta s bit c cu trc ca ti liu XML.
Trc khi i vo phn chi tit v cch to mt ti liu DTD, chng ta hy xem
v d sau:

<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>

Xem v d trn chng ta thy phn c bi en l phn DTD dng m t


kiu d liu ca ti liu XML, phn c mu nht l cc phn t ca ti liu
XML.
Phn DTD trong v d ny c hiu nh sau: Ti liu XML c mt phn t
gc tn l note, phn t gc ny c 3 phn t con l from, heading, body v 3
phn t con ny c kiu d liu text.
2 nh ngha mt ti liu DTD
vit mt ti liu DTD cng rt d, ch cn chng ta tun th ng mt s
quy tc ca W3C l c. u tin chng ta hy tm hiu v cc phn t
(emlement), thuc tnh, thc th ca DTD.
2.1 Phn t <!DOCTYPE>

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 9 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Phn t ny c chc nng dng khai bo bt u nh ngha kiu t liu


DTD.
nh ngha kiu t liu c 2 dng, l DTD tham chiu ni v DTD tham
chiu ngoi. DTD tham chiu ni l DTD c nh ngha ngay trong ti liu
XML cn DTD tham chiu ngoi l DTD c nh ngha bn ngoi ti liu
XML. By gi chng ta s tm hiu tng c php mt.
2.1.1 nh ngha DTD tham chiu ni
bt u nh ngha kiu t liu DTD tham chiu ni chng ta dng c php
sau:
<!DOCTYPE root-element [DTD]
Trong root-element l phn t gc ca ti liu XML, DTD l cc nh ngha
cho cc phn t trong ti liu XML.

<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note body>
<!ELEMENT body (#PCDATA)>
]>

<note>
<body>Don't forget me this weekend</body>
</note>

2.1.2 nh ngha DTD tham chiu ngoi


S dng nh ngha DTD tham chiu ngoi s lm cho cc ng dng XML ca
chng ta tr nn d dng chia s v dng chung vi cc ng dng khc. C
hai cch ch nh mt DTD tham chiu ngoi: Tham chiu ngoi ring v
tham chiu ngoi chung.
Nhng nh ngha DTD tham chiu ngoi ring c s dng cho mt nhm
ngi mang tnh c nhn, chng khng c dng cho mc ch chung rng
ln, mc ch phn phi. Cn nhng nh ngha DTD tham chiu ngoi chung
s mang tnh cng ng hn.
nh ngha mt DTD tham chiu ngoi ring chng ta dng c php sau:
<!DOCTYPE root-element SYSTEM filename>
Trong root-element l tn ca phn t gc trong ti liu XML, filename l
tn file nh ngha kiu t liu DTD
V d:

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 10 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

<body>Don't forget me this weekend!</body>


</note>

File note.dtd vi ni dung nh sau:

<!ELEMENT note (to,from,heading,body)>


<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

a ch cha file DTD c th mt URL/URI.

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "http://www.w3schools.com/dtd/note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

nh ngha mt DTD tham chiu ngoi chung chng ta dng c php sau:
<!DOCTYPE root-element PUBLIC FPI URL>
Trong FPI (Formal Public Identifier) l mt nh danh chung hnh thc,
chng ta cn tun theo mt s quy tc p dng cho FPI sau:
o Trng u tin ca mt FPI l xc nh kt ni ca DTD n chun hnh
thc. i vi cc DTD chng ta t nh ngha th trng ny l mt du
chm. i vi cc chun hnh thc trng ny s t tham chiu n
chun ca n.
o Trng th hai l tn nhm hay tn ngi chu trch nhim bo tr v
nng cp cc nh ngha DTD v tn ny phi mang tnh duy nht.
o Trng th ba ch nh kiu ca ti liu c m t, thng th trng
ny km theo mt s nh danh duy nht no (chn hn nh version
1.0).
o Trng th ba ch nh ngn ng m bn nh ngha DTD (v d nh ngn
ng Ting Anh - EN)
o Mi trng ca FPI cch nhau bi du //
V d

<?xml version="1.0"?>
<!DOCTYPE note PUBLIC .//w3schools//note XML version 1.0//EN
"http://www.w3schools.com/dtd/note.dtd">
<note>

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 11 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

Cn URL l a ch ca file DTD.


2.2 Phn t <!ELEMENT>
Phn t <!ELEMENT> dng nh ngha kiu d liu cho mt phn t ca
mt ti liu XML. Chng ta s dng theo c php sau:
<!ELEMENT element-name content_model>
Trong :
element_name l tn ca phn t m ta mun nh ngha
content_model l kiu ca phn t ny, c th l EMPTY, ANY,
#PCDATA, cc phn t con hay trn ln nhiu thnh phn
By gi chng ta tm hiu chi tit hn.
nh ngha mt phn t rng
<!ELEMENT element_name EMPTY>
V d: <!EMLEMENT note EMPTY>
nh ngha mt phn t c cha nhiu kiu d liu
<!ELEMENT element_name ANY>
V d: <!ELEMENT note ANY>
nh ngha mt phn t c kiu vn bn
<!ELEMENT element_name (#PCDATA)>
V d: <!ELEMENT note (#PCDATA)>
nh ngha mt phn t c cha mt phn t con
<!ELEMENT element_name (child_element)>
V d: <!ELEMENT note (to)>
nh ngha mt phn t c cha nhiu hn mt phn t con, cch th nht
l chng ta c th lit k tt c cc phn t con v mi phn t con
cch nhau bi du phy.
V d khai bo phn t note c 4 phn t con l to, from, heading, body
chng ta vit nh sau:
<!ELEMENT note (to, from, heading, body)>
Tt nhin vi cch vit nh th ny th khng ti u, chng ta c th dng
cch vit th hai cho nhng phn t c nhiu phn t con bng cch dng
k t i din.
Di y l mt s nguyn tc s dng k t i din:
Gi s chng ta c phn t ROOT, phn t ny c hai phn t con l
LIMB_A v LIMB_B, chng ta c mt s nh ngha sau:

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 12 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

<!ELEMENT ROOT (LIMB_A*)>


Phn t ROOT khng c hoc c nhiu phn t LIMB_A
<!ELEMENT ROOT (LIMB_A+)>
Phn t ROOT c mt hoc nhiu phn t con LIMB_A
<!ELEMENT ROOT (LIMB_A?)>
Phn t ROOT khng c hoc c mt phn t con LIMB_A
<!ELEMENT ROOT (LIMB_A, LIMB_B)>
Phn t ROOT c 2 phn t con, u tin l phn t LIMB_A tip n l
LIMB_B
<!ELEMENT ROOT (LIMB_A | LIMB_B)>
Phn t ROOT c mt phn t con hoc l LIMB_A hoc l LIMB_B
nh ngha mt phn t c cha phn t con hoc cha d liu vn bn
<!ELEMENT LIMB_A (LIMB_A1| #PCDATA)>
2.3 Phn t <!ATTLIST>
Phn t <!ATTLIST> dng nh ngha kiu t liu ca cc thuc tnh cho
mt phn t trong ti liu XML. Chng ta dng c php sau:
<!ATTLIST element-name attribute-name attribute-type default-value>
Trong :
o element-name l tn ca mt phn t cn nh ngha thuc tnh
o attribute-name l tn thuc tnh cn nh ngha
o attribute-type kiu ca thuc tnh. C th nhn mt tong cc gi tr sau:

Kiu M t

CDATA Cho bit thuc tnh ny ch c th cha kiu d liu k t

(en1|en2|..) Danh sch cc gi tr m thuc tnh c th c gn

ID Cho bit thuc tnh ny l mt ID, tc l cc gi tr ca thuc


tnh ny khng c trng nhau v phi bt u bi mt ch
ci

IDREF Cho bit gi tr ca thuc tnh ny phi l mt trong cc gi tr


ca thuc tnh ID ca cc phn t khc

IDREFS Cho bit gi tr ca thuc tnh ny phi l cc gi tr ca cc


thuc tnh c kiu ID

NMTOKEN Cho bit gi tr ca thuc tnh l cc gi tr hp vi quy tc t


tn ca phn t ca ti liu XML

NMTOKENS Cng ging nh NMTOKEN nhng n cho php cha nhiu


NMTOKEN

ENTITY Cho bit thuc tnh ny nhn gi tr l mt tn tham chiu ca


thc th

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 13 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

ENTITIES Cho bit thuc tnh ny nhn gi tr l cc tn tham chiu ca


thc th v cch nhau bi khon trng

NOTATION (ti cha hiu kiu ny)

xml: (ti cha hiu kiu ny)

o default-value thng tin v gi mc nh tr ca thuc tnh ny. N c th


nhn mt trong cc gi t sau:

Gi tr M t

value value l mt gi tr mc nh no cho gi tr ny (v d


CNTT)

#REQUIRED Ch nh l khng c gi tr mc nh cho thuc tnh ny,


nhng khi s dng l phi khi to

#IMPLIED Ch nh l khng c gi tr mc nh cho thuc tnh ny, v


thuc tnh ny khng cn dng n

#FIXED Ch nh thuc tnh ny ch mang duy nht gi tr value ny


value

Chng ta c th nh ngha mt phn t c nhiu thuc tnh theo c php sau:


<!ATTLIST element-name
attribute-name_1 attribute-type_1 default-value_1
attribute-name_2 attribute-type_2 default-value_2
...
attribute-name_n attribute-type_n default-value_n>
(Xem v d1)
V d1:
Gi s chng ta c file att.dtd vi ni dung sau:

<!ELEMENT attributes (#PCDATA)>


<!ATTLIST attributes aaa CDATA #REQUIRED bbb CDATA #IMPLIED>

File XML chng ta vit nh sau:

<?xml version="1.0"?>
<!DOCTYPE attributes SYSTEM "att.dtd">
<attributes aaa="#d1" bbb="*~*">Text</attributes>

V d2:
Gi s chng ta c file att.dtd vi ni dung sau:

<!ELEMENT attributes (#PCDATA)>


<!ATTLIST attributes aaa CDATA #IMPLIED

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 14 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

bbb NMTOKEN #REQUIRED


ccc NMTOKENS #REQUIRED>

File XML chng ta vit nh sau:

<?xml version="1.0"?>
<!DOCTYPE attributes SYSTEM "att.dtd">
<attributes aaa="#d1" bbb="a1:12" ccc=" 3.4 div -4"/>

Nu chng ta vit nh sau s khng hp quy tc v kiu NMTOKEN v


NMTOKEN khng chp nhn k t # :

<?xml version="1.0"?>
<!DOCTYPE attributes SYSTEM "att.dtd">
<attributes aaa="#d1" bbb="#d1" ccc="#d1"/>

V d3:
Gi s chng ta c file att.dtd vi ni dung sau:

<!ELEMENT XXX (AAA+ , BBB+ , CCC+)>


<!ELEMENT AAA (#PCDATA)>
<!ELEMENT BBB (#PCDATA)>
<!ELEMENT CCC (#PCDATA)>
<!ATTLIST AAA id ID #REQUIRED>
<!ATTLIST BBB code ID #IMPLIED
list NMTOKEN #IMPLIED>
<!ATTLIST CCC X ID #REQUIRED
Y NMTOKEN #IMPLIED>

File XML chng ta vit nh sau:

<?xml version="1.0"?>
<!DOCTYPE XXX SYSTEM "att.dtd">
<XXX>
<AAA id="L12"/>
<BBB code="QW" list="L12"/>
<CCC X="x-0" Y="QW" />
<CCC X="x-1" Y="QW" />
</XXX>

Nu chng ta vit nh sau s khng hp quy tc v phn t CCC c thuc


tnh X c kiu l ID nn phi l duy nht.

<?xml version="1.0"?>
<!DOCTYPE XXX SYSTEM "att.dtd">
<XXX>
TRUNG TM CNG NGH PHN MM THA THIN HU Trang 15 / 59
a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

<AAA id="L12"/>
<BBB code="QW" list="L12"/>
<CCC X="ZA" Y="QW" />
<CCC X="ZA" Y="QW" />
</XXX>

Nu chng ta vit nh sau s khng hp quy tc v phn t AAA v CCC c


thuc tnh c kiu l ID nn khng c c gi tr ging nhau.

<?xml version="1.0"?>
<!DOCTYPE XXX SYSTEM "att.dtd">
<XXX>
<AAA id="L12"/>
<BBB code="QW" list="L12"/>
<CCC X="L12" Y="QW" />
</XXX>

V d4
Gi s chng ta c file att.dtd vi ni dung sau:

<!ELEMENT XXX (AAA+ , BBB+, CCC+, DDD+)>


<!ELEMENT AAA (#PCDATA)>
<!ELEMENT BBB (#PCDATA)>
<!ELEMENT CCC (#PCDATA)>
<!ELEMENT DDD (#PCDATA)>
<!ATTLIST AAA
mark ID #REQUIRED>
<!ATTLIST BBB
id ID #REQUIRED>
<!ATTLIST CCC
ref IDREF #REQUIRED>
<!ATTLIST DDD
ref IDREFS #REQUIRED>

File XML chng ta vit nh sau l hp quy tc:

<?xml version="1.0"?>
<!DOCTYPE XXX SYSTEM "att.dtd">
< XXX>
<AAA mark="a1"/>
<AAA mark="a2"/>
<AAA mark="a3"/>
<BBB id="b001" />

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 16 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

<CCC ref="a3" />


<DDD ref="a1 b001 a2" />
</XXX>

Nu chng ta vit nh sau s khng hp quy tc v phn t DDD c thuc


tnh ref c kiu l IDREFS, trong khi chng ta li gn gi tr cho thuc tnh
ca phn t ny l ref=a1 b001 a2 trong khi b001 khng phi l gi tr
ca mt ID no c.

<?xml version="1.0"?>
<!DOCTYPE XXX SYSTEM "att.dtd">
<XXX>
<AAA mark="a1"/>
<AAA mark="a2"/>
<BBB id="b01" />
<CCC ref="a3" />
<DDD ref="a1 b001 a2" />
</XXX>

V d 5.
Gi s chng ta c file att.dtd vi ni dung sau:

<!ELEMENT XXX (AAA+, BBB+)>


<!ELEMENT AAA (#PCDATA)>
<!ELEMENT BBB (#PCDATA)>
<!ATTLIST AAA true ( yes | no ) #REQUIRED>
<!ATTLIST BBB month (1|2|3|4|5|6|7|8|9|10|11|12) #IMPLIED>

File XML chng ta vit nh sau l hp quy tc:

<?xml version="1.0"?>
<!DOCTYPE XXX SYSTEM "att.dtd">
<XXX>
<AAA true="yes"/>
<AAA true="no"/>
<AAA true="yes"/>
<BBB month="8" />
<BBB month="2" />
<BBB month="12" />
</XXX>

Nu chng ta vit nh sau s khng hp quy tc v phn t AAA v phn t


BBB c thuc tnh true v month c kiu lit k, trong khi chng ta gn gi
tr cho hai thuc tnh ny ngoi gi tr lit k.

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 17 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

<?xml version="1.0"?>
<!DOCTYPE XXX SYSTEM "att.dtd">
<XXX>
<AAA true="yes"/>
<AAA true="no"/>
<AAA true="maybe"/>
<BBB month="8" />
<BBB month="2" />
<BBB month="16" />
</XXX>

2.4 Thc th(Entity)


Nh chng 1 cp n thc th nhng ch l nhng thc th
c nh ngha sn. By gi chng ta cn tm hiu k hn v thc th l g
v cch nh ngha mt thc th.
2.4.1 Thc th l g?
Thc th thc cht l mt cch nh ngha mt bin lu tr mt khi d liu,
khi thc th ny c triu gi th n s chn nguyn khi d ca n vo v t
triu gi. Khi d liu ca thc th thng l dng text, tuy nhin n cng
c th l d liu nh phn, min l khi d liu ny khng ph v khun dng
ca mt ti liu XML khi n c gi.
C hai loi thc th l thc th tng qut v thc th tham s. Thc th
c khai bo trong phn nh ngha DTD.
tham chiu n thc th tng qut chng ta vit theo c php:
&name_entity;
Trong name_entity l tn thc th tng qut cn tham chiu. Lu l bt
u bi k t & v kt thc bi du chm phy.
tham chiu n thc th tham s chng ta vit theo c php:
%name_entity;
Trong name_entity l tn thc th tham s cn tham chiu. Lu l bt
u bi k t % v kt thc bi du chp phy
2.4.1.1 Thc th tng qut
C hai loi thc th tng qut l thc th tng qut ni v thc th tng
qut ngoi.
2.4.1.1.1 Thc th tng qut ni
Thc th tng qut ni l thc th c nh ngha ngay trn DTD ca ti liu
XML.
Chng ta nh ngha theo c php sau:
<!ENTITY entity-name entity-value >
V d:

<?xml version="1.0"?>

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 18 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

<!DOCTYPE attributes [
<!ELEMENT attributes (#PCDATA)>
<!ATTLIST attribute aaa CDATA #REQUIRED>
<!ENTITY out-text TT CN PM>
]>
<attributes aaa="C" >&out-text;</attributes>

i vi thc th ny chng ta cng c th nh ngha cc thc th tham chiu


lng nhau.
V d:
<!ENTITY name Open source software>
<!ENTITY name-group &name; Group>
Tuy nhin chng ta khng th o ngc li
<!ENTITY name-group &name; Group>
<!ENTITY name Open source software>
2.4.1.1.2 Thc th tng qut ngoi
Thc th tng qut ngoi l thc th c nh ngha v tham chiu t mt
ngun bn ngoi.
Chng ta nh ngha nh ngha theo 1 trong 2 c php sau:
<!ENTITY entity-name SYSTEM "URI/URL">
<!ENTITY entity-name PUBLIC FPI "URI/URL">
Trong :
FPI c cp n trong phn 1.2.1.2
URI/URL l a ch n ngun d liu cn gn cho entity-name
V d:

<?xml version="1.0"?>
<!DOCTYPE author [
<!ELEMENT author (#PCDATA)>
<!ATTLIST author CR CDATA #REQUIRED>
<!ENTITY writer SYSTEM
"http://www.w3schools.com/entities/entities.xml">
<!ENTITY copyright SYSTEM "copyright.txt">
]>
<author CR="C" >& writer; &copyright; </author>

Ch : Chng ta khng th dng tham chiu thc th tng qut ngay trong bn
thn cc khai bo DTD
2.4.1.2 Thc th tham s
Thc th tham s khc vi thc th tng qut ch l n cho php tham
chiu n n ngay trong bn thn cc khai bo DTD v vng hot ng ca n
ch nm trong vng khai bo cc DTD.
TRUNG TM CNG NGH PHN MM THA THIN HU Trang 19 / 59
a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Mc ch ca ch ca vic s dng thc th tham s l trnh cc khai bo


lp li khi nh ngha DTD v gip cho chng ta d dng thay i.
Tng t nh thc th tng qut, thc th tham s cng c hai loi l
thc th tham s ngoi v thc th tham s ni.
2.4.1.2.1 Thc th tham s ni
Thc th tham s ni l thc th c nh ngha ngay trn DTD ca ti liu
XML. nh ngha thc th tham s chng ta dng c php sau:
<!ENTITY % entity-name entity-vale>
Trong :
% l tham s bt buc
entity-name l tn ca thc th tham s cn nh ngha
entity-value l gi tr cn gn cho entity-name
V d:

C s dng thc th tham s ni Khng s dng thc th tham s ni

<?xml version="1.0"?> <?xml version="1.0"?>


<!DOCTYPE author [ <!DOCTYPE author [
<!ENTITY name "Open source <!ENTITY name1 "Open source
software"> software">
<!ENTITY name-group "&name; <!ENTITY name-group "&name1;
Group"> Group">
<!ENTITY % EL "<!ELEMENT author <!ELEMENT author (#PCDATA)>
(#PCDATA)>"
>
>
]>
%EL;
<author>&name-group;</author>
]>
<author>&name-group;</author>

2.4.1.2.2 Thc th tham s ngoi


Thc th tham s ngoi l thc th c nh ngha v tham chiu t mt
ngun bn ngoi. nh ngha thc th tham s ngoi chng ta vit theo mt
trong hai c php sau:
<!ENTITY % entity-name SYSTEM "URI/URL">
<!ENTITY % entity-name PUBLIC FPI "URI/URL">
Trong :
T kha SYSTEM cho bit y l thc th tham s ngoi ring
T kha PUBLIC cho bit y l thc th tham s ngoi chung
FPI (Formal Public Identifier) l mt nh danh chung hnh thc ( trnh by
phn 1.2.1.2).
URI/URL l a ch ca khi gi liu cn gn cho entity-name
V d:
Gi s chng ta c file hocsinh.dtd nh sau:
TRUNG TM CNG NGH PHN MM THA THIN HU Trang 20 / 59
a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

<!ELEMENT HOCSINH (HOTEN, NGAYSINH, LOP)>


<!ELEMENT HOTEN (#PCDATA)>
<!ELEMENT NGAYSINH (#PCDATA)>
<!ELEMENT LOP (#PCDATA)>

By gi chng ta vit file ti liu XML c tn test.xml vi thc th tham s


ngoi nh sau:

<?xml version=1.0?>
<!DOCTYPE HOCSINH [
<!ENTITY % hs SYSTEM hocsinh.dtd>
%hs;
]>
<HOSCINH>
<HOTEN>Le Van A</HOTEN>
<NGAYSINH>26-06-79</NGAYSINH>
<LOP>6A3</LOP>
</HOSCINH>

Vit <!ENTITY % hs SYSTEM hocsinh.dtd> c ngha l file hocsinh.dtd nm


cng th mc vi file test.xml.
Nu file hocsinh.dtd t ti a ch http://hs.com.vn/hocsinh.dtd th chng ta
vit li dng nh sau: <!ENTITY % hs SYSTEM
http://hs.com.vn/hocsinh.dtd>
Ch : Trc khi c c iu lu th chng ta hy xem v d sau:

<!ENTITY % mathml-colon ''>


<!ENTITY % mathml-prefix ''>
<!ENTITY % mathml-exp '%mathml-prefix;%mathml-colon;exp' >
<!ENTITY % mathml-abs '%mathml-prefix;%mathml-colon;abs' >
<!ENTITY % mathml-arg '%mathml-prefix;%mathml-colon;arg' >
<!ENTITY % mathml-real '%mathml-prefix;%mathml-colon;real' >
<!ENTITY % mathml-imaginary '%mathml-prefix;%mathml-colon;imaginary'
>
<!ELEMENT %mathml-imaginary; (#PCDATA)>

y l mt DTD c nh ngha cc thc th tham s, chng ta thy cc thc


th tham s c th tham chiu ln nhau theo mt trnh t t trn xung v c
th c tham chiu ngay trong mt nh ngha element. Tuy nhin cho
cc cch tham chiu ny c th hot ng c th bt buc n phi c
nh ngha c lp t mt file DTD v c tham chiu vo ti liu XML di
dng DTD tham chiu ngoi.

<?xml version=1.0>
<!DOCTYPE exp SYSTEM exp.dtd [
TRUNG TM CNG NGH PHN MM THA THIN HU Trang 21 / 59
a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

]
>
<exp> imaginary </exp>

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 22 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Chng 3
Xpath (XML Path Language)
1 Gii thiu.
Trc khi i vo phn ny chng ta hy xem li mt v d v ti liu XML:
<?xml version="1.0"?>
<Order OrderNo="1047">
<OrderDate>2002-03-26</OrderDate>
<Customer>John Costello</Customer>
<Item>
<Product ProductID="1" UnitPrice="70">Chair</Product>
<Quantity>6</Quantity>
</Item>
<Item>
<Product ProductID="2" UnitPrice="250">Desk</Product>
<Quantity>1</Quantity>
</Item>
</Order>

Vi v d ny khi chng ta m vi trnh duyt IE chng ta s c kt qu


sau:

Nh vy chng ta thy trn trnh duyt s hin th y nguyn ti liu gc. Vy


lm cch no chng ta c th i li trn cc phn t ca ti liu XML
trch ra nhng d liu m chng ta cn thit.
p ng iu ny ngi ta thit k ra mt ngn ng XPath. XPath c mt
vai tr quan trng trong vic trao i d liu gia cc my tnh hay gia cc
chng trnh ng dng v n cho chng ta sng lc cc d liu m ta mong
mun.

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 23 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

XPath xem XML nh mt cy, vi v d trn s c biu din di dng cy


sau:

Order (Element gc)

Ch thch
OrderNo = 1047
Element
OrderDate (2002-03-26) Thuc tnh

Custumer (John Costello)

Item

Product (Chair)
ProductID=1

UnitPrice=70
Quantity(6)

Item

Product (Desk)
ProductID=2

UnitPrice=250
Quantity(1)

Hnh 2.2
By gi chng ta hy hc cch i qua cc nt trong ti liu XML.
2 C php ca XPath
2.1 ng dn tuyt i
Nu ng dn XPath bt u bi du / th c ngha y l mt ng dn
tuyt i bt u t phn t gc.
Trong hnh2.2 trn, by gi chng ta mun chn nt Order ta vit nh sau
C php nguyn: /child::Order
C php tt: /Order
i ra nhnh con Custumer bng XPath nh sau:
C php nguyn: /child::Order/child::Custumer
C php tt: /Order/Custumer
Trong trng hp mun i n thuc tnh ca nt th chng ta cn phi ch r
t kha Attribute trong c php nguyn hoc @ trong c php tt.
ly thuc tnh OrderNo ca nt Order ta dng c php XPath nh sau:
C php nguyn: /child::Order/Attribute::OrderNo
TRUNG TM CNG NGH PHN MM THA THIN HU Trang 24 / 59
a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

C php tt: /Order/@OrderNo


2.2 ng dn tng i
Khi chng ta mun trch mt phn t no m chng ta ch bit tn ca
phn t ny ch chng ta khng bit l phn t ny nm v tr no th chng
ta c th dng ng dn tng i lm iu ny. Chng ta dng du //
ch cho trnh phn tch bit y l ng dn tng i.
V d, trch cc phn t c tn l Product chng ta vit nh sau:
C php nguyn: //child::Product
C php vit tt: //Product
Khi chng ta vit nh th ny th khi i qua trnh phn tch s truy tm n cc
phn t c tn l Product
2.3 Chn cc phn t bng k t i din
chn tt c cc phn t con ca mt phn t no chng ta dng k t
i din *.
V d, ly tt c cc phn t con ca phn t Order ta vit nh sau:
C php nguyn: /child::Order/child::*
C php tt: /Order/*
2.4 Chn cc phn t theo iu kin
ly cc phn t theo mt iu kin no chng ta dng du ngoc
vung([ ]).
V d, ly mi phn t Product c thuc tnh UnitPrice > 70 ta vit nh
sau:
C php nguyn:
//child::Product[Attribute::UnitPrice>70]
C php tt:
//Product[@UnitPrice>70]
V d, ly nhng phn t Item c phn t con l Product v c thuc tnh
ProductID=1 chng ta vit nh sau:
C php nguyn:
//child::Item[child::Product/Attribute::ProductID=1]
C php tt:
//Item[Product/@ProductID=1]

2.5 Mt s hm thng dng


Tn hm ngha V d

count() Hm ly tng s nt con ca mt //Item[count(*)=2]


phn no
Chn tt c cc phn t Item c s phn
TRUNG TM CNG NGH PHN MM THA THIN HU Trang 25 / 59
a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

t con l 2

name() Ly tn ca phn t /Order/*[name()=Item]


Chn tt c cc phn t con ca Order
c tn l Item

not() Hm ph nh //Item/*[not(@*)]
Chn tt c cc phn t con ca Item
khng cha thuc tnh no

normalize-space(str) Hm b khong trng //Item/*[normalize-


space(@ProductID)=abc]
Chn tt c cc phn t con ca Item c
thuc tnh ProductID=abc (khng phn
bit khong trng)

starts- Hm kim tra xem chui str c //item/*[starts-with(name(),P)]


with(str,substr) cha chui substr (tnh t v tr u
tin) hay khng Chn tt c cc phn t con ca Item c
tn bt u bi k t P

contains(str,substr) Kim tra mt chui str c cha //item/*[contains(name(),u)]


chui con substr hay khng
Chn tt c cc phn t con ca phn
t Item m tn ca cc phn t con ny
c ch k t u

string-length(str) Hm ly chiu di ca 1 chui //Item/*[string-length(name())=5]


Chn tt c cc phn t con ca Item
m di tn ca cc phn t con ny
l 5

position() Cho bit v tr hin ti ca phn t //Item[position()=5]


Chn phn t Item c v tr l 5

floor() Ly gi tr nh nht gn vi gi tr
ch nh

ceiling() Ly gi tr ln nht gn vi gi tr
ch nh

last() V tr nt cui cng //Item[last()]


Chn phn t Item cui cng

2.6 Mt s ton t thng dng


Tn ton t Chc nng V d

| Ton t hoc dng chn ra mt //Item/*[starts-with(name(),U) | starts-


ln nhiu phn t c iu kin khc with(name(),Q) ]
nhau
Chn tt c cc phn t l con ca Item
c c tn bt u bi k t P hoc Q

descendant Chn phn t con ca phn t ch /Order /Item/Product/descendant::*


nh
Chn tt c cc phn t l con ca
/Order/Item/Product

ancestor Chn phn t cp trn /Order/Item/Product/ancestor::*

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 26 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

chn 2 phn t Item v phn t Order

following-sibling Chn phn t cng cp k tip /Order/OrderDate/following-sibling::*


chn cc phn t Custumer v hai phn
t Item theo sau v cng cp vi phn
t OrderDate

preceding-sibling Chn phn t cng cp trc /Order/Custumer/preceding-sibling::*


chn phn t OrderDate

following Chn phn t theo sau phn t ch /Order/OrderDate/following::*


nh
chn phn t Custumer v 2 phn t
Item v cc phn t con ca Item

preceding Chn cc phn t ng trc phn /Order/Custumer/preceding::*


t ch nh
chn tt c cc phn t i trc phn t
Custumer

descendant-or-self Chn phn t cp di v phn t /Order/Item/descendant-or-self::*


ch nh
Chn tt c cc phn t Item v cc
phn t con ca phn t ny

ancestor-or-self Chn phn t cp trn v phn t /Order/Item/product/ancestor-or-


ch nh self::*
chn 2 phn t product, 2 phn t Item
v phn t Order

3 Mt s v d
Chn phn t gc AAA (/AAA)

<AAA> AAA
<BBB/>
<CCC/> BBB
<BBB/>
<BBB/> CCC
<DDD>
<BBB/> BBB
</DDD>
<CCC/> DDD BBB
</AAA>
CCC

Chn phn t CCC l con ca AAA (/AAA/CCC)

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 27 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

<AAA> AAA
<BBB/>
<CCC/> BBB
<BBB/>
<BBB/> CCC
<DDD>
<BBB/> BBB
</DDD>
<CCC/> DDD BBB
</AAA>
CCC

Chn tt c cc phn t BBB l con ca DDD m DDD l con ca AAA


(/AAA/DDD/BBB)

<AAA> AAA
<BBB/>
<CCC/> BBB
<BBB/>
<BBB/> CCC
<DDD>
<BBB/>
BBB
</DDD>
<CCC/>
</AAA> DDD BBB

CCC DDD BBB

BBB

Chn tt c cc phn t BBB (//BBB)

<AAA> AAA
<BBB/>
<CCC/> BBB
<BBB/>
<DDD> CCC
<BBB/>
</DDD>
BBB
<CCC>
<DDD>
<BBB/> DDD BBB
<BBB/>
</DDD> CCC DDD BBB
</CCC>
</AAA> BBB

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 28 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Chn tt c cc phn t BBB l con ca DDD (//DDD/BBB)

<AAA> AAA
<BBB/>
<CCC/> BBB
<BBB/>
<DDD> CCC
<BBB/>
</DDD>
BBB
<CCC>
<DDD>
<BBB/> DDD BBB
<BBB/>
</DDD> CCC DDD BBB
</CCC>
</AAA> BBB

Chn tt c cc phn t m dng h ca n l /AAA/CCC/DDD


(/AAA/CCC/DDD/*)

<AAA> AAA
<XXX>
<DDD> XXX DDD BBB
<BBB/>
BBB
<BBB/>
<EEE/> EEE
<FFF/>
</DDD> FFF
</XXX>
<CCC> CCC DDD BBB
<DDD>
<BBB/> BBB
<BBB/>
EEE
<EEE/>
<FFF/> FFF
</DDD>
</CCC> CCC BBB BBB
<CCC>
<BBB>
<BBB> BBB
<BBB/>
</BBB>
</BBB>
</CCC>
</AAA>

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 29 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Chn tt c cc phn t BBB m n c 3 cp cha (/*/*/*/BBB)

<AAA> AAA
<XXX>
<DDD> XXX DDD BBB
<BBB/>
BBB
<BBB/>
<EEE/> EEE
<FFF/>
</DDD> FFF
</XXX>
<CCC> CCC DDD BBB
<DDD>
<BBB/> BBB
<BBB/>
EEE
<EEE/>
<FFF/> FFF
</DDD>
</CCC> CCC BBB BBB
<CCC>
<BBB>
<BBB> BBB
<BBB/>
</BBB>
</BBB>
</CCC>
</AAA>

Chn phn t BBB u tin l con ca AAA (/AAA/BBB[1])

<AAA> AAA
<BBB/>
<BBB/> BBB
<BBB/> BBB
<BBB/>
</AAA> BBB

BBB

Chn phn t BBB cui cng l con ca AAA (/AAA/BBB[last()])

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 30 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

<AAA> AAA
<BBB/>
BBB
<BBB/>
<BBB/> BBB
<BBB/>
</AAA> BBB

BBB

Chn tt c cc thuc tnh c tn id (//@id)

<AAA>
AAA
<BBB id = "b1"/> id
<BBB id = "b2"/> BBB
<BBB name = "bbb"/> id
<BBB/> BBB
name
</AAA>
BBB

BBB

Chn tt c cc phn t BBB c thuc tnh tn l id (//BBB[@id])

<AAA>
AAA
<BBB id = "b1"/> id
<BBB id = "b2"/> BBB
<BBB name = "bbb"/> id
<BBB/> BBB
name
</AAA>
BBB

BBB

Chn tt c cc phn t BBB c tn thuc tnh (//BBB[@*])

<AAA>
AAA
<BBB id = "b1"/> id
<BBB id = "b2"/> BBB
<BBB name = "bbb"/> id
<BBB/> BBB
name
</AAA>
BBB

BBB

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 31 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Chn tt c cc phn t BBB c tn thuc tnh l bbb, khng phn bit khon
trng (//BBB[normalize-space(@name)='bbb'])

<AAA>
AAA
<BBB id = "b1"/> id
<BBB name=" bbb "/> BBB
<BBB name = "bbb"/> id
</AAA> BBB
name
BBB

Chn tt c cc phn t c cha cc phn t m trong c 2 phn t con


tn l BBB (//*[count(BBB)=2])

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

Chn tt c cc phn t m tn ca n bt u l k t B
(//*[starts-with(name(),B)])

<AAA>
AAA
<BCC>
<BBB/> BCC BBB
<BBB/>
</BCC> BBB
<BEC>
BEC CCC
<CCC/>
<DBD/> DBD
</BEC>
</AAA>

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 32 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Chn tt c cc phn t m tn ca n c cha k t


B(//*[contains(name(),B)])

<AAA>
AAA
<BCC>
<BBB/> BCC BBB
<BBB/>
</BCC> BBB
<BEC>
BEC CCC
<CCC/>
<DBD/> DBD
</BEC>
</AAA>

Chn tt c cc phn t m tn ca n c di l 3
(//*[string-length(name())=3)])

<AAA>
AAA
<Q/>
<SSSS/> Q
<BB/>
<CCC/> SSSS
<DDDD/>
BB
</AAA>
CCC

DDDD

Chn tt c cc phn t m tn ca n c di khc 3


(//*[string-length(name())!=3)])

<AAA>
AAA
<Q/>
<SSSS/> Q
<BB/>
<CCC/> SSSS
<DDDD/>
BB
</AAA>
CCC

DDDD

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 33 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Chn tt c cc phn t m tn ca n l CCC hoc BBB


( //*[name()=CCC]| //*[name()=BBB] )

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

Chn tt c cc phn t l con ca AAA/BBB (/AAA/BBB/descendant::*)

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

Chn tt c cc phn t l cha ca phn t DDD (//DDD/parent::*)

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

Chn tt c cc phn t l t tin ca phn t DDD (//DDD/ancestor::*)

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 34 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

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

Chn tt c cc phn t cng cp i sau phn t BBB (//BBB/following-


sibling::*)

<AAA>
AAA
<BBB>
<CCC/> CCC
<DDD/> BBB
</BBB> DDD
<XXX>
<DDD>
XXX DDD EEE
<EEE/>
</DDD>
</XXX>
<CCC> CCC DDD
<DDD/>
</CCC>
</AAA>

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 35 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Chn tt c cc phn t cng cp i trc phn t XXX (//XXX/preceding-


sibling::*)

<AAA>
AAA
<BBB>
<CCC/> CCC
<DDD/> BBB
</BBB> DDD
<XXX>
<DDD>
XXX DDD EEE
<EEE/>
</DDD>
</XXX>
<CCC> CCC DDD
<DDD/>
</CCC>
</AAA>

Chn tt c cc phn t i sau phn t ZZZ (//ZZZ/following::*)

<AAA>
AAA
<BBB>
<CCC/> CCC
<ZZZ> BBB
<DDD/> ZZZ DDD
</ZZZ>
<FFF>
<GGG/> FFF GGG
</FFF>
</BBB> EEE
<XXX>
<DDD> XXX DDD DDD
<EEE/>
<DDD/>
FFF
<FFF/>
</DDD>
</XXX> CCC DDD
<CCC>
<DDD/>
</CCC>
</AAA>

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 36 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Chn tt c cc phn t i trc phn t XXX ngoi tr nhng phn t gc


(//XXX/preceding::*)

<AAA>
AAA
<BBB>
<CCC/> CCC
<ZZZ> BBB
<DDD/> ZZZ DDD
</ZZZ>
<FFF>
<GGG/> FFF GGG
</FFF>
</BBB> EEE
<XXX>
<DDD> XXX DDD DDD
<EEE/>
<DDD/>
FFF
<FFF/>
</DDD>
</XXX> CCC DDD
<CCC>
<DDD/>
</CCC>
</AAA>

Chn tt c cc phn t CCC v con ca n (//CCC/descendant-or-self::*)

<AAA>
AAA
<BBB>
<CCC/> CCC
<ZZZ> BBB
<DDD/> ZZZ DDD
</ZZZ>
</BBB>
<XXX> CCC
<DDD> XXX DDD
<CCC/>
FFF
<FFF/>
</DDD>
</XXX>
<CCC> CCC DDD
<DDD/>
</CCC>
</AAA>

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 37 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Chn tt c cc phn t GGG v t tin ca n (//GGG/ancestor-or-self::*)

<AAA>
AAA
<BBB>
<CCC/>
</BBB> BBB CCC
<XXX>
<DDD>
<FFF/>
<FFF> FFF
<GGG/> XXX DDD
</FFF> FFF
</DDD>
</XXX>
GGG
<CCC>
<DDD/> CCC DDD
</CCC>
</AAA>

Chn phn t BBB u tin (//BBB[floor(1.2)])

<AAA>
AAA
<BBB>
<CCC/>
</BBB> BBB CCC
<BBB/>
</AAA>
BBB

Chn phn t BBB th hai (//BBB[ceiling(1.2)])

<AAA>
AAA
<BBB>
<CCC/>
</BBB> BBB CCC
<BBB/>
</AAA>
BBB

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 38 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Chng 4
XSL (eXtensible style sheet)
1 XSL l g?
XSL l mt ngn ng chun gip chng ta chuyn i ti liu XML thnh mt
ng dng khc nh HTML, WML (Wireless (v tuyn in) Markup
Language),... v ngay c nh dng XML khc. Ban u XSL c thit k
sinh ra HTML nhng dng khc nhau ty theo style sheet. Nhng by gi
XSL rt hu ch cho vic chuyn i nh dng ca ti liu XML.
Hin ti c mt phin bn mi ca XSL l XSLT(eXtensible style sheet
transformations).
Trong chng trc chng ta tm hiu v XPath, XPath gip cho chng ta
i li trn cc phn t ca mt ti liu XML. Nhng lm cho mt ti liu
XML tr nn hu ch v d dng pht trin th s kt hp giu XPath v XSL
l khng th thiu
bit c XSL lm vic nh th no v s kt hp nh th no, chng
ta ln lc tm hiu mt s c php ca XSL.
2 Qui tc chung
Bn thn XSL cng l mt XML well-formed nhng n cha nhng lnh ca
chnh n v d liu HTML dng y nguyn cho d liu ra. V vy chng ta phi
tun th mi quy tc ca mt XML well-formed.
trnh phn tch XML nhn din c cc lnh ca XSL th chng ta cn
phi khai bo mt namespace trong phn t gc. Mt style sheet thng
cha mt trong hai namespace:
Namespace nguyn thy: http://www.w3.org/TR/WD-xsl
Namespace ca XSLT: http://www.w3.org/1999/XSL/Transform

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

Phn t gc trong ti liu XSL thng l mt phn t xsl:stylesheet, n cha


mt hay nhiu phn t xsl:template
V d, chng ta c file test.xsl sau:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE>Northwind Home Page</TITLE>
</HEAD>
<BODY>
<P>Customer Order</P>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

Thuc tnh match trong phn t template ch ra node xut pht.


TRUNG TM CNG NGH PHN MM THA THIN HU Trang 39 / 59
a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

tham chiu file mt ti liu xsl vo trong ti liu XML bng cch thm vo
u ti liu XML dng:
<?xml-stylesheet type=text/xsl href=URI/URL?>
Trong URI/URL l a ch ca ti liu xsl m chng ta mun tham chiu

<?xml-stylesheet type=text/xsl href=test.xsl?>

3 Mt s phn t(element) thng dng ca XSL


3.1 Phn t value-of
Phn t value-of c chc nng chn gi tr ca mt phn t hay mt thuc
tnh no trong ti liu XML ha n vo ti liu xut. value-of s dng
mt thuc tnh select c gi tr l mt biu thc XPath trch ra mt phn
t. Kt qu l kt qu ca vic thc hin biu thc XPath.
V d:

Ti liu XSL lu vi tn test.xsl XML

1 <xsl:stylesheet xmlns:xsl = <?xml version="1.0"?>


"http://www.w3.org/1999/XSL/Transform" version =
<?xml-stylesheet type="text/xsl"
"1.0" >
href="test.xsl" ?>
2 <xsl:output method = "html" /> <AAA >
<BBB>10 </BBB>
3 <xsl:template match = "/" >
<BBB>5 </BBB>
<HTML>
4 <BBB>7 </BBB>
<HEAD>
<TITLE>value-of</TITLE> </AAA>
</HEAD>
<BODY>

5 <xsl:value-of select = "//BBB[1]" /><BR/> Kt qu hin th trn trnh duyt

6 <xsl:value-of select = "//BBB[2]" /> <BR/> 10


5
7 <xsl:value-of select = "//BBB[3]" />
7
8 </BODY>
</HTML>

9 </xsl:template>

10 </xsl:stylesheet>

Gii thch v d:
Dng 1: Phn t stylesheet dng khai bo namespace, bo cho trnh phn
tch bit y l phin bn XSLT.
Dng 2: Khai bo kiu d liu ra, kiu d liu ra l di dng HTML
Dng 3: Khai bo phn t template chnh v cho bit v tr khi u l phn t
gc
TRUNG TM CNG NGH PHN MM THA THIN HU Trang 40 / 59
a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Dng 4 Cc th m HTML
Dng 5, 6, 7: Chn ni dung ca phn t BBB th 1, 2, 3
Dng 8: Cc th ng HTML
Dng 9: Th ng phn t template chnh
Dng 10: Th ng ca phn t stylesheet
3.2 Phn t attribute
Phn t ny gip chng ta a thm mt thuc tnh vo vo mt phn t no
trong h s kt qu vi mt tr s ly t ti liu XML.
V d:

Ti liu XSL lu vi tn test.xsl XML

1 <?xml version="1.0"?> <?xml version="1.0"?>


<?xml-stylesheet type="text/xsl"
2 <xsl:stylesheet
href="test.xsl" ?>
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <Order OrderNo="1">
<OrderDate>2002-03-26
3 <xsl:template match="/">
</OrderDate>
4 <A>
<Item>
5 <xsl:attribute <Product ProductID="1"
name="HREF">Products.php?ProductID= UnitPrice="70">Chair
<xsl:value-of </Product>
select="Product/@ProductID"/> </Item>
</xsl:attribute> </Order>

6 <xsl:value-of select="Product"/> Kt qu hin th trn trnh duyt

7 </A> Chair

8 </xsl:template>

9 </xsl:stylesheet>

Gii thch v d:
Dng 5: To mt thuc tnh c tn l HREF cho phn t A dng 4.
Kt qu s cho ra t Chair, t ny c link l Products.php?ProductID=1
3.3 Phn t attribute-set
Phn t ny dng to ra mt tp cc thuc tnh. Phn t ny c hai
thuc tnh:
name: Tn ca tp thuc tnh
use-attribute-sets: Nu thuc tnh ny c s dng th gi tr ca n s
l mt tn ca mt tp thuc tnh khc b sung vo cho tp thuc tnh ny
Cc phn t con ca phn t ny l cc phn t attribute
V d:Xem v d mc 1.2.4.
TRUNG TM CNG NGH PHN MM THA THIN HU Trang 41 / 59
a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

3.4 Phn t element


Phn t ny cho php chng ta thm mt phn t vo ti liu kt qu. Phn
t ny c 3 thuc tnh:
name: Gi tr l mt tn ca phn t cn nh ngha
namespace: Gi tr l mt khng gian tn
use-attribute-set: Gi tr ca n l mt hoc nhiu tn ca cc phn t
attribute hay attribute-set khc (c ngha l chng ta mun dng cc thuc
tnh c nh ngha trong cc phn t attribute).
V d:

Ti liu XSL lu vi tn test.xsl XML

1 <xsl:stylesheet xmlns:xsl = <?xml version=1.0


"http://www.w3.org/1999/XSL/Transform" version = encoding=utf-8?>
"1.0" >
<?xml-stylesheet type="text/xsl"
2 <xsl:output method = "xml" indent = "yes" /> href="test.xsl" ?>
<AAA >
3 <xsl:attribute-set name = "xxx" > <BBB>bbb </BBB>
<CCC>ccc </CCC>
</AAA>

4 <xsl:attribute name = "a" >1</xsl:attribute> KT QU

5 <xsl:attribute name = "b" >2</xsl:attribute> <?xml version="1.0"


encoding="utf-8"?>
6 </xsl:attribute-set>
<QQQ a="1" b="2" cc="33"
dd="44" xxx="555"/>
7 <xsl:attribute-set name = "yyy" use-attribute-sets
= "xxx" >

8 <xsl:attribute name = "cc" >33


</xsl:attribute>

9 <xsl:attribute name = "dd" >44


</xsl:attribute>

10 </xsl:attribute-set>

11 <xsl:template match = "/" >

12 <xsl:element name = "QQQ" use-attribute-


sets = "yyy" >

13 <xsl:attribute name = "xxx" >555


</xsl:attribute>

14 </xsl:element>

15 </xsl:template>

16 </xsl:stylesheet>

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 42 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Gii thch v d:
Dng 3: Thit lp tp thuc tnh, tp c tn l xxx
Dng 4, 5: Thit lp hai thuc tnh a v b cho tp thuc tnh xxx
Dng 6: Thit lp tp thuc tnh, tp c tn l yyy, ngoi cc thuc tnh c
thit lp dng 8, 9 cn s dng thm tp thuc tnh xxx.
Dng 8, 9: Thit lp 2 thuc tnh cc v dd cho tp thuc tnh yyy
Dng 11: Ch nh phn t gc
Dng 12: Thit lp phn t QQQ c cc thuc tnh ngoi thuc tnh c thit
lp trong dng 13 cn s dng thm tp thuc tnh yyy
Kt qu l to ra mt ti liu XML, ti liu ny c mt Phn t l QQQ v c
cc thuc tnh l a="1" b="2" cc="33" dd="44" xxx="555".
3.5 Phn t apply-templates
Khi mt style sheet cha nhiu phn t template, chng ta c th p dng
chng vo mt khung trnh by no bng cch s dng phn t apply-
templates. Chng ta cn to ra mt phn t template cha phn t apply-
templates, n s ly kt qu ca cc template nm bn ngoi template cha
n a vo khung trnh by ca n. Nu trng hp khng c template
no ngoi c p dng th n s t ly kt qu ca chnh bn thn n.
Tht l kh hiu, d hiu hn chng ta xem cc v d sau:

Ti liu XSL lu vi tn test.xsl XML

1 <xsl:stylesheet xmlns:xsl = <?xml version=1.0?>


"http://www.w3.org/1999/XSL/Transform" version =
<?xml-stylesheet type="text/xsl"
"1.0" >
href="test.xsl" ?>
2 <xsl:output method = "html" /> <AAA >
<HTML> <BBB>10 </BBB>
<TABLE> <BBB>5 </BBB>
<BBB>7 </BBB>
3 <xsl:template match = "/" >
</AAA>
<TR>
<TD>

4 <xsl:apply-templates select = "//BBB" /> M HTML KT QU

5 </TD> <HTML>
</TR> <TABLE>
</xsl:template> <TR>
</TABLE> <TD>BBB[1]: 10
</HTML> </TD>
<TD> BBB[2]: 5
6 <xsl:template match = "BBB" >
</TD>
7 BBB[ <xsl:value-of select = "position()" /> ]:
<TD> BBB[3]: 7
<xsl:value-of select = "." />

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 43 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

</TD>
8 </xsl:template>
</TR>
9 </xsl:stylesheet> </TABLE>
</HTML>

Ti liu XSL lu vi tn test.xsl XML

1 <xsl:stylesheet xmlns:xsl = <?xml version=1.0?>


"http://www.w3.org/1999/XSL/Transform" version =
<?xml-stylesheet type="text/xsl"
"1.0" >
href="test.xsl" ?>
2 <xsl:output method = "html" /> <AAA >
<HTML> <BBB>10 </BBB>
<TABLE> <BBB>5 </BBB>
<BBB>7 </BBB>
3 <xsl:template match = "/" >
</AAA>
<TR>
<TD>

4 <xsl:apply-templates select = "//BBB" /> M HTML KT QU

5 </TD> <HTML>
</TR> <TABLE>
</xsl:template> <TR>
</TABLE> <TD> 10 </TD>
</HTML> <TD>5</TD>
<TD> 7</TD>
</TR>
6 </xsl:stylesheet>
</TABLE>
</HTML>

3.6 Phn t call-template


Phn t ny c dng triu gi mt xsl:template bi tn ca
xsl:template ny.
V d:

Ti liu XSL lu vi tn test.xsl XML

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 44 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

1 <xsl:stylesheet xmlns:xsl = <?xml version=1.0?>


"http://www.w3.org/1999/XSL/Transform" version =
<?xml-stylesheet type="text/xsl"
"1.0" >
href="test.xsl" ?>
<xsl:output method = "text" />
<AAA >
<BBB>bbb </BBB>
<CCC>ccc </CCC>
</AAA>

2 <xsl:template match = "/" > Kt qu hin th trn trnh duyt


<xsl:call-template name = "print" />
bbb ccc
</xsl:template>

3 <xsl:template name = "print" >


<xsl:value-of select=.>
</xsl:template>
</xsl:stylesheet>

3.7 Phn t for-each


Phn t for-each dng i qua tt c cc phn t c ch nh ra trong
thuc tnh select (for-each lm vic cng ging nh lnh for ca cc ngn ng
lp trnh).
V d:

Ti liu XSL lu vi tn test.xsl XML

1 <xsl:stylesheet xmlns:xsl = <?xml version=1.0?>


"http://www.w3.org/1999/XSL/Transform" version =
<?xml-stylesheet type="text/xsl"
"1.0" >
href="test.xsl" ?>
2 <xsl:output method = "text" /> <AAA >
<BBB>TT </BBB>
3 <xsl:template match = "/" >
<BBB>CN</BBB>
4 <xsl:for-each select = "//BBB" > <BBB>PM</BBB>
<BBB>TTH</BBB>
</AAA>

5 <xsl:value-of select = "." /> Kt qu hin th trn trnh duyt

6 </xsl:for-each> TT
CN
7 </xsl:template>
PM
8 </xsl:stylesheet> TTH

Gii thch v d
Dng 4: Phn t for-each s cho php duyt qua ht tt c cc phn t BBB
Dng 5: Phn t value-of s ly ni dung ca phn t BBB hin thi.
TRUNG TM CNG NGH PHN MM THA THIN HU Trang 45 / 59
a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

3.8 Phn t if
Phn t if l mt phn t dng kim tra iu kin ca mt biu thc logic,
nu biu thc logic c fa tr true th cc phn t bn trong phn t if s c
thc hin v ngc li th khng (cch lm vic ca n cng ging nh cu
lnh if trong cc ngn ng lp trnh khc). Phn t ny c thuc tnh tn l
test thuc tnh ny chc biu thc iu kin. Biu thc ny c th l mt biu
thc so snh hoc mt biu thc XPath, kt qu l true khi kt qu ca biu
thc nhn mt trong cc gi tr sau:
Mt nt c t nht mt nt
Mt con s khc khng
Mt mnh cy
Mt chui khng phi l rng
V d:

Ti liu XSL lu vi tn test.xsl XML

1 <xsl:stylesheet xmlns:xsl = <?xml version=1.0?>


"http://www.w3.org/1999/XSL/Transform" version
<?xml-stylesheet type="text/xsl"
= "1.0" >
href="test.xsl" ?>
<xsl:output method = "text" />
<AAA >
3 <xsl:template match = "BBB | CCC" > <BBB bbb = "111" >B-1 </BBB>
<BBB bbb = "222" >B-2 </BBB>
4 <xsl:if test = "position()=1" >
<CCC>222 </CCC>
<xsl:value-of select = "name()" />
<CCC>333 </CCC>
<xsl:text > : </xsl:text>
<CCC>111 </CCC>
</xsl:if>
</AAA

Kt qu hin th trn trnh duyt

BBB : B-1 B-2 222 333 111

5 <xsl:value-of select = "." />

6 </xsl:template>
</xsl:stylesheet>

Gii thch v d
Dng 3: Dng d ch ra node khi u ca qu trnh trch d liu l node BBB
hoc CCC
Dng 4: Kim tra xem node hin ti c phi l node th 1 hay khng, nu l
node c v tr 1 th ly tn ca node ny v du : v ngc li th khng.
Dng 5: Ly ni dung ca node hin thi.
3.9 Phn t iu khin choose
y l phn t iu khin chn la, n lm vic ging nh cu lnh switch
trong ca mt s ngn ng lp trnh. Cc chn la trong phn t iu khin
choose l cc phn t xsl:when (ging nh case trong trong cu lnh switch
TRUNG TM CNG NGH PHN MM THA THIN HU Trang 46 / 59
a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

ca ngn ng C) v phn t xsl:otherwise (Ging nh default trong cu lnh


switch ca ngn ng C).
Phn t choose khng c thuc tnh, phn t xsl:when c mt thuc tnh test,
gi tr ca n l mt biu thc, phn t xsl:otherwise khng c thuc tnh.
d hiu hn chng ta xem v d sau:

Ti liu XSL lu vi tn test.xsl XML

1 <xsl:stylesheet xmlns:xsl = <?xml version ="1.0"?>


"http://www.w3.org/1999/XSL/Transform" version =
<?xml-stylesheet type="text/xsl"
"1.0" >
href="test.xsl" ?>
<xsl:output method = "text" />
<AAA >
2 <xsl:template match = "BBB" > <BBB>10 </BBB>
<BBB>5 </BBB>
3 <xsl:choose >
<BBB>7 </BBB>
4 <xsl:when test = ".=7" >
</AAA>
<xsl:text >test=7</xsl:text>
</xsl:when>

5 <xsl:when test = ".=5" >


<xsl:text >test=5</xsl:text>
</xsl:when>

6 <xsl:otherwise > Kt qu hin th trn trnh duyt


<xsl:text >otherwise</xsl:text>
otherwise
</xsl:otherwise>
test=5
7 </xsl:choose> test=7

8 </xsl:template>
</xsl:stylesheet>

Gii thch v d
Dng 2: Ch nh node bt u
Dng 3: Phn t la chn
Dng 4: Kim tra xem gi tr ca node hin ti c bng 7 hay khng nu bng
th cho ra cu test=7
Dng 5: Thc hin cng vic ging dng 4 nhng kim tra xem gi tr ca
node hin ti c bng 5 hay khng, nu bng th cho ra cu test=5
Dng 6: Nu hai iu kin trn khng tha th cho ra cu ortherwise
Kt qu:
Ln lt i qua 2 node BBB, u tin l node c gi tr l 10 nn cho ra cu
ortherwise tip n i qua node BBB th hai c gi tr l 5 nn cho ra cu
test=5, cui cng l i qua node BBB cui cng c gi tr l 7 nn cho ra cu
test=7.
3.10 Phn t variable
TRUNG TM CNG NGH PHN MM THA THIN HU Trang 47 / 59
a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Phn t ny dng khai bo mt bin. khai bo mt bin chng ta vit


theo mt trong hai cch sau:
<xsl:variable name=tn bin select=gi tr gn cho bin />
<xsl:variable name=tn bin >Gi tr gin cho bin</xsl:variable>
Mt bin c th c khai bo m khng c gi tr khi to
3.11 Phn t param
Phn t ny cng tng t nh phn t variable l khai bo mt bin
nhng hai phn t ny c mt s im khc nhau. Phn t param khi chng
ta khai bo gi tr khi gn cho n ch l mt gi tr default, gi tr ca bin c
th c thay i bi phn t with-param (phn t with-param dng gn
gi tr cho bin c khai bo bi phn t param).
V d:

Ti liu XSL lu vi tn test.xsl XML

1 <xsl:stylesheet xmlns:xsl = <?xml version=1.0 ?>


"http://www.w3.org/1999/XSL/Transform" version =
<?xml-stylesheet type="text/xsl"
"1.0" >
href="test.xsl" ?>
<xsl:output method = "text" />
<AAA >
<BBB>bbb </BBB>
<CCC>ccc </CCC>
</AAA>

2 <xsl:template match = "/" > Kt qu hin th trn trnh duyt

3 <xsl:call-template name = "print" > 11 + 33 = 44


55 + 111 = 166
4 <xsl:with-param name = "A" >11
</xsl:with-param>
<xsl:with-param name = "B" >33
</xsl:with-param>

5 </xsl:call-template>

6 <xsl:call-template name = "print" >

7 <xsl:with-param name = "A" >55


</xsl:with-param>

8 </xsl:call-template>

9 </xsl:template>

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 48 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

10 <xsl:template name = "print" >


<xsl:param name = "A" />
<xsl:param name = "B" >111</xsl:param>
<xsl:text ></xsl:text>
<xsl:value-of select = "$A" />
<xsl:text > + </xsl:text>
<xsl:value-of select = "$B" />
<xsl:text > = </xsl:text>
<xsl:value-of select = "$A+$B" />
</xsl:template>

11 </xsl:stylesheet>

Gii thch v d:
Dng 2: To phn t xsl:template, phn t ny c hai phn t con l xsl:call-
template
Dng 3: To phn t xsl:call-template triu gi phn t template c tn l
print, phn t call-template c hai phn t con xsl:param
Dng 4: Gn gi tr cho bin A =11 v bin B=33
Dng 7: Tng t nh dng 3, phn t ny c mt phn t con xsl:param
dng gn gi tr cho bin A=55
Dng 10: To phn t xsl:template c tn l print. Phn t ny c cc phn t
con thc hin cc chc nng sau:
o Khai bo bin A (khng c gi tr khi to)
o Khai bo bin B (vi gi tr khi to l 111)
o Cho ra gi tr ca bin A
o Cho ra du +
o Cho ra gi tr ca bin B
o Cho ra du =
o Cho ra tng ca 2 bin A v B
Cc buc thc hin:
o Gi n phn t template c tn l print, gn gi tr cho bin A=11, B=33
v thc hin cng hai bin A v B
o Gi n phn t template c tn l print, gn gi tr cho bin A=55 thc
hin cng hai bin A v B
3.12 Phn t include
Phn t ny lm vic ging nh cu lnh include trong mt s ngn ng lp
trnh (C, PHP...), tc l phn t ny c chc nng chn on ca file xsl c
ch ra trong thuc tnh href ca phn t include vo ngay phn t include, c
ngha l n thc hin php th.
3.13 Phn t import

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 49 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Phn t ny lm vic cng ging nh phn t include, nhng chng ta cn


lu l phn t import phi l phn t con u tin ca phn t stylesheet
V d:

Ti liu XSL lu vi tn test.xsl xslt33.xslt

<xsl:stylesheet xmlns:xsl = <xsl:stylesheet xmlns:xsl =


"http://www.w3.org/1999/XSL/Transform" "http://www.w3.org/1999/XSL/Transform"
version = "1.0" > version = "1.0" >

<xsl:import href = "xslt33.xslt" /> <xsl:output method = "text" />

<xsl:output method = "text" /> <xsl:template match = "BBB" >

<xsl:template match = "/" >

<xsl:apply-templates select = "//BBB" /> <xsl:text > BBB[</xsl:text>


<xsl:value-of select = "position()" />
<xsl:text >]: </xsl:text>
<xsl:value-of select = "." />

</xsl:template> </xsl:template>

</xsl:stylesheet> </xsl:stylesheet>

XML Kt qu hin th trn trnh duyt

<?xml version=1.0?> BBB[1]: cc


<?xml-stylesheet type="text/xsl" BBB[2]: ff
href="test.xsl" ?>
BBB[3]: aa
<AAA >
BBB[4]: fff
<BBB>cc </BBB>
BBB[5]: FFF
<BBB>ff </BBB>
BBB[6]: Aa
<BBB>aa </BBB>
BBB[7]: ccCCC
<BBB>fff </BBB>
<BBB>FFF </BBB>
<BBB>Aa </BBB>
<BBB>ccCCC </BBB>
</AAA>

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 50 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Chng 5
XLink v XPointer
1 XLink
1.1 XLink l g?
Xlink (XML Linking Language) l mt ngn ng h tr cho lin kt ti liu XML
mt cch rt tng qut.
Siu lin kt HTML cung cp mt s th nh <A>, <IMG> mi c kh nng
to lin kt. Nhng lin kt ny ch l lin kt mt chiu, HTML cho php tin
ch khng cho quay lui, tc l khi chng ta link n mt trang no th
chng ta khng th no i ngc li trang trc (nu khng s dng
History ca trnh duyt hay mt s ngn ng khc). XLink cho php to lin
kt n mt phn (ging nh boockmark ca HTML) hoc ton b ti liu theo
nhiu hnh thc khc nhau. XLink cho php lin kt mt chiu hoc nhiu
chiu.
XLink cn c s h tr ca XPointer v XPath c th tr n mt cch
chnh xc tng vng d liu do XPointer v XPath nh v.
1.2 Cch to lin kt trong XLink
Khng ging nh HTML, XLink khng quy nh mt phn t lin kt no c,
n ph thuc vo thuc tnh lin kt c ch ra. Chng ta cn phi nh
ngha mt khng gian tn cho cc phn t lin kt trnh phn tch phn bit
c u l XLink, khai bo khng gian tn vi URL:
http://www.w3.org/1999/xlink

<zvon xmlns:xlink = "http://www.w3.org/1999/xlink"


xlink:type="simple"
xlink:href="zvon.gif">Click here</zvon>

Chng ta khng nht thit phi ly tip u ng ca khng gian tn XLink l


xlink, chng ta c th dng bt k nhng dng tn xlink s d phn bit hn.
Trong v d trn chng ta thy c s dng thuc tnh type
(xlink:type=simple). y chnh la thuc tnh quy nh kiu XLink.
C tt c 7 kiu XLink c nh ngha thng qua gi tr ca thuc tnh
xlink:type:

Gi tr M t

simple Lin kt n gin, lin kt ny ging nh lin kt trong HTML

extended Lin kt m rng

locator nh v

arc Cung lin kt

reource Ti nguyn lin kt

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 51 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Title Tiu lin kt

None Ty bin

1.2.1 Lin kt n gin (simple)


y ch l mt lin kt n gin ging nh lin lt trong HTML, kt hp vi
thuc tnh xlink:type c cc thuc tnh sau:

Tn thuc tnh Gi tr M t

xlink:href L mt a ch cn link n a ch cn link n

xlink:show new, replace. embed

xlink:actuate onLoad, onRequest

Khi thuc tnh xlink:show c thit lp l new th link ny s c m ra vi


mt ca s mi. thuc tnh xlink:show c th c kt hp vi thuc tnh
xlink:actuate lm cho lin kt a dng hn:
xlink:actuate=onLoad th link ny s t ng c gi, tc l khng cn phi
click vo link ny, cn nu xlink:actuate=onRequest th link s khng c
gi mt cch t ng

<zvon:logo xmlns:zvon = "http://www.zvon.org"


xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="simple"
xlink:href="zvon.gif"
xlink:show="new"
xlink:actuate="onLoad">
</zvon:logo>

Khi thuc tnh xlink:show c thit lp l replace th ni dung ca link nay s


c thay th ngay trn trang hin ti, khi kt hp vi thuc tnh
xlink:actuate=onLoad th link ny s uc t ng gi

<zvon:doclink xmlns:zvon = "http://www.zvon.org"


xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="simple"
xlink:href="xml5_out.xml"
xlink:show="replace"
xlink:actuate="onLoad"> After clicking on this link the following example
will
open in this window.
</zvon:doclink>

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 52 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Khi thuc tnh xlink:show c thit lp l embed th ti liu link ny s c


nhng vo ti liu hin hnh, chng ta c th hnh dung n ging nh lin kt
<IMG> trong HTML

<zvon:logo xmlns:zvon = "http://www.zvon.org"


xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="simple"
xlink:href="zvon.gif"
xlink:show="embed"
xlink:actuate="onLoad">
Mozilla M17 users:
This feature is not yet implemented,
otherwise you will see the picture here.
</zvon:logo>

v thuc tnh xlink:actuate cng c s dng ging nh trn.


1.2.2 Lin kt m rng (extended)
XLink m rng dng lin kt nhiu ngun ti liu khc nhau t nhng
ngun khc nhau. XLink m rng l tp cc nh ngha bao gm quan h gia
ti nguyn ngun v ti nguyn ch. C hai loi ti nguyn lin kt m rng
c ch ra bi thuc tnh xlink:type, l resource (ti nguyn cc b) v
locator (ti nguyn xa).
Ti nguyn cc b c nh ngha trc tip bn trong lin kt cn ti nguyn
xa c tham chiu n thng qua a ch URL/URI.
<WEBSITE xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="extended">
<NAME xlink:type="resource">Cafe au Lait</NAME>
<HOMESITE xlink:type="locator
xlink:href="http://ibiblio.org/javafaq/"/>
<MIRROR xlink:type="locator
xlink:href="http://sunsite.kth.se/javafaq"/>
<MIRROR xlink:type="locator
xlink:href="http://sunsite.informatik.rwth-aachen.de/javafaq/"/>
<MIRROR xlink:type="locator"
xlink:href="http://sunsite.cnlab-switch.ch/javafaq/"/>
</WEBSITE>

V d trn chng ta nh ngha mt WEBSITE gm mt ti nguyn cc b


v 4 ti nguyn xa. Khi hin th trn ng dng hay trnh duyt th ni dung
ca ti nguyn cc b s c hin th v khi ngi dng kch hot lin kt th
cc a ch lin kt s c chn triu gi. Nhng y mi ch l c s l
thuyt v cha c trnh duyt no h tr iu ny.

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 53 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Chng ta c th hnh dung mi ngun ti nguyn l mt nh v s kt ni


gia mt nh n mt hay nhiu nh khc ngi ta gi l cung lin kt, mi
nh c mt tn gi c t bi thuc tnh xlink:role. C 3 loi cung lin kt,
l cung kt ni, cung kt ni nhiu nh, cung kt ni t hp.
1.2.3 Cung lin kt
Mt phn t l cung lin kt khi thuc tnh xlink:type c nhn gi tr l arc.
By gi chng ta s tm hiu tng loi cung lin kt.
1.2.3.1 Cung kt ni
Cung kt ni l s ni kt gia mt nh ti nguyn ny vi mt nh ti
nguyn khc, gia hai nh kt ni th c mt ti nguyn ngun v mt ti
nguyn ch c phn bit nh vo thuc tnh xlink:from v xlink:to ca
XLink.
<WEBSITE xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="extended">
<NAME xlink:type="resource" xlink:role=source>Cafe au
Lait</NAME>
<HOMESITE xlink:type="locator xlink:href="http://ibiblio.org/javafaq/"
xlink:role=ibiblio
/>
<MIRROR xlink:type="locator xlink:href="http://sunsite.kth.se/javafaq"
xlink:role=sunsite-kth
/>
<MIRROR xlink:type="locator
xlink:href="http://sunsite.informatik.rwth-aachen.de/javafaq/"
xlink:role=sunsite-informatik
/>
<MIRROR xlink:type="locator"
xlink:href="http://sunsite.cnlab-switch.ch/javafaq/"
xlink:role=sunsite-cnlab
/>
<LINK-TO >
xlink:type=arc
xlink:from= source xlink:to= ibiblio
xlink:show=replace xlink:actuate=onRequest
</LINK-TO>
</WEBSITE>

1.2.3.2 Cung kt ni nhiu nh

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 54 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

Trong trng hp chng ta mun ni kt t mt nh n ng thi nhiu nh


bng cch chng ta t tn cho cc nh mun ni kt n cng mt tn. Loi
ni kt ny gi l cung kt ni nhiu nh.
Vic x l cc cung lin kt l do trnh ng dng hay trnh duyt quyt nh.
<WEBSITE xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="extended">
<NAME xlink:type="resource" xlink:role=source>Cafe au
Lait</NAME>
<HOMESITE xlink:type="locator xlink:href="http://ibiblio.org/javafaq/"
xlink:role=multi-con
/>
<MIRROR xlink:type="locator xlink:href="http://sunsite.kth.se/javafaq"
xlink:role=multi-con
/>
<MIRROR xlink:type="locator
xlink:href="http://sunsite.informatik.rwth-aachen.de/javafaq/"
xlink:role=multi-con
/>
<MIRROR xlink:type="locator"
xlink:href="http://sunsite.cnlab-switch.ch/javafaq/"
xlink:role=multi-con
/>
<LINK-TO >
xlink:type=arc
xlink:from= source xlink:to=multi-con
xlink:show=replace xlink:actuate=onRequest
</LINK-TO>
</WEBSITE>

1.2.3.3 Cung kt ni t hp
Nu chng ta khng mun ch ra mt cung c th no th trong nh ngha
cung chng ta khng cn s dng n thuc tnh xlink:to, nh vy cc cung
lin kt l mt s t hp ca cc nh

<LINK-TO >
xlink:type=arc
xlink:from= source
xlink:show=replace xlink:actuate=onRequest
</LINK-TO>

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 55 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn
http://www.ebook.edu.vn Cn bn v XML

2 XPointer(XML Pointer Language)


2.1 XPointer l g?
Nh trong chng 2 chng ta tm hiu v XPath, XPath gip cho chng ta
trch ra mt phn t no trong ti liu XML, XPath l mt ngn ng nh v
nhng n khng gip cho chng ta i i su vo ni dung ca tng phn t
m n nh v c phn t.
V d, khi chng ta dng c php ca XPath trch ra ni dung ca mt phn
t B no , nhng n khng th no gip cho chng ta i vo tng v tr ca
ni dung m n trch lc c .
V vy s ra i ca XPointer s gip cho chng ta gii quyt c iu ny.
XPointer c xy dng da trn c s ca XPath.
2.2 nh v v tr d liu
XPointer nh v mt v tr d liu da trn im tr. C hai loi im tr l
im tr node v im tr k t, v tr im tr c bt u tnh t 0.
Khi chng ta mun tr n mt phn t trong ti liu XML chng ta dng im
tr node v mun tr n tng v tr ca ni dung ti liu chng ta dng im
tr k t. Cc im tr da vo v tr ch nh. Nu tp d liu chng ta ch nh
l gm nhiu phn t con th ch s xc nh v tr im tr node cn nu d
liu khng cha cc phn t th ch s xc nh im tr k t.
Chng ta dng hm pointer() nh v d liu, XPointer c th c s dng
chung vi a ch URL/URI sau k hiu #.
V d:

<link xmlns:xlink="http://www.w3.org/2000/xlink" xlink:type="simple"


xlink:href= "mydocument.xml#xpointer(//AAA/BBB[1])">
</link>

TRUNG TM CNG NGH PHN MM THA THIN HU Trang 56 / 59


a ch: S 6 L Li, Hu
in thoi: (054).849.499 Fax: (054).849.498
Email: huesoft@dng.vnn.vn website: http://www.huesoft.com.vn