SlideShare a Scribd company logo
Chuyên đề 1: TÌM HIỂU VỀ XML Chương 4 Lược đồ XML (XML Schema) Trường Đại học Nha Trang Khoa Công nghệ Thông tin
Nội dung XML Schema là gì? Khai báo phần tử trong XML Schema Tham chiếu đến một XML Schema
XML Schema Definition (XSD)
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í 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
Ví dụ <?xml version=&quot;1.0&quot;?> < xs:schema   xmlns:xs=“http://www.w3.org/2001/XMLSchema” targetNamespace=“http://www.w3schools.com” xmlns=“http://www.w3schools.com” elementFormDefault=&quot;qualified&quot;> < xs:element name=&quot;note&quot; > < xs:complexType > < xs:sequence > <xs:element name=&quot;to&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;from&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;heading&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;body&quot; type=&quot;xs:string&quot;/> < /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 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
Tham chiếu đến một XML Schema <?xml version=&quot;1.0&quot;?> <note xmlns=http://www.w3schools.com xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=&quot;http://www.w3schools.com note.xsd&quot;> <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
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
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=&quot;1.0&quot;?> < xs:schema  xmlns:xs= http://www.w3.org/2001/XMLSchema targetNamespace= http://www.w3schools.com xmlns= http://www.w3schools.com elementFormDefault=&quot;qualified&quot;> ...... </ xs:schema >
Khai báo phần tử trong XSD xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;   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 p hần tử được định nghĩa trong lược đồ đến từ namespace  http://www.w3schools.com xmlns=&quot;http://www.w3schools.com&quot;   namespace mặc định là xmlns=&quot;http://www.w3schools.com&quot;  elementFormDefault=&quot;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
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á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
Khai báo phần tử kiểu giản đơn
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 đó
Định nghĩa một phần tử giản đơn < xs:element   name =&quot;xxx&quot;   type =&quot;yyy&quot;/>   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
Ví dụ XML elements: Simple element definition: <lastname>Refsnes</lastname> <age>36</age> <dateborn>1970-03-27</dateborn>   < xs:element   name =&quot;lastname&quot;  type =&quot;xs:string&quot;/> < xs:element   name =&quot;age&quot;  type =&quot;xs:integer&quot;/> < xs:element   name =&quot;dateborn&quot;  type =&quot;xs:date&quot;/>
Giá trị mặc định và cố định Giá trị mặc định (default value): <xs:element name=&quot;color&quot; type=&quot;xs:string&quot;  default=&quot;red&quot; />   Giá trị cố định (fixed value): <xs:element name=&quot;color&quot; type=&quot;xs:string&quot;  fixed=&quot;red&quot; />
Khai báo thuộc tính trong XSD
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
Khai báo thuộc tính < xs:attribute   name =&quot;xxx&quot;  type =&quot;yyy&quot;/>   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:boolean xs:date xs:time   xs:string  xs:decimal  xs:integer
Ví dụ Phần tử XML có thuộc tính: <lastname  lang=&quot;EN&quot; >Smith</lastname>   Định nghĩa thuộc tính trong XSD: < xs:attribute   name =&quot;lang&quot;  type =&quot;xs:string&quot;/>
Khai báo loại thuộc tính Khai báo default: <xs:attribute name=&quot;lang&quot; type=&quot;xs:string&quot;  default =&quot;EN&quot;/>   Khai báo fixed: <xs:attribute name=&quot;lang&quot; type=&quot;xs:string&quot;  fixed =&quot;EN&quot;/>   Khai báo required: <xs:attribute name=&quot;lang&quot; type=&quot;xs:string&quot;  use =&quot;required&quot;/>
Các loại ràng buộc
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)
Ràng buộc giá trị < xs:element  name=&quot;age&quot;> < xs:simpleType > < xs:restriction  base=&quot;xs:integer&quot;> <xs:minInclusive value=&quot;0&quot;/> <xs:maxInclusive value=&quot;120&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element >  Kiểu miền con
Ràng buộc giá trị < xs:element   name=&quot;car&quot;> < xs:simpleType > < xs:restriction   base=&quot;xs:string&quot;> <xs:enumeration  value=&quot;Audi&quot;/> <xs:enumeration  value=&quot;Golf&quot;/> <xs:enumeration  value=&quot;BMW&quot;/> < /xs:restriction > < /xs:simpleType > </xs:element >  Kiểu liệt kê
Ràng buộc giá trị < xs:element  name=&quot;letter&quot;> < xs:simpleType > < xs:restriction  base=&quot;xs:string&quot;> <xs:pattern value=&quot;[a-z]&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element >  < xs:element  name=&quot;initials&quot;> < xs:simpleType > < xs:restriction  base=&quot;xs:string&quot;> <xs:pattern value=&quot;[a-zA-Z][a-zA-Z][a-zA-Z]&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element >  Pattern constraint
Ràng buộc giá trị < xs:element   name=&quot;letter&quot;> < xs:simpleType > < xs:restriction   base=&quot;xs:string&quot;> <xs:pattern  value=&quot;([a-z])*&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element >  < xs:element  name=&quot;letter&quot;> < xs:simpleType > < xs:restriction   base=&quot;xs:string&quot;> <xs:pattern  value=&quot;([a-z][A-Z])+&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element >
Ràng buộc giá trị < xs:element  name=&quot;gender&quot;> < xs:simpleType > < xs:restriction  base=&quot;xs:string&quot;> <xs:pattern value=&quot;male|female&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element >  < xs:element  name=&quot;password&quot;> < xs:simpleType > < xs:restriction  base=&quot;xs:string&quot;> <xs:pattern value=&quot;[a-zA-Z0-9]{8}&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element >
Ràng buộc về ký tự dấu cách <xs:whiteSpace value=&quot;preserve&quot;/>:  XML parser sẽ không loại bỏ bất kỳ ký tự dấu cách nào <xs:whiteSpace value=“replace&quot;/>:  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&quot;/>:  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 < xs:element  name=&quot;address&quot;> < xs:simpleType > < xs:restriction  base=&quot;xs:string&quot;> <xs:whiteSpace value=&quot;preserve&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element >
Ràng buộc về độ dài < xs:element  name=&quot;password&quot;> < xs:simpleType > < xs:restriction  base=&quot;xs:string&quot;> <xs:length value=&quot;8&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element >  < xs:element  name=&quot;password&quot;> < xs:simpleType > < xs:restriction  base=&quot;xs:string&quot;> <xs:minLength value=&quot;5&quot;/> <xs:maxLength value=&quot;8&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element >
Bảng tóm lược Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled  whiteSpace   Specifies the exact number of digits allowed. Must be greater than zero  totalDigits   Defines the exact sequence of characters that are acceptable  pattern   Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero   minLength   Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)   minInclusive   Specifies the lower bounds for numeric values (the value must be greater than this value)   minExclusive   Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero   maxLength   Specifies the upper bounds for numeric values (the value must be less than or equal to this value)   maxInclusive   Specifies the upper bounds for numeric values (the value must be less than this value)   maxExclusive   Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero   length   Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero   fractionDigits   Defines a list of acceptable values   enumeration   Description   Constraint
Khai báo phần tử kiểu phức hợp
Đị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
Ví dụ < product  pid=&quot;1345&quot; / >   < employee > <firstname>John</firstname> <lastname>Smith</lastname> < /employee >   < food  type=&quot;dessert&quot;>Ice cream< /food >   < description > It happened on <date lang=&quot;norwegian&quot;>03.03.99</date>   .... < /description >
Định nghĩa một phần tử phức hợp trong XSD Trong tài liệu XML: Có nhiều cách để định nghĩa phần tử employee… <employee> <firstname>John</firstname> <lastname>Smith</lastname> </employee>
Định nghĩa một phần tử phức hợp trong XSD Cách 1: < xs:element  name=&quot;employee&quot;> < xs:complexType > < xs:sequence > <xs:element name=&quot;firstname&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;lastname&quot; type=&quot;xs:string&quot;/> < /xs:sequence > < /xs:complexType > < /xs:element >
Định nghĩa một phần tử phức hợp trong XSD Cách 2: < xs:element  name=&quot;employee&quot; type=&quot;personinfo&quot;/> < xs:complexType  name=&quot;personinfo&quot;> < xs:sequence > <xs:element name=&quot;firstname&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;lastname&quot; type=&quot;xs:string&quot;/> < /xs:sequence > < /xs:complexType >
Định nghĩa phần tử rỗng <product prodid=&quot;1345&quot; />   < xs:element  name=&quot;product&quot; type=&quot;prodtype&quot;/> < xs:complexType  name=&quot;prodtype&quot;> < xs:attribute  name=&quot;prodid&quot; type=&quot;xs:positiveInteger&quot;/> < /xs:complexType >   < xs:element  name=&quot;product&quot;> < xs:complexType > < xs:attribute  name=&quot;prodid&quot; type=&quot;xs:positiveInteger&quot;/> < /xs:complexType > < /xs:element >
Định nghĩa phần tử chỉ chứa phần tử con  <person> <firstname>John</firstname> <lastname>Smith</lastname> </person>   < xs:element  name=&quot;person&quot;> < xs:complexType > < xs:sequence > <xs:element name=&quot;firstname&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;lastname&quot; type=&quot;xs:string&quot;/> < /xs:sequence > < /xs:complexType > < /xs:element >

More Related Content

What's hot (20)

PDF
Kiến trúc máy tính và hợp ngữ bài 07
Nhóc Nhóc
 
PDF
Bài 2 : Các đối tượng trong CSDL - SQL server
MasterCode.vn
 
PDF
Bài 3: Ngôn ngữ truy vân có cấu trúc (SQL) - Giáo trình FPT
MasterCode.vn
 
PDF
Báo cáo Quản lý Khách Sạn Làm Đồ án Access 01677525178
Đồ án access 01677525178
 
DOC
BÀI TẬP LỚN LÝ THUYẾT TẬP THÔ VÀ ỨNG DỤNG
Tai Tran
 
PDF
Luận văn: Kỹ thuật giấu tin CPT trên ảnh nhị phân, HAY
Dịch vụ viết bài trọn gói ZALO 0917193864
 
PDF
Bài tập lớn Phát triển phần mềm hướng dịch vụ PTIT
Popping Khiem - Funky Dance Crew PTIT
 
PPT
Bài số 5 Địa chỉ IP (IP Address) Sưu tầm
annguyenthanh7
 
PPT
C2 2
Trần Thanh
 
PPTX
Kiểm thử bảo mật web
Minh Tri Nguyen
 
DOC
LinQ to XML
Bình Trọng Án
 
PPT
Kĩ năng giao tiếp qua Mail
thuyhr
 
PDF
Báo cáo đồ án tốt nghiệp "Ứng dụng trí tuệ nhân tạo nhận dạng chữ viết tay xâ...
The Boss
 
PPT
C3 2
Trần Thanh
 
PPT
Chuong 1 - CSDL phân tán
duysu
 
PDF
Báo cáo ATTT CÁC DẠNG TẤN CÔNG SQL INJECTION
nataliej4
 
PDF
[14HCB-PTTKHTTT]- Quản Lý Siêu Thị Điện Thoại Phương Nam
Tran Van Cuong
 
PDF
Lựa chọn thuộc tính và Khai phá luật kết hợp trên WEKA
Ho Quang Thanh
 
DOCX
Báo cáo tkhttt
Thiện Cô
 
PDF
Giáo trình phân tích thiết kế hệ thống thông tin
Võ Phúc
 
Kiến trúc máy tính và hợp ngữ bài 07
Nhóc Nhóc
 
Bài 2 : Các đối tượng trong CSDL - SQL server
MasterCode.vn
 
Bài 3: Ngôn ngữ truy vân có cấu trúc (SQL) - Giáo trình FPT
MasterCode.vn
 
Báo cáo Quản lý Khách Sạn Làm Đồ án Access 01677525178
Đồ án access 01677525178
 
BÀI TẬP LỚN LÝ THUYẾT TẬP THÔ VÀ ỨNG DỤNG
Tai Tran
 
Luận văn: Kỹ thuật giấu tin CPT trên ảnh nhị phân, HAY
Dịch vụ viết bài trọn gói ZALO 0917193864
 
Bài tập lớn Phát triển phần mềm hướng dịch vụ PTIT
Popping Khiem - Funky Dance Crew PTIT
 
Bài số 5 Địa chỉ IP (IP Address) Sưu tầm
annguyenthanh7
 
Kiểm thử bảo mật web
Minh Tri Nguyen
 
LinQ to XML
Bình Trọng Án
 
Kĩ năng giao tiếp qua Mail
thuyhr
 
Báo cáo đồ án tốt nghiệp "Ứng dụng trí tuệ nhân tạo nhận dạng chữ viết tay xâ...
The Boss
 
Chuong 1 - CSDL phân tán
duysu
 
Báo cáo ATTT CÁC DẠNG TẤN CÔNG SQL INJECTION
nataliej4
 
[14HCB-PTTKHTTT]- Quản Lý Siêu Thị Điện Thoại Phương Nam
Tran Van Cuong
 
Lựa chọn thuộc tính và Khai phá luật kết hợp trên WEKA
Ho Quang Thanh
 
Báo cáo tkhttt
Thiện Cô
 
Giáo trình phân tích thiết kế hệ thống thông tin
Võ Phúc
 

Similar to Chapter 4 xml schema (6)

PPT
Thiết kế dữ liệu
Nguyen Tran
 
PDF
2_Giới thiệu và tạo file dữ liệu dưới dạngXML.pdf
PhamThiThuThuy1
 
PPT
Semantic Web Intro
Nguyễn Tuyến
 
PDF
Hướng dẫn lập trình quản lý c#
An Nguyen
 
PDF
Hướng dẫn tạo CSS cơ bản cho người mới bắt đầu
ThyPhanThBch
 
PDF
Javascript tong-hop a-z
Manhh Nguyễn
 
Thiết kế dữ liệu
Nguyen Tran
 
2_Giới thiệu và tạo file dữ liệu dưới dạngXML.pdf
PhamThiThuThuy1
 
Semantic Web Intro
Nguyễn Tuyến
 
Hướng dẫn lập trình quản lý c#
An Nguyen
 
Hướng dẫn tạo CSS cơ bản cho người mới bắt đầu
ThyPhanThBch
 
Javascript tong-hop a-z
Manhh Nguyễn
 
Ad

More from Bình Trọng Án (19)

PDF
A Developer's Guide to CQRS Using .NET Core and MediatR
Bình Trọng Án
 
PDF
Nếu con em vị nói lắp
Bình Trọng Án
 
PDF
Bài giảng chuyên đề - Lê Minh Hoàng
Bình Trọng Án
 
PDF
Tìm hiểu về NodeJs
Bình Trọng Án
 
PDF
Clean code-v2.2
Bình Trọng Án
 
PDF
Các câu chuyện toán học - Tập 3: Khẳng định trong phủ định
Bình Trọng Án
 
PDF
Luyện dịch Việt Anh
Bình Trọng Án
 
DOCX
2816 mcsa--part-11--domain-c111ntroller--join-domain-1
Bình Trọng Án
 
PDF
Chuyên đề group policy
Bình Trọng Án
 
DOCX
Tỷ lệ vàng - một phát hiện vĩ đại của hình học
Bình Trọng Án
 
PPT
Attributes & .NET components
Bình Trọng Án
 
DOCX
Ajax Control ToolKit
Bình Trọng Án
 
PPT
Linq intro
Bình Trọng Án
 
DOCX
Sách chữa tật nói lắp Version 1.0 beta
Bình Trọng Án
 
PPT
Mô hình 3 lớp
Bình Trọng Án
 
PPT
Xsd examples
Bình Trọng Án
 
PPT
Displaying XML Documents Using CSS and XSL
Bình Trọng Án
 
PPT
Introduction to XML
Bình Trọng Án
 
A Developer's Guide to CQRS Using .NET Core and MediatR
Bình Trọng Án
 
Nếu con em vị nói lắp
Bình Trọng Án
 
Bài giảng chuyên đề - Lê Minh Hoàng
Bình Trọng Án
 
Tìm hiểu về NodeJs
Bình Trọng Án
 
Clean code-v2.2
Bình Trọng Án
 
Các câu chuyện toán học - Tập 3: Khẳng định trong phủ định
Bình Trọng Án
 
Luyện dịch Việt Anh
Bình Trọng Án
 
2816 mcsa--part-11--domain-c111ntroller--join-domain-1
Bình Trọng Án
 
Chuyên đề group policy
Bình Trọng Án
 
Tỷ lệ vàng - một phát hiện vĩ đại của hình học
Bình Trọng Án
 
Attributes & .NET components
Bình Trọng Án
 
Ajax Control ToolKit
Bình Trọng Án
 
Linq intro
Bình Trọng Án
 
Sách chữa tật nói lắp Version 1.0 beta
Bình Trọng Án
 
Mô hình 3 lớp
Bình Trọng Án
 
Xsd examples
Bình Trọng Án
 
Displaying XML Documents Using CSS and XSL
Bình Trọng Án
 
Introduction to XML
Bình Trọng Án
 
Ad

Chapter 4 xml schema

  • 1. Chuyên đề 1: TÌM HIỂU VỀ XML Chương 4 Lược đồ XML (XML Schema) Trường Đại học Nha Trang Khoa Công nghệ Thông tin
  • 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
  • 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
  • 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
  • 6. Ví dụ <?xml version=&quot;1.0&quot;?> < xs:schema xmlns:xs=“http://www.w3.org/2001/XMLSchema” targetNamespace=“http://www.w3schools.com” xmlns=“http://www.w3schools.com” elementFormDefault=&quot;qualified&quot;> < xs:element name=&quot;note&quot; > < xs:complexType > < xs:sequence > <xs:element name=&quot;to&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;from&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;heading&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;body&quot; type=&quot;xs:string&quot;/> < /xs:sequence > < /xs:complexType > < /xs:element > < /xs:schema >
  • 7. Schema và namespace <schema xmlns=http://www.w3.org/2001/XMLSchema targetNamespace=“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
  • 8. Tham chiếu đến một XML Schema <?xml version=&quot;1.0&quot;?> <note xmlns=http://www.w3schools.com xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=&quot;http://www.w3schools.com note.xsd&quot;> <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
  • 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
  • 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=&quot;1.0&quot;?> < xs:schema xmlns:xs= http://www.w3.org/2001/XMLSchema targetNamespace= http://www.w3schools.com xmlns= http://www.w3schools.com elementFormDefault=&quot;qualified&quot;> ...... </ xs:schema >
  • 11. Khai báo phần tử trong XSD xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; 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 p hần tử được định nghĩa trong lược đồ đến từ namespace http://www.w3schools.com xmlns=&quot;http://www.w3schools.com&quot; namespace mặc định là xmlns=&quot;http://www.w3schools.com&quot; elementFormDefault=&quot;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
  • 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á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
  • 13. Khai báo phần tử kiểu giản đơn
  • 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 đó
  • 15. Định nghĩa một phần tử giản đơn < xs:element name =&quot;xxx&quot; type =&quot;yyy&quot;/> 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
  • 16. Ví dụ XML elements: Simple element definition: <lastname>Refsnes</lastname> <age>36</age> <dateborn>1970-03-27</dateborn> < xs:element name =&quot;lastname&quot; type =&quot;xs:string&quot;/> < xs:element name =&quot;age&quot; type =&quot;xs:integer&quot;/> < xs:element name =&quot;dateborn&quot; type =&quot;xs:date&quot;/>
  • 17. Giá trị mặc định và cố định Giá trị mặc định (default value): <xs:element name=&quot;color&quot; type=&quot;xs:string&quot; default=&quot;red&quot; /> Giá trị cố định (fixed value): <xs:element name=&quot;color&quot; type=&quot;xs:string&quot; fixed=&quot;red&quot; />
  • 18. Khai báo thuộc tính trong XSD
  • 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
  • 20. Khai báo thuộc tính < xs:attribute name =&quot;xxx&quot; type =&quot;yyy&quot;/> 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:boolean xs:date xs:time xs:string xs:decimal xs:integer
  • 21. Ví dụ Phần tử XML có thuộc tính: <lastname lang=&quot;EN&quot; >Smith</lastname> Định nghĩa thuộc tính trong XSD: < xs:attribute name =&quot;lang&quot; type =&quot;xs:string&quot;/>
  • 22. Khai báo loại thuộc tính Khai báo default: <xs:attribute name=&quot;lang&quot; type=&quot;xs:string&quot; default =&quot;EN&quot;/> Khai báo fixed: <xs:attribute name=&quot;lang&quot; type=&quot;xs:string&quot; fixed =&quot;EN&quot;/> Khai báo required: <xs:attribute name=&quot;lang&quot; type=&quot;xs:string&quot; use =&quot;required&quot;/>
  • 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)
  • 25. Ràng buộc giá trị < xs:element name=&quot;age&quot;> < xs:simpleType > < xs:restriction base=&quot;xs:integer&quot;> <xs:minInclusive value=&quot;0&quot;/> <xs:maxInclusive value=&quot;120&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element > Kiểu miền con
  • 26. Ràng buộc giá trị < xs:element name=&quot;car&quot;> < xs:simpleType > < xs:restriction base=&quot;xs:string&quot;> <xs:enumeration value=&quot;Audi&quot;/> <xs:enumeration value=&quot;Golf&quot;/> <xs:enumeration value=&quot;BMW&quot;/> < /xs:restriction > < /xs:simpleType > </xs:element > Kiểu liệt kê
  • 27. Ràng buộc giá trị < xs:element name=&quot;letter&quot;> < xs:simpleType > < xs:restriction base=&quot;xs:string&quot;> <xs:pattern value=&quot;[a-z]&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element > < xs:element name=&quot;initials&quot;> < xs:simpleType > < xs:restriction base=&quot;xs:string&quot;> <xs:pattern value=&quot;[a-zA-Z][a-zA-Z][a-zA-Z]&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element > Pattern constraint
  • 28. Ràng buộc giá trị < xs:element name=&quot;letter&quot;> < xs:simpleType > < xs:restriction base=&quot;xs:string&quot;> <xs:pattern value=&quot;([a-z])*&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element > < xs:element name=&quot;letter&quot;> < xs:simpleType > < xs:restriction base=&quot;xs:string&quot;> <xs:pattern value=&quot;([a-z][A-Z])+&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element >
  • 29. Ràng buộc giá trị < xs:element name=&quot;gender&quot;> < xs:simpleType > < xs:restriction base=&quot;xs:string&quot;> <xs:pattern value=&quot;male|female&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element > < xs:element name=&quot;password&quot;> < xs:simpleType > < xs:restriction base=&quot;xs:string&quot;> <xs:pattern value=&quot;[a-zA-Z0-9]{8}&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element >
  • 30. Ràng buộc về ký tự dấu cách <xs:whiteSpace value=&quot;preserve&quot;/>: XML parser sẽ không loại bỏ bất kỳ ký tự dấu cách nào <xs:whiteSpace value=“replace&quot;/>: 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&quot;/>: 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 < xs:element name=&quot;address&quot;> < xs:simpleType > < xs:restriction base=&quot;xs:string&quot;> <xs:whiteSpace value=&quot;preserve&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element >
  • 31. Ràng buộc về độ dài < xs:element name=&quot;password&quot;> < xs:simpleType > < xs:restriction base=&quot;xs:string&quot;> <xs:length value=&quot;8&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element > < xs:element name=&quot;password&quot;> < xs:simpleType > < xs:restriction base=&quot;xs:string&quot;> <xs:minLength value=&quot;5&quot;/> <xs:maxLength value=&quot;8&quot;/> < /xs:restriction > < /xs:simpleType > < /xs:element >
  • 32. Bảng tóm lược Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled whiteSpace Specifies the exact number of digits allowed. Must be greater than zero totalDigits Defines the exact sequence of characters that are acceptable pattern Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero minLength Specifies the lower bounds for numeric values (the value must be greater than or equal to this value) minInclusive Specifies the lower bounds for numeric values (the value must be greater than this value) minExclusive Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero maxLength Specifies the upper bounds for numeric values (the value must be less than or equal to this value) maxInclusive Specifies the upper bounds for numeric values (the value must be less than this value) maxExclusive Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero length Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero fractionDigits Defines a list of acceptable values enumeration Description Constraint
  • 33. Khai báo phần tử kiểu phức hợp
  • 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
  • 35. Ví dụ < product pid=&quot;1345&quot; / > < employee > <firstname>John</firstname> <lastname>Smith</lastname> < /employee > < food type=&quot;dessert&quot;>Ice cream< /food > < description > It happened on <date lang=&quot;norwegian&quot;>03.03.99</date> .... < /description >
  • 36. Định nghĩa một phần tử phức hợp trong XSD Trong tài liệu XML: Có nhiều cách để định nghĩa phần tử employee… <employee> <firstname>John</firstname> <lastname>Smith</lastname> </employee>
  • 37. Định nghĩa một phần tử phức hợp trong XSD Cách 1: < xs:element name=&quot;employee&quot;> < xs:complexType > < xs:sequence > <xs:element name=&quot;firstname&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;lastname&quot; type=&quot;xs:string&quot;/> < /xs:sequence > < /xs:complexType > < /xs:element >
  • 38. Định nghĩa một phần tử phức hợp trong XSD Cách 2: < xs:element name=&quot;employee&quot; type=&quot;personinfo&quot;/> < xs:complexType name=&quot;personinfo&quot;> < xs:sequence > <xs:element name=&quot;firstname&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;lastname&quot; type=&quot;xs:string&quot;/> < /xs:sequence > < /xs:complexType >
  • 39. Định nghĩa phần tử rỗng <product prodid=&quot;1345&quot; /> < xs:element name=&quot;product&quot; type=&quot;prodtype&quot;/> < xs:complexType name=&quot;prodtype&quot;> < xs:attribute name=&quot;prodid&quot; type=&quot;xs:positiveInteger&quot;/> < /xs:complexType > < xs:element name=&quot;product&quot;> < xs:complexType > < xs:attribute name=&quot;prodid&quot; type=&quot;xs:positiveInteger&quot;/> < /xs:complexType > < /xs:element >
  • 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=&quot;person&quot;> < xs:complexType > < xs:sequence > <xs:element name=&quot;firstname&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;lastname&quot; type=&quot;xs:string&quot;/> < /xs:sequence > < /xs:complexType > < /xs:element >