SlideShare a Scribd company logo
Nguy n Minh Đ oễ ạ
XML Schema
An XML Schema describes the structure of an XML
document.
In this tutorial you will learn how to create XML
Schemas, why XML Schemas are more powerful than
DTDs, and how to use XML Schema in your
application.
2
XML Schema
3
Introduction to XML Schema
XML Schema is an XML-based alternative to DTD.
An XML schema describes the structure of an XML
document.
The XML Schema language is also referred to as XML
Schema Definition (XSD).
4
Introduction to XML Schema
What is an XML Schema?
The purpose of an XML Schema is to define the legal
building blocks of an XML document, just like a DTD.
An XML Schema:
 defines elements that can appear in a document
 defines attributes that can appear in a document
 defines which elements are child elements
 defines the order of child elements
 defines the number of child elements
 defines whether an element is empty or can include text
 defines data types for elements and attributes
 defines default and fixed values for elements and attributes
5
XML Schema
XML Schemas are the Successors of DTDs
We think that very soon XML Schemas will be used in
most Web applications as a replacement for DTDs.
Here are some reasons:
XML Schemas are extensible to future additions
XML Schemas are richer and more powerful than DTDs
XML Schemas are written in XML
XML Schemas support data types
XML Schemas support namespaces
6
Why Use XML Schemas?
XML Schemas Support Data Types
One of the greatest strength of XML Schemas is the
support for data types.
With support for data types:
 It is easier to describe allowable document content
 It is easier to validate the correctness of data
 It is easier to work with data from a database
 It is easier to define data facets (restrictions on data)
 It is easier to define data patterns (data formats)
 It is easier to convert data between different data types
7
XML Schemas use XML Syntax
Another great strength about XML Schemas is that
they are written in XML.
Some benefits of that XML Schemas are written in
XML:
You don't have to learn a new language
You can use your XML editor to edit your Schema files
You can use your XML parser to parse your Schema
files
You can manipulate your Schema with the XML DOM
You can transform your Schema with XSLT
8
XML Schemas Secure Data
Communication
When sending data from a sender to a receiver, it is
essential that both parts have the same "expectations"
about the content.
With XML Schemas, the sender can describe the data in a
way that the receiver will understand.
A date like: "03-11-2004" will, in some countries, be
interpreted as 3.November and in other countries as
11.March.
However, an XML element with a data type like this:
<date type="date">2004-03-11</date>
ensures a mutual understanding of the content, because
the XML data type "date" requires the format "YYYY-MM-
DD".
9
XSD How To?
10
XSD How To?
11
XSD How To?
12
XSD How To?
13
XSD How To?
14
XSD - The <schema> Element
15
The <schema> element is the root element of every
XML Schema.
The <schema> element may contain some attributes.
A schema declaration often looks something like this:
XSD - The <schema> Element
16
The following fragment:
xmlns:xs="http://www.w3.org/2001/XMLSchema"
indicates that the elements and data types used in the
schema come from the
"http://www.w3.org/2001/XMLSchema" namespace. It also
specifies that the elements and data types that come from
the "http://www.w3.org/2001/XMLSchema" namespace
should be prefixed with xs:
This fragment:
targetNamespace="http://www.w3schools.com" indicates
that the elements defined by this schema (note, to, from,
heading, body.) come from the "http://www.w3schools.com"
namespace.
XSD - The <schema> Element
17
This fragment:
xmlns="http://www.w3schools.com" indicates that
the default namespace is
"http://www.w3schools.com".
This fragment:
elementFormDefault="qualified" indicates that any
elements used by the XML instance document which
were declared in this schema must be namespace
qualified.
XSD - The <schema> Element
18
Referencing a Schema in an XML Document
This XML document has a reference to an XML
Schema:
XSD - The <schema> Element
19
The following fragment:
xmlns="http://www.w3schools.com" specifies the default
namespace declaration. This declaration tells the schema-
validator that all the elements used in this XML document are
declared in the "http://www.w3schools.com" namespace.
Once you have the XML Schema Instance namespace
available:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
you can use the schemaLocation attribute. This attribute has
two values. The first value is the namespace to use. The second
value is the location of the XML schema to use for that
namespace:
xsi:schemaLocation="http://www.w3schools.com note.xsd"
XSD Simple Elements
20
XML Schemas define the elements of your XML files.
A simple element is an XML element that contains only
text. It cannot contain any other elements or attributes.
Defining a Simple Element
The syntax for defining a simple element is:
<xs:element name="xxx" type="yyy"/> where xxx is the
name of the element and yyy is the data type of the
element.
XSD Simple Elements
21
XML Schema has a lot of built-in data types. The
most common types are:
xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
XSD Simple Elements
22
XSD Simple Elements
23
XSD Attributes
24
All attributes are declared as simple types.
Simple elements cannot have attributes. If an element
has attributes, it is considered to be of a complex
type. But the attribute itself is always declared as a
simple type.
How to Define an Attribute?
The syntax for defining an attribute is:
<xs:attribute name="xxx" type="yyy"/> where xxx is
the name of the attribute and yyy specifies the data
type of the attribute.
XSD Attributes
25
XML Schema has a lot of built-in data types. The most
common types are:
xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
Example
Here is an XML element with an attribute:
<lastname lang="EN">Smith</lastname> And here is the
corresponding attribute definition:
<xs:attribute name="lang" type="xs:string"/>
XSD Attributes
26
XSD Attributes
27
XSD Restrictions/Facets
28
Restrictions are used to define acceptable values for
XML elements or attributes. Restrictions on XML
elements are called facets.
Restrictions on Values
 The following example defines an element called "age" with a
restriction. The value of age cannot be lower than 0 or greater
than 120:
XSD Restrictions/Facets
29
XSD Restrictions/Facets
30
XSD Restrictions/Facets
31
XSD Restrictions/Facets
32
XSD Restrictions/Facets
33
XSD Restrictions/Facets
34
XSD Restrictions/Facets
35
XSD Restrictions/Facets
36
XSD Restrictions/Facets
37
XSD Restrictions/Facets
38
XSD Restrictions/Facets
39
XSD Restrictions/Facets
40
XSD Restrictions/Facets
41
XSD Restrictions/Facets
42
XSD Complex Elements
43
A complex element is an XML element that contains
other elements and/or attributes.
There are four kinds of complex elements:
empty elements
elements that contain only other elements
elements that contain only text
elements that contain both other elements and text
Note: Each of these elements may contain attributes
as well!
XSD Complex Elements
44
XSD Complex Elements
45
XSD Complex Elements
46
XSD Complex Elements
47
XSD Complex Elements
48
XSD Empty Elements
49
An empty complex element cannot have contents,
only attributes.
An empty XML element:
<product prodid="1345" /> The "product" element
above has no content at all.
XSD Empty Elements
50
To define a type with no content, we must define a
type that allows elements in its content, but we do
not actually declare any elements, like this:
XSD Empty Elements
51
XSD Elements Only
52
An "elements-only" complex type contains an element
that contains only other elements.
XSD Elements Only
53
XSD Text-Only Elements
54
A complex text-only element can contain text and attributes. This
type contains only simple content (text and attributes), therefore we
add a simpleContent element around the content.
Tip: Use the extension/restriction element to expand or to limit the
base simple type for the element.
XSD Text-Only Elements
55
XSD Text-Only Elements
56
XSD Mixed Content
57
A mixed complex type element can contain
attributes, elements, and text.
Complex Types with Mixed Content
An XML element, "letter", that contains both text and
other elements:
 <letter>
Dear Mr.<name> John Smith</name>.
Your order <orderid> 1032</orderid>
will be shipped on <shipdate> 2001-07-13</shipdate>.
</letter>
XSD Mixed Content
58
XSD Mixed Content
59
XSD Indicators
60
We can control HOW elements are to be used in
documents with indicators.
Indicators
There are seven indicators:
 Order indicators:
 All
 Choice
 Sequence
 Occurrence indicators:
 maxOccurs
 minOccurs
 Group indicators:
 Group name
 attributeGroup name

More Related Content

PPT
DITA, Semantics, Content Management, Dynamic Documents, and Linked Data – A M...
PPTX
SPARQL Cheat Sheet
PPTX
Power bi components
PDF
Bài 3: Ngôn ngữ truy vân có cấu trúc (SQL) - Giáo trình FPT
PDF
Snowflake_free_trial_LabGuide.pdf
PDF
[系列活動] 手把手教你R語言資料分析實務
DOCX
Introduction to xml schema
DITA, Semantics, Content Management, Dynamic Documents, and Linked Data – A M...
SPARQL Cheat Sheet
Power bi components
Bài 3: Ngôn ngữ truy vân có cấu trúc (SQL) - Giáo trình FPT
Snowflake_free_trial_LabGuide.pdf
[系列活動] 手把手教你R語言資料分析實務
Introduction to xml schema

Similar to Xsd examples (20)

PPT
02 xml schema
DOC
PPTX
distributed system concerned lab sessions
PPTX
PPTX
PPTX
XML Schema.pptx
PDF
XSD Incomplete Overview Draft
PDF
Xml schema
PPTX
Web Service Workshop - 3 days
PPTX
programming with xml for graduate students
PPT
unit_5_XML data integration database management
PPT
XML Schema
PPTX
https://www.slideshare.net/slideshow/cyber-security-introductionpptx-25514344...
PPT
DATA INTEGRATION (Gaining Access to Diverse Data).ppt
PPT
Xml and DTD's
PPTX
PPTX
advDBMS_XML.pptx
PPTX
XML Schema
PPTX
XSD Schema Presentation on basic of the schema 2
02 xml schema
distributed system concerned lab sessions
XML Schema.pptx
XSD Incomplete Overview Draft
Xml schema
Web Service Workshop - 3 days
programming with xml for graduate students
unit_5_XML data integration database management
XML Schema
https://www.slideshare.net/slideshow/cyber-security-introductionpptx-25514344...
DATA INTEGRATION (Gaining Access to Diverse Data).ppt
Xml and DTD's
advDBMS_XML.pptx
XML Schema
XSD Schema Presentation on basic of the schema 2
Ad

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

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

Recently uploaded (20)

PPTX
Spectroscopy.pptx food analysis technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Electronic commerce courselecture one. Pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Cloud computing and distributed systems.
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
cuic standard and advanced reporting.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation theory and applications.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Spectroscopy.pptx food analysis technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
MYSQL Presentation for SQL database connectivity
Electronic commerce courselecture one. Pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Cloud computing and distributed systems.
Encapsulation_ Review paper, used for researhc scholars
Digital-Transformation-Roadmap-for-Companies.pptx
sap open course for s4hana steps from ECC to s4
The Rise and Fall of 3GPP – Time for a Sabbatical?
NewMind AI Weekly Chronicles - August'25 Week I
Advanced methodologies resolving dimensionality complications for autism neur...
cuic standard and advanced reporting.pdf
Chapter 3 Spatial Domain Image Processing.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Spectral efficient network and resource selection model in 5G networks
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation theory and applications.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Build a system with the filesystem maintained by OSTree @ COSCUP 2025

Xsd examples

  • 1. Nguy n Minh Đ oễ ạ
  • 2. XML Schema An XML Schema describes the structure of an XML document. In this tutorial you will learn how to create XML Schemas, why XML Schemas are more powerful than DTDs, and how to use XML Schema in your application. 2
  • 4. Introduction to XML Schema XML Schema is an XML-based alternative to DTD. An XML schema describes the structure of an XML document. The XML Schema language is also referred to as XML Schema Definition (XSD). 4
  • 5. Introduction to XML Schema What is an XML Schema? The purpose of an XML Schema is to define the legal building blocks of an XML document, just like a DTD. An XML Schema:  defines elements that can appear in a document  defines attributes that can appear in a document  defines which elements are child elements  defines the order of child elements  defines the number of child elements  defines whether an element is empty or can include text  defines data types for elements and attributes  defines default and fixed values for elements and attributes 5
  • 6. XML Schema XML Schemas are the Successors of DTDs We think that very soon XML Schemas will be used in most Web applications as a replacement for DTDs. Here are some reasons: XML Schemas are extensible to future additions XML Schemas are richer and more powerful than DTDs XML Schemas are written in XML XML Schemas support data types XML Schemas support namespaces 6
  • 7. Why Use XML Schemas? XML Schemas Support Data Types One of the greatest strength of XML Schemas is the support for data types. With support for data types:  It is easier to describe allowable document content  It is easier to validate the correctness of data  It is easier to work with data from a database  It is easier to define data facets (restrictions on data)  It is easier to define data patterns (data formats)  It is easier to convert data between different data types 7
  • 8. XML Schemas use XML Syntax Another great strength about XML Schemas is that they are written in XML. Some benefits of that XML Schemas are written in XML: You don't have to learn a new language You can use your XML editor to edit your Schema files You can use your XML parser to parse your Schema files You can manipulate your Schema with the XML DOM You can transform your Schema with XSLT 8
  • 9. XML Schemas Secure Data Communication When sending data from a sender to a receiver, it is essential that both parts have the same "expectations" about the content. With XML Schemas, the sender can describe the data in a way that the receiver will understand. A date like: "03-11-2004" will, in some countries, be interpreted as 3.November and in other countries as 11.March. However, an XML element with a data type like this: <date type="date">2004-03-11</date> ensures a mutual understanding of the content, because the XML data type "date" requires the format "YYYY-MM- DD". 9
  • 15. XSD - The <schema> Element 15 The <schema> element is the root element of every XML Schema. The <schema> element may contain some attributes. A schema declaration often looks something like this:
  • 16. XSD - The <schema> Element 16 The following fragment: xmlns:xs="http://www.w3.org/2001/XMLSchema" indicates that the elements and data types used in the schema come from the "http://www.w3.org/2001/XMLSchema" namespace. It also specifies that the elements and data types that come from the "http://www.w3.org/2001/XMLSchema" namespace should be prefixed with xs: This fragment: targetNamespace="http://www.w3schools.com" indicates that the elements defined by this schema (note, to, from, heading, body.) come from the "http://www.w3schools.com" namespace.
  • 17. XSD - The <schema> Element 17 This fragment: xmlns="http://www.w3schools.com" indicates that the default namespace is "http://www.w3schools.com". This fragment: elementFormDefault="qualified" indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.
  • 18. XSD - The <schema> Element 18 Referencing a Schema in an XML Document This XML document has a reference to an XML Schema:
  • 19. XSD - The <schema> Element 19 The following fragment: xmlns="http://www.w3schools.com" specifies the default namespace declaration. This declaration tells the schema- validator that all the elements used in this XML document are declared in the "http://www.w3schools.com" namespace. Once you have the XML Schema Instance namespace available: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" you can use the schemaLocation attribute. This attribute has two values. The first value is the namespace to use. The second value is the location of the XML schema to use for that namespace: xsi:schemaLocation="http://www.w3schools.com note.xsd"
  • 20. XSD Simple Elements 20 XML Schemas define the elements of your XML files. A simple element is an XML element that contains only text. It cannot contain any other elements or attributes. Defining a Simple Element The syntax for defining a simple element is: <xs:element name="xxx" type="yyy"/> where xxx is the name of the element and yyy is the data type of the element.
  • 21. XSD Simple Elements 21 XML Schema has a lot of built-in data types. The most common types are: xs:string xs:decimal xs:integer xs:boolean xs:date xs:time
  • 24. XSD Attributes 24 All attributes are declared as simple types. Simple elements cannot have attributes. If an element has attributes, it is considered to be of a complex type. But the attribute itself is always declared as a simple type. How to Define an Attribute? The syntax for defining an attribute is: <xs:attribute name="xxx" type="yyy"/> where xxx is the name of the attribute and yyy specifies the data type of the attribute.
  • 25. XSD Attributes 25 XML Schema has a lot of built-in data types. The most common types are: xs:string xs:decimal xs:integer xs:boolean xs:date xs:time Example Here is an XML element with an attribute: <lastname lang="EN">Smith</lastname> And here is the corresponding attribute definition: <xs:attribute name="lang" type="xs:string"/>
  • 28. XSD Restrictions/Facets 28 Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets. Restrictions on Values  The following example defines an element called "age" with a restriction. The value of age cannot be lower than 0 or greater than 120:
  • 43. XSD Complex Elements 43 A complex element is an XML element that contains other elements and/or attributes. There are four kinds of complex elements: empty elements elements that contain only other elements elements that contain only text elements that contain both other elements and text Note: Each of these elements may contain attributes as well!
  • 49. XSD Empty Elements 49 An empty complex element cannot have contents, only attributes. An empty XML element: <product prodid="1345" /> The "product" element above has no content at all.
  • 50. XSD Empty Elements 50 To define a type with no content, we must define a type that allows elements in its content, but we do not actually declare any elements, like this:
  • 52. XSD Elements Only 52 An "elements-only" complex type contains an element that contains only other elements.
  • 54. XSD Text-Only Elements 54 A complex text-only element can contain text and attributes. This type contains only simple content (text and attributes), therefore we add a simpleContent element around the content. Tip: Use the extension/restriction element to expand or to limit the base simple type for the element.
  • 57. XSD Mixed Content 57 A mixed complex type element can contain attributes, elements, and text. Complex Types with Mixed Content An XML element, "letter", that contains both text and other elements:  <letter> Dear Mr.<name> John Smith</name>. Your order <orderid> 1032</orderid> will be shipped on <shipdate> 2001-07-13</shipdate>. </letter>
  • 60. XSD Indicators 60 We can control HOW elements are to be used in documents with indicators. Indicators There are seven indicators:  Order indicators:  All  Choice  Sequence  Occurrence indicators:  maxOccurs  minOccurs  Group indicators:  Group name  attributeGroup name