You are on page 1of 71

CHNG VIII:

XML
L thuyt 3 tit
Thc hnh 6 tit
C BN V XML
Gii thiu XML
Cu trc ca ti liu XML

Gii thiu XML
XML l vit tt cho ch eXtensible Markup
Language.
XML l ngn ng nh du tng i l mi,
c pht trin t mt ngn ng nh du
Standard Generalized Markup Language
(SGML).
Cu trc ca ti liu XML
Gm nhiu phn t (element), mi thnh phn
c bt u vi mt th bt u (start-tag) v
mt th kt thc (end-tag).
Gia start-tag v end-tag l ni dung ca phn
t ny .
Ni dung c th bao gm d liu vn bn hay
c th l mt phn t khc.
Cu trc ca ti liu XML (Vd)
<?xml version=1.0?>
<Catalog>
<Product>
<ProductID>F10</ProductID>
<ProductName>Shimano</ProductName>
<ListPrice>47.76</ListPrice>
</Product>
</Catalog>

Cu trc ca ti liu XML (tt)
Mt ti liu XML khng c tn ti mt th no
khng ng quy nh .
Khi xy dng ti liu XML cn phi tun th theo
mt s quy lut no , nu tun th ng quy lut
th c gi l well-formed.
Mt ti liu XML well-formed cha chc l mt ti
liu hp l, n ch c xem l hp l nu m bo
nhng quy tc trong ti liu Document Type
Definition (DTD) hay gin (schema)
Mt ti liu XML well-formed
Phi c mt root Element duy nht, gi l
Document Element.
Mi tag m phi c mt tag ng tng ng.
Tags trong XML th case sensitive ( phn bit
ch hoa ch thng).
Mi child Element phi nm trong Element
cha .
Gi tr thuc tnh trong XML phi c bao
trong mt cp ngoc kp hay mt cp ngoc
kp .
Processing Instructions v Comments
Processing Instructions (ch th v cch ch
bin) nm trong cp tag <? V ?> .
Mc d mt ti liu XML well-formed khng
cn c mt Processing Instruction, nhng
thng thng ta s hng u ti liu,
c gi l prologue
Processing Instructions v Comments
<?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>
</Order>
Processing Instructions v Comments
C mt loi Processing Instruction khc cng
kh thng dng l cho bit tn stylesheet ca
XML ny
<?xml-stylesheet type="text/xsl ref="order.xsl"?>
C th thm Comment bng cch dng cp
Tags <!-- v --> .
Namespace
Cho ta cch dng mt Elemet ni n hai
d liu khc nhau trong cng mt ti liu
XML.

Namespace
<?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>
Namespace
trnh s nhm ln, bn c th s dng
Namespace ni r tn Element y thuc v
URI (Univeral Resource Identifier) no .
Mt URI c th l mt URL hay mt ch no
nh ngha n.
Khai bo mt Namespace trong mt Element
bng cch dng Attribute xmlns khai bo
mt default namespace .

Name space
y l mt th vin sch c phn t gc l
<library>
<library>
<book>
<title>XML</title>
</book>
</library>
Name space
Khng gian tn dcs c to ra cho th vin
nh sau
<library
xmlns:dcs=http://www.incoll4.edu.vn/lib>
<book>
<title>XML</title>
</book>
</library>
Name space
p dng khng gian tn dcs cho tt c cc th no
trong ti liu m ta mun dng chng duy nht v
c quyn v dcs
<dcs:library
xmlns:dcs=http://www.incoll4.edu.vn/lib>
<dcs:book>
<dcs:title>XML</dcs:title>
</dcs:book>
</dcs:library>
Namespace
<?xml version="1.0"?>
<BookOrder OrderNo="1234">
<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>
<dcs:library
xmlns:dcs=http://www.incoll4.edu.vn/lib
xmlns:amazon=http://www.amazon.com/lib
<dcs:book>
<dcs:title>XML</title>
</dcs:book>
<amazon:book>
<amazon:title>XML</amazon:title>
</amazon:book>
</dcs:library>
XY DNG TI LIU XML

Document Type Define (DTD)
XML schema
Document Type Define (DTD)
DTD l mt b nhng qui tc c php cho
nhn. N cho bn bit nhng nhn no bn c
th s dng trong mt ti liu, chng nn
c xp theo th t no, nhng nhn no c
th xut hin bn trong cc nhn khc, nhng
nhn no c cc thuc tnh,...
Mt DTD c th l mt b phn ca mt ti
liu XML, nhng thng n l mt ti liu
ring bit hoc mt loi cc ti liu.
Document Type Define (DTD)
<?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>
Document Type Define (DTD)
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


Document Type Define (DTD)
V file note.dtd

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


http://www.scribd.com/doc/40179716/Bai02-XML

XML schema
XML Schema m t cu trc ca ti liu
XML, thay th cho DTD
XML Schema m t:
Cc phn t v thuc tnh trong ti liu XML
Th t v s lng cc phn t con
Cc kiu d liu ca phn t v thuc tnh

L do s dng XML Schema
XML Schema l mt s thay th cho DTD
H tr nhiu loi d liu
S dng c php XML
Bo ton s giao tip d liu
Rng buc kha v tham chiu mnh hn DTD
Tch hp vi namespace
XML Schema
Cu trc chung ( thng dng ) ca cc ti liu trong
XML Shema nh sau
<?xmlversion="1.0"encoding="utf-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
c t cc th
c t cc kiu
</xs:schema>
http://www.scribd.com/doc/38247160/Giao-Trinh-XML

XML Schema (V d
<?xml version="1.0"?>
<xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema
targetNamespace=http://www.w3schools.com
xmlns=http://www.w3schools.com
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Schema v Namespace
<schema xmlns=http://www.w3.org/2001/XMLSchema
targetNamespace=http://xyz.edu/Admin>
..
</schema>
http://www.w3.org/2001/XMLSchema:
namespace ca cc t kha dng trong s xc
nh lc XML, v d: schema,
targetNamespace,
targetNamespace: nh ngha namespace ca
lc c xc nh trong ti liu
<schema></schema> trn

Tham chiu n mt XML Schema
<?xml version="1.0"?>
<note xmlns=http://www.w3schools.com
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation="http://www.w3schools.com note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
xsi:schemaLocation ngha l lc ca namespace
http://www.w3schools.com c tm thy
http://www.w3schools.com note.xsd

LP TRNH VI XML

1. Xpath
2. Document Object Model (DOM)


Xpath
Xpath ng vai tr quan trng trong cng tc
trao i d liu gia cc my tnh hay gia
cc chng trnh ng dng
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
Xpath (c php n gin)

/: 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 (c php n gin)

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

- /AAA/*: Tt c cc nt con trc tip ca AAA
- /*/BBB: Tt c cc nt B cp th 2
//*: TT C CC NODE
Xpath (c php n gin)
<AAA>
<BBB/>
<BBB/>
<BBB>
<CCC>
content
</CCC>
</BBB>
<BBB/>
</AAA>

- /AAA/BBB[1]: Nt BBB th 1 (con ca AAA)
- /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 )
[ ]: 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
Xpath (c php n gin)
<AAA>
<BBB id=b1/>
<BBB id=b2/>
<CCC name=ccc/>
<CCC/>
</AAA>

@: Truy xut n thuc tnh
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
child:: : Chn tt c node con ca node hin thi
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

<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
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
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
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
<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
Document Object Model (DOM)
DOM l mt giao din ng dng (API).
DOM thng c dng l mt cy cu trc d
liu (Tree View), c dng truy xut cc
ti liu dng HTML v XML.
Document Object Model (DOM)
DOM c nhiu cp :
Cp 0 : Bao gm mi chi tit DOM theo qui nh
ca tng nh pht trin ring bit tn ti trc khi
DOM cp 1 ra i. V d: document.images,
document.forms, document.layers, v
document.all
Cp 1 : Cch duyt qua ti liu (cu trc cy) ca
DOM (HTML v XML) v thao tc ni dung
(bao gm vic thm thnh phn). Cc thnh phn
ring cho HTML cng c a vo.
Document Object Model (DOM)
Cp 2 : H tr khng gian tn XML, khung nhn
c lc (filtered views) v cc s kin DOM
Cp 3 : Nhm nng cao cc kh nng ca DOM,
cp 3 cha 6 bn m t chi tit k thut khc
nhau: 1) the DOM Level 3 Core; 2) the DOM
Level 3 Load and Save; 3) the DOM Level 3
XPath; 4) the DOM Level 3 Views and
Formatting; 5) DOM Level 3 Requirements; and
6) the DOM Level 3 Validation.
DOM (vd)

<?xml version="1.0"?>
<order number="312597">
<date>2000/1/1</date>
<customer id="216A">Company A</customer>
<item>
<part-number warehouse="Warehouse 11">
E16-25A</part-number>
<description>Production-Class
Widget</description>
<quantity>16</quantity>
</item>
</order>
XSLT
XmlDocument v XPathDocument
XmlReader v XmlWriter

XSLT
XSL (Extension Style Language) chuyn
d liu ca XML ra dng HTML .
XSL chia lm hai dng :
XSLT (XSL Transformation) : ngn ng XSL
chuyn v chuyn dch thc hin vic rt trch
d liu XML a vo khun dnh HTML .
XSLFO (XSL Formating Object) : ngn ng XSL
chuyn v nh dng cung cp cc thuc tnh
nh dng ti liu cho phong ph v a dng hn
nh font, ch, mu sc, hnh nh .
S chuyn i d liu
Cu trc tng qut file XSL
Nhng ti liu XSLT vo ti liu
XML
<?xml version=1.0 encoding=UTF-8?>
<?xml-stylesheet type=text/xsl href=path file
.xsl?>
<!--Cu trc ti liu XML -->
Mt s phn t XSLT
Template
Apply-templates
Value-of
Sort
Copy-of
Copy

XmlDocument v XPathDocument
XmlDocument l i tng ti liu .
XML Document
Cc thuc tnh ca lp
XmlDocument
Attributes : Tp cc thuc tnh ca node hin
hnh .
Childnodes : Tr v cc con ca node hin
hnh .
DocumentElement : ly phn t gc .
DocumentType
FirstChild, lastChild, nextSibling, nodeName,
nodeType, nodeValue .
Phn tch ti liu XML
Ti liu np t tp tin v t ng to cy
DOM :
XmlDocument doc = new XmlDocument();
doc.Load(xmlPath);
Lu tr phn t gc :
XmlNode rootNode = doc.DocumentElement;
Duyt ti liu XML
XPathDocument
XPathDocument tng thch cho bt k thao
tc no ca lp XmlDocument.
Khi cn thao tc nhanh chng ta c th s
dng XPathDocument .
V d :
XmlReader v XmlWriter

XmlReader
Mt s thuc tnh ca XmlReader :
AttributeCount .
EOF .
Depth : Xc nh su ca node hin hnh
HasAttribute
HasValue
IsEmptyElement
Name
Value
ValueType
S dng XmlReader
Khng gian tn : Using System.Xml
M ti liu XML :
xmlFile = @Server.MapPath() + \\file_xml ;
To i tng s dng phng thc Create()
XmlReader reader = XmlReader.Create(xmlFile);
c ti liu
while(reader.Read())
{
Response.Write(reader.Name);
}
XmlWriter
Cc thuc tnh :
Settings : to i tng XmlWriterSettings
WriteState
XmlLang
XmlSpace
S dng XmlWriter
Bt u :
WriteStartDocument()
Ghi ch th : <?xml version=1.0?>
Kt thc :
WriteEndDocument()
ng cc thuc tnh v phn t
Ghi phn t
<name>
<firstName>Minh</firstName>
</name>
Writer.WriteStartElement(name);
Writer.WriteElementString(firstName,Minh)
Writer.WriteEndElement()
Ghi thuc tnh
Writer.WriteStartElement(employee);
Writer.WriteStartAttribute(id);
Writer.WriteString(1);
Writer.WriteEndAttribute();
Writer.WriteEndElement();
Kt qu :
<employee id=1 />
Bi tng hp
IV. C s d liu v XML
XML l nh dng chung cho vic thay i d
liu trn Internet
Datasets xut d liu nh XML
XML cung cp mt nh dng thun li cho
vic chuyn ni dung mt dataset n client
v ngc li .

IV. C s d liu v XML (tt)
Cc i tng XML ng b


Tng quan XML v Datasets
Cc phng thc ca i tng
DataSet
S dng ReadXml np d liu t file hoc dng
DataSet ds = new DataSet();
Ds.ReadXml(Server.MapPath(filename.xml));
S dng WriteXml ghi d liu XML vo file, stream
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(strSql,conn);
da.Fill(ds);
ds.WriteXml(Server.MapPath(filename.xml));
S dng GetXml ghi d liu vo bin chui
String strXmlDS = ds.GetXml();

You might also like