Lí do sử dụng XML Schema 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... Khai báo
Trang 1Chuyên đề 1: TÌM HIỂU VỀ XML
Chương 4 Lược đồ XML (XML Schema)
Trường Đại học Nha Trang
Trang 2Nội dung
Trang 3XML Schema Definition
(XSD)
Trang 5Lí do sử dụng XML Schema
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
Trang 6<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 >
Trang 9Xây dựng lược đồ từ nhiều
</schema>
<include>: giống như #include trong ngôn ngữ C
schemaLocation: cho biết nơi để lấy thông tin
Trang 10Khai báo phần tử trong XML
Trang 11Khai báo phần tử trong XSD
Trang 12Khai báo phần tử trong XSD
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
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
Trang 13Khai báo phần tử kiểu giản
đơn
Trang 14Kiể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 đó
Trang 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:
Trang 16<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
Trang 17Giá trị mặc định và cố định
<xs:element name="color" type="xs:string" default="red" />
<xs:element name="color" type="xs:string" fixed="red" />
Trang 18Khai báo thuộc tính trong
XSD
Trang 20Khai 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:boolean xs:date
xs:time
Trang 21Ví dụ
<lastname lang="EN">Smith</lastname>
<xs:attribute name="lang" type="xs:string"/>
Trang 22Khai 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"/>
Trang 23Các loại ràng buộc
Trang 24Ràng buộc nội dung
nghĩa kiểu dữ liệu, phần tử hay thuộc
tính đó đã có ràng buộc về nội dung
cho phần tử hay thuộc tính, gọi là (facet)
Trang 27< /xs:simpleType >
Pattern constraint
Trang 28< /xs:simpleType >
Trang 29< /xs:simpleType >
Trang 30 <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
Trang 31<xs:maxLength value="8"/> < /xs:restriction >
Trang 32Bảng tóm lược
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
Trang 33Khai báo phần tử kiểu phức
hợp
Trang 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
Trang 37< /xs:complexType >
< /xs:element >
Trang 38< /xs:complexType >
Trang 40<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
< /xs:sequence >
< /xs:complexType >