Professional Documents
Culture Documents
03 XML
03 XML
2
Topics
3
Đánh dấu
v Phương pháp phân biệt text với các chỉ dẫn trong các
hệ thống sắp chữ (typesetting system).
5
Cấu trúc XML
● Kiểu tài liệu: tài liệu XML được xem là có nhiều loại.
– Cấu trúc và bộ phận cấu thành của XML xác định các loại tài liệu.
● Một tài liệu gồm có nhiều bộ chứa - containers và giá trị dữ liệu
container đó chứa.
– các container có thể là các khai báo, các phần tử và các thuộc tính.
● Một tài liệu XML được biết như là một thực thể/một thực thể tài liệu
XML, nó biểu diễn một tập dữ liệu có thể cho một ngôn ngữ đánh dấu
đặc biệt.
<?xml version="1.0" encoding="UTF-8"?>
<BillingInformation>
<Name> Right Plastic Products </Name>
<BillingDate> 2002-09-15 </BillingDate>
<Address>
<Street> 158 Edward st. </Street>
<City> Brisbane </City>
<State> QLD </State>
<PostalCode> 4000 </PostalCode>
</Address>
</BillingInformation>
6
Bố cục một tài liệu XML điển hình
Layout of a typical
7 XML document
XML: các phần tử
● Các phần tử là khối nội dung cơ bản, bao gồm tên và nội dung phần tử.
– Một phần tử - element là một chuỗi các ký tự được bắt đầu với thẻ mở đầu và kết
thúc bởi thẻ kết thúc và có thể chứa bất cứ gì ở giữa.
<chapter number="1">
Text for Chapter 1
</chapter>
● Nội dung?
Các ký tự nằm giữa 2 thẻ.
● Phần tử cao nhất của tài liệu XML là một phần tử đơn, gọi là phần tử root.
● Các phần tử nằm trong phần tử khác được gọi là các phần tử lồng vào -
nested elements.
– Phần tử chứa -> phân từ cha, phần tử lồng vào -> phần tử con.
8
XML: Thuộc tính
● Thuộc tính: Dữ liệu được đặt kèm cùng với thẻ mở đầu.
– Thuộc tính được xác đinh bởi cặp tên-giá trị, liên kết với một phần tử.
– Thuộc tính được sử dụng để chỉ rõ hơn nội dung của một phần tử.
9
XML: Namespace
● Không gian tên - Namespaces dựa trên XML cung cấp tiện ích: (i) để
kết hợp các yếu tố và/hoặc các thuộc tính trong tất cả hoặc một phần
của tài liệu với một lược đồ (schema) cụ thể và (ii) để tránh xung đột
tên.
● Các khai báo không gian tên có phạm vi xác định.
– Một khai báo không gian tên được đặt trong pham vi cho phần tử mà nó
được khai báo và cho các phần tử con của nó.
● Tên không gian tên và tên cục bộ của phần tử cùng nhau tạo một tên
toàn cục duy nhất được gọi là tên hợp cách - qualified name.
<?xml version=”1.0” encoding=”UTF-8”?>
<BillingInformation customer-type=”manufacturer”
xmlns="http://www.plastics_supply.com/BillInfo">
<Name> Right Plastic Products </Name>
<Address xmlns="http://www.plastics_supply.com/Addr">
<Street> 158 Edward st. </Street>
<City> Brisbane </City>
<State> QLD </State>
<PostalCode> 4000 </PostalCode> Uniform resource identifier (URI).
</Address>
<BillingDate> 2002-09-15 </BillingDate>
</BillingInformation> 10
XML: Cấu trúc
Linear sequence
● Một tài liệu XML phải có một <book>
thẻ root. <chapter n=“1”> Title 1 </chapter>
<section n=“1.1”> Section 1.1 </section>
<paragraph> …. </paragraph>
● Tài liệu XML là một khối <section n=“1.2”> Section 1.2 </section>
<chapter n=“2”> Title 2 </chapter>
thông tin có thể được xem: <section n=“2.1”> Section 2.1 </section>
– như là một chuỗi ký tự tuyến < paragraph > …. < /paragraph >
</book>
tính (linear sequence of
characters) chứa dữ liệu và Tree
các ký tự đánh dấu. Book
– như là một cấu trúc dữ liệu Chapter Chapter
trừu tượng, là một cây các
nút. Section Section Section
Para Para
XML: Cấu trúc
13
XML: Lược đồ
● Lược đồ (schema) XML tham chiếu tới một tài liệu định
nghĩa nội dung và cấu trúc để biểu diễn các tài liệu XML.
● Các lược đồ cung cấp hỗ trợ các đặc điểm meta-data bổ
sung, ví dụ như các mối quan hệ cấu trúc, các giá trị hợp
lệ và các kiểu dữ liệu.
● Mỗi kiểu lược đồ hoạt động như một phương thức biểu
diễn các đặc tính dữ liệu và áp dụng các luật và ràng buộc
cho một tài liệu XML tham chiếu.
● Một lược đồ XML mô tả các thuộc tính và phần tử có thể
chứa trong một tài liệu lược đồ hợp cách và cách các
phần tử có thể bố trí trong một cấu trúc tài liệu.
14
Ví dụ: Lược đồ XML và một tài liệu XML
File “Person.xml” File “Person.xsd”
<?xml version="1.0" <?xml version="1.0" encoding="UTF-
encoding="UTF-8"?> 8"?>
<xs:schema
<Person xmlns:xs="http://www.w3.org/2001/
xmlns:xsi="http://www.w3.org/ XMLSchema">
2001/XMLSchema-instance" <xs:element name="Person">
xsi:noNamespaceSchemaLocation=" <xs:complexType>
<xs:sequence>
Person.xsd">
<xs:element name="First“
<First>Sophie</First> type="xs:string"/>
<xs:element name="Middle“
<Last>Jones</Last> type="xs:string“
<Age>34</Age> minOccurs="0"/>
<xs:element name="Last“
</Person> type="xs:string"/>
<xs:element name="Age“
type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
15
Schema và namespace
<schema xmlns=http://www.w3.org/2001/XMLSchema
targetNamespace= http://xyz.edu/Admin >
…..
</schema>
vhttp://www.w3.org/2001/XMLSchema:
namespace của các từ khóa dùng trong sự
xác định lược đồ XML, ví dụ: schema,
targetNamespace,…
vtargetNamespace: định nghĩa namespace của
lược đồ được xác định trong tài liệu
<schema>…</schema> trên
16
Khai báo phần tử trong XML Schema
17
Tham chiếu đến một 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>
v xsi:schemaLocation nghĩa là lược đồ của namespace
http://www.w3schools.com được tìm thấy ở
http://www.w3schools.com note.xsd
18
Xây dựng lược đồ từ nhiều thành phần
<schema xmlns=http://www.w3.org/2001/XMLSchema
targetNamespace= http://xyz.edu/Admin >
<include schemaLocation= http://xyz.edu/StudentTypes.xsd >
<include schemaLocation= http://xyz.edu/ClassTypes.xsd >
<include schemaLocation= http://xyz.edu/CourseTypes.xsd >
….
</schema>
19
Khai báo phần tử trong XSD
v xmlns:xs="http://www.w3.org/2001/XMLSchema"
F Phần tử và loại dữ liệu trong lược đồ được khai báo trong
namespace http://www.w3.org/2001/XMLSchema và có tiền tố là xs
v targetNamespace=http://www.w3schools.com
F Các phần tử được định nghĩa trong lược đồ đến từ namespace
http://www.w3schools.com
v xmlns="http://www.w3schools.com"
F namespace mặc định là xmlns="http://www.w3schools.com"
v elementFormDefault="qualified
F Bất kỳ phần tử nào được sử dụng trong tài liệu XML phải từ
namespace đảm bảo chất lượng
20
Kiểu giản đơn
21
Định nghĩa một phần tử giản đơn
22
Thuộc tính trong XSD
vThuộc tính được khai báo như một kiểu đơn giản
vCác phần tử giản đơn không thể có thuộc tính.
Nếu một phần tử có thuộc tính, thì phần tử đó
được xem là phần tử phức hợp
23
Khai báo thuộc tính
xs:string xs:boolean
xs:decimal xs:date
xs:integer xs:time
24
Ràng buộc nội dung
25
Ràng buộc giá trị
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/> Kiểu miền con
</xs:restriction>
</xs:simpleType>
</xs:element>
26
Ràng buộc giá trị
<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/> Kiểu liệt kê
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
27
Ràng buộc giá trị
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/> Pattern constraint
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
28
Bảng tóm lược
Constraint Description
maxExclusive Specifies the upper bounds for numeric values (the value must be less than this value)
maxInclusive Specifies the upper bounds for numeric values (the value must be less than or equal
to this value)
maxLength Specifies the maximum number of characters or list items allowed. Must be equal to or
greater than zero
minExclusive Specifies the lower bounds for numeric values (the value must be greater than this
value)
minInclusive Specifies the lower bounds for numeric values (the value must be greater than or
equal to this value)
minLength Specifies the minimum number of characters or list items allowed. Must be equal to or
greater than zero
30
Định nghĩa một phần tử phức hợp
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
31
Định nghĩa một phần tử phức hợp
v Cách 1:
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
32
Định nghĩa một phần tử phức hợp
v Cách 2:
33
Định nghĩa phần tử rỗng
<xs:element name="product">
<xs:complexType>
<xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>
34
Phần tử chỉ chứa phần tử con
v <person>
<firstname>John</firstname>
<lastname>Smith</lastname>
</person>
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
35
Topics
36
Mở rộng kiểu phức tạp
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:PO="http://www.plastics_supply.com/PurchaseOrder"
targetNamespace="http://www.plastics_supply.com/PurchaseOrder">
<xsd:complexType name="Address">
<xsd:sequence>
<xsd:element name="Number" type="xsd:decimal"/>
<xsd:element name="Street" type="xsd:string"/>
<xsd:element name="City" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="AustralianAddress">
<xsd:complexContent>
<xsd:extension base="PO:Address">
<xsd:sequence>
<xsd:element name="State" type="xsd:string"/>
<xsd:element name="PostalCode" type="xsd:decimal"/>
<xsd:element name="Country" type="xsd:string"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>
Hạn chế kiểu phức tạp
<!-- Uses the data type declarations from Listing on page 17 -->
<xsd:complexType name="AustralianPostalAddress">
<xsd:complexContent>
<xsd:restriction base="PO:AustralianAddress">
<xsd:sequence>
<xsd:element name="Number" type="xsd:decimal"/>
<xsd:element name="Street" type="xsd:string"/>
<xsd:element name="City" type="xsd:string"
minOccurs="0" maxOccurs="0"/>
<xsd:element name="State" type="xsd:string"/>
<xsd:element name="PostalCode" type="xsd:decimal"/>
<xsd:element name="Country" type="xsd:string"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
Đa dạng (polymorphism) kiểu phức
<!-- Uses the data type declarations from Listing on page 17 -->
<xsd:complexType name="PurchaseOrder">
<xsd:sequence>
<xsd:element name="Name" minOccurs="1" maxOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:string"/>
</xsd:simpleType> Variant of the PurchaseOrder type that uses
the base type Address for its billingAddress
</xsd:element> and shippingAddress elements.
<xsd:element name="shippingAddress" type="PO:Address"
minOccurs= "1" maxOccurs="1"/>
<xsd:element name="billingAddress" type="PO:Address" minOccurs= "1"
<xsd:choice minOccurs="1" maxOccurs="1">
<xsd:element name="BillingDate" type="xsd:date"/>
<xsd:element name="ShippingDate" type="xsd:date"/>
</xsd:choice>
</xsd:sequence>
</xsd:complexType
Đa dạng (polymorphism) kiểu phức
<?xml version="1.0" encoding=”UTF-8”?>
<PO:PurchaseOrder xmlns:PO="http://www.plastics_supply.com/PurchaseOrder
An instance document can now use any
<Name> Plastic Products </Name> type derived from base type Address
<shippingAddress xsi:type="PO:AustralianAddress">
for its billingAddress and
shippingAddress elements.
<Number> 459 </Number>
PurchaseOrder type uses the
<Street> Wickham st. </Street> derived AustralianAddress
<City> Fortitude Valley </City> type as its billingAddress and the derived
AustralianPostalAddress type as its
<State> QLD </State> shippingAddress elements.
<PostalCode> 4006 </PostalCode>
<Country> Australia </country>
</shippingAddress>
<billingAddress xsi:type=="PO:AustralianAddress">
<Number> 158 </Number>
<Street> Edward st. </Street>
<State> QLD </State>
<PostalCode> 4000 </PostalCode>
<Country> Australia </Country>
</billingAddress>
<BillingDate> 2002-09-15 </BillingDate>
</PO:PurchaseOrder>
Topics
41
Chuyển đổi và duyệt tài liệu
● XML được sử dụng chính để mô tả và chứa dữ liệu. Mặc dù
cách dùng hiệu quả và rõ ràng nhất của XML là mô tả dữ liệu,
nhưng Ngôn ngữ định dạng chuyển đổi mở rộng XSLT
(eXtensible Stylesheet Language Transform) cũng có thể được
dùng để định dạng hoặc chuyển đổi nội dung XML để biểu diễn
cho người dùng.
● Tiến trình XSLT chuyển đổi cấu trúc XML sang các công nghệ
biểu diễn, như HTML hoặc bất kỳ cấu trúc và định dạng được
yêu cầu.
● XSLT sử dụng XPath (XML Path Language) để đánh địa chỉ và
định vị các phần của tài liệu XML.
● XPath là chuẩn để tạo các chỉ thị được dùng để tìm các mẫu
thông tin cụ thể bên trong tài liệu XML.
– XPath là một ngôn ngữ trừu tượng định nghĩa mô hình cây để hệ thống hoá
cấu trúc logic của tài liệu XML. 42
Ví dụ: mô hình cây XPath
Transformation service
<PurchaseOrder>
<Name> Plastic Products </Name>
<billingAddress>
<Number> 158 </Number>
<Street> Edward st. </Street> Target
<PostalCode> QLD 4000 XML
</PostalCode> application
<Country> Australia </country>
</billingAddress>
<PurchaseOrder>
44
Looks Familiar?
45
Bài tập
46
Bài 1
47