Upload
binh-trong-an
View
5.261
Download
0
Embed Size (px)
DESCRIPTION
Tổng quan về XSD
Citation preview
Chuyên đề 1: TÌM HIỂU VỀ XML
Chương 4Lược đồ XML (XML
Schema)
Trường Đại học Nha TrangKhoa Công nghệ Thông tin
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 2
Nội dung
XML Schema là gì? Khai báo phần tử trong XML
Schema Tham chiếu đến một XML Schema
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 3
XML Schema Definition (XSD)
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 4
XML Schema
XML Schema mô tả cấu trúc của tài liệu XML, thay thế cho DTD
XML Schema mô tả: Các phần tử và thuộc tính trong tài liệu
XML Thứ tự và số lượng các phần tử con Các kiểu dữ liệu của phần tử và thuộc
tính
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 5
Lí do sử dụng XML Schema
XML Schema là một sự thay thế cho DTD Hỗ trợ nhiều loại dữ liệu Sử dụng cú pháp XML Bảo toàn sự giao tiếp dữ liệu Ràng buộc khóa và tham chiếu mạnh
hơn DTD Tích hợp với namespace
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 6
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>
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 7
Schema và namespace
<schema xmlns=http://www.w3.org/2001/XMLSchematargetNamespace=“http://xyz.edu/Admin”>
…..</schema>
http://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,…
targetNamespace: định nghĩa namespace của lược đồ được xác định trong tài liệu <schema>…</schema> trên
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 8
Tham chiếu đến một XML Schema
<?xml version="1.0"?><notexmlns=http://www.w3schools.comxmlns:xsi=http://www.w3.org/2001/XMLSchema-instancexsi: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 nghĩa là lược đồ của namespace http://www.w3schools.com được tìm thấy ở http://www.w3schools.com note.xsd
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 9
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>
<include>: giống như #include trong ngôn ngữ C schemaLocation: cho biết nơi để lấy thông tin
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 10
Khai báo phần tử trong XML Schema
Phần tử <schema> là phần tử gốc trong các tài liệu XML Schema
<?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:schema>
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 11
Khai báo phần tử trong XSD
xmlns:xs="http://www.w3.org/2001/XMLSchema" 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
targetNamespace=http://www.w3schools.com Các phần tử được định nghĩa trong lược đồ đến từ
namespace http://www.w3schools.com xmlns="http://www.w3schools.com" namespace mặc định là xmlns="http://www.w3schools.com" elementFormDefault="qualified“ 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
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 12
Khai báo phần tử trong XSD
Các kiểu phần tử: Kiểu giản đơn (Simple element): là một
phần tử XML chỉ có kiểu dữ liệu text, mà không có thuộc tính hoặc không thể chứa các phần tử khácKiểu text có thể có nhiều loại
Kiểu phức hợp (Complex element): là một phần tử XML có thuộc tính, hoặc chứa các phần tử con khác
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 13
Khai báo phần tử kiểu giản đơn
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 14
Kiểu giản đơn
Kiểu giản đơn (Simple element): Là một phần tử XML chỉ có kiểu dữ liệu text,
mà không có thuộc tính hoặc không thể chứa các phần tử khác
Kiểu text có thể có nhiều loại khác nhau: Built-in data type User-defined data type
Người dùng có thể thêm ràng buộc cho kiểu dữ liệu để giới hạn nội dung của nó, hoặc yêu cầu dữ liệu thỏa mãn một mẫu nào đó
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 15
Định nghĩa một phần tử giản đơn
<xs:element name="xxx" type="yyy"/> xxx: tên của phần tử yyy: kiểu dữ liệu của phần tử Các kiểu dữ liệu thông dụng:
xs:string xs:decimal xs:integer xs:boolean xs:date xs:time
<xs:element name="xxx" type="yyy"/> xxx: tên của phần tử yyy: kiểu dữ liệu của phần tử Các kiểu dữ liệu thông dụng:
xs:string xs:decimal xs:integer xs:boolean xs:date xs:time
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 16
Ví dụ
XML elements:
Simple element definition:
<lastname>Refsnes</lastname><age>36</age><dateborn>1970-03-27</dateborn>
<xs:element name="lastname" type="xs:string"/><xs:element name="age" type="xs:integer"/><xs:element name="dateborn" type="xs:date"/>
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 17
Giá trị mặc định và cố định
Giá trị mặc định (default value):<xs:element name="color" type="xs:string" default="red"/>
Giá trị cố định (fixed value):<xs:element name="color" type="xs:string" fixed="red"/>
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 18
Khai báo thuộc tính trong XSD
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 19
Thuộc tính trong XSD
Thuộc tính được khai báo như một kiểu giản đơn
Cá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
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 20
Khai báo thuộc tính
<xs:attribute name="xxx" type="yyy"/> xxx: tên của thuộc tính yyy: kiểu dữ liệu của thuộc tính. Các kiểu dữ
liệu thông dụng:xs:string xs:decimal xs:integer
xs:booleanxs:date
xs:time
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 21
Ví dụ
Phần tử XML có thuộc tính:<lastname lang="EN">Smith</lastname> Định nghĩa thuộc tính trong XSD:<xs:attribute name="lang" type="xs:string"/>
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 22
Khai báo loại thuộc tính
Khai báo default:<xs:attribute name="lang" type="xs:string" default="EN"/>
Khai báo fixed:<xs:attribute name="lang" type="xs:string" fixed="EN"/>
Khai báo required:<xs:attribute name="lang" type="xs:string" use="required"/>
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 23
Các loại ràng buộc
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 24
Ràng buộc nội dung
Khi một phần tử hay thuộc tính đã định nghĩa kiểu dữ liệu, phần tử hay thuộc tính đó đã có ràng buộc về nội dung
Người dùng có thể thêm các ràng buộc cho phần tử hay thuộc tính, gọi là (facet)
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 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"/> </xs:restriction></xs:simpleType></xs:element>
Kiểu miền con
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 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"/> <xs:enumeration value="BMW"/> </xs:restriction></xs:simpleType></xs:element>
Kiểu liệt kê
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 27
Ràng buộc giá trị
<xs:element name="letter"><xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-z]"/> </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>
Pattern constraint
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 28
Ràng buộc giá trị
<xs:element name="letter"><xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="([a-z])*"/> </xs:restriction></xs:simpleType></xs:element>
<xs:element name="letter"><xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="([a-z][A-Z])+"/> </xs:restriction></xs:simpleType></xs:element>
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 29
Ràng buộc giá trị
<xs:element name="gender"><xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="male|female"/> </xs:restriction></xs:simpleType></xs:element> <xs:element name="password">
<xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-zA-Z0-9]{8}"/> </xs:restriction></xs:simpleType></xs:element>
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 30
Ràng buộc về ký tự dấu cách
<xs:element name="address"><xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="preserve"/> </xs:restriction></xs:simpleType></xs:element>
<xs:whiteSpace value="preserve"/>: XML parser sẽ không loại bỏ bất kỳ ký tự dấu cách nào
<xs:whiteSpace value=“replace"/>: XML parser sẽ thay thế tất cả ký tự dấu cách (line feed, tab, space, carriage return) bằng khoảng trắng
<xs:whiteSpace value=“collapse"/>: XML parser sẽ loại bỏ tất cả ký tự dấu cách dư (line feed, tab, space, carriage return) bằng khoảng trắng
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 31
Ràng buộc về độ dài
<xs:element name="password"><xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="8"/> </xs:restriction></xs:simpleType></xs:element> <xs:element name="password">
<xs:simpleType> <xs:restriction base="xs:string"> <xs:minLength value="5"/> <xs:maxLength value="8"/> </xs:restriction></xs:simpleType></xs:element>
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 32
Bảng tóm lượcConstraint Description
enumeration Defines a list of acceptable values
fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero
length Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero
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
pattern Defines the exact sequence of characters that are acceptable
totalDigits Specifies the exact number of digits allowed. Must be greater than zero
whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 33
Khai báo phần tử kiểu phức hợp
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 34
Định nghĩa phần tử phức hợp
Phần tử phức hợp là một phần tử XML chứa các phần tử khác hoặc có thuộc tính
Có 4 loại phần tử phức hợp: Phần tử rỗng Phần tử chỉ chứa các phần tử khác Phần tử chỉ chứa văn bản Phần tử vừa chứa các phần tử khác, vừa chứa
văn bản
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 35
Ví dụ
<product pid="1345"/> <employee>
<firstname>John</firstname><lastname>Smith</lastname>
</employee> <food type="dessert">Ice cream</food> <description>
It happened on <date lang="norwegian">03.03.99</date> ....</description>
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 36
Định nghĩa một phần tử phức hợp trong XSD
<employee> <firstname>John</firstname> <lastname>Smith</lastname></employee>
Trong tài liệu XML:
Có nhiều cách để định nghĩa phần tử employee…
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 37
Định nghĩa một phần tử phức hợp trong XSD
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>
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 38
Định nghĩa một phần tử phức hợp trong XSD
Cách 2:
<xs:element name="employee" type="personinfo"/><xs:complexType name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence></xs:complexType>
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 39
Định nghĩa phần tử rỗng
<product prodid="1345" />
<xs:element name="product" type="prodtype"/><xs:complexType name="prodtype"> <xs:attribute name="prodid" type="xs:positiveInteger"/></xs:complexType>
<xs:element name="product"> <xs:complexType> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:complexType></xs:element>
Lê Thị Bích Hằng - Bộ môn Kỹ thuật phần mềm 40
Định nghĩa phần tử chỉ chứa phần tử con
<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>