XML Schemas - 北京邮电大学软件学院

Download Report

Transcript XML Schemas - 北京邮电大学软件学院

SOA principles & practice
By Professor Guoshi Wu
Email: [email protected]
Work Phone: 010-5882-8088
2015/7/16
BUPTSSE
1
Lecture 3
XML Schemas
By Professor Guoshi Wu
Email: [email protected]
Work Phone: 010-5882-8088
2015/7/16
BUPTSSE
2
XML Schemas
http://www.w3.org/TR/xmlschema-0/ (Primer)
http://www.w3.org/TR/xmlschema-1/ (Structures)
http://www.w3.org/TR/xmlschema-2/ (Datatypes)
2015/7/16
BUPTSSE
3
Schema Validators
GUI Oriented
XML Spy
• http://www.xmlspy.com
Turbo XML
• http://www.extensibility.com
Command Line Only
XSV by Henry Thompson
• ftp://ftp.cogsci.ed.ac.uk/pub/XSV/XSV12.EXE
Has a Programmatic API
xerces by Apache
• http://www.apache.org/xerces-j/index.html
IBM Schema Quality Checker
• http://www.alphaworks.ibm.com/tech/xmlsqc
MSXML6.0
• http://www.microsoft.com
2015/7/16
BUPTSSE
4
What is XML Schemas?
Answer: An XML vocabulary for expressing your data's
business rules
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
2015/7/16
BUPTSSE
5
Example
<location>
<latitude>32.904237</latitude>
<longitude>73.620290</longitude>
<uncertainty units="meters">2</uncertainty>
</location>
Is this data valid?
To be valid, it must meet these constraints (data business rules):
1. The location must be comprised of a latitude, followed
by a longitude, followed by an indication of the uncertainty
of the lat/lon measurements.
2. The latitude must be a decimal with a value between -90 to +90
3. The longitude must be a decimal with a value between -180 to +180
4. For both latitude and longitude the number of digits to the right
of the decimal point must be exactly six digits.
5. The value of uncertainty must be a non-negative integer
6. The uncertainty units must be either meters or feet.
We
can express all these data constraints
using XML Schemas
2015/7/16
BUPTSSE
6
Validating your data
<location>
<latitude>32.904237</latitude>
<longitude>73.620290</longitude>
<uncertainty units="meters">2</uncertainty>
</location>
-check that the latitude is between -90 and
+90
-check that the longitude is between -180 and
+180
- check that the fraction digits is 6 for lat and
lon
...
XML Schema
validator
Data is ok!
XML Schema
2015/7/16
BUPTSSE
7
Purpose of XML Schemas (and
DTDs)
Specify:
the structure of instance documents
• "this element contains these elements, which contains
these other elements, etc"
the datatype of each element/attribute
• "this element shall hold an integer with the range 0 to
12,000" (DTDs don't do too well with specifying datatypes
like this)
2015/7/16
BUPTSSE
8
Motivation for XML Schemas
People are dissatisfied with DTDs
It's a different syntax
• You write your XML (instance) document using one
syntax and the DTD using another syntax --> bad,
inconsistent
Limited datatype capability
• DTDs support a very limited capability for specifying
datatypes. You can't, for example, express "I want the
<elevation> element to hold an integer with a range of 0
to 12,000"
• Desire a set of datatypes compatible with those found in
databases
– DTD supports 10 datatypes; XML Schemas supports
44+ datatypes
2015/7/16
BUPTSSE
9
Highlights of XML Schemas
XML Schemas are a tremendous advancement over DTDs:
Enhanced datatypes
• 44+ versus 10
• Can create your own datatypes
– Example: "This is a new type based on the string type and
elements of this type must follow this pattern: ddd-dddd,
where 'd' represents a digit".
Written in the same syntax as instance documents
• less syntax to remember
2015/7/16
BUPTSSE
10
Highlights of XML Schemas(Cont)
XML Schemas are a tremendous advancement over DTDs:
Object-oriented'ish
• Can extend or restrict a type (derive new type definitions on the
basis of old ones)
Can express sets, i.e., can define the child elements to occur in any
order
Can specify element content as being unique (keys on content) and
uniqueness within a region
Can define multiple elements with the same name but different
content
Can define elements with nil content
Can define substitutable elements - e.g., the "Book" element is
substitutable for the "Publication" element.
2015/7/16
BUPTSSE
11
Let's Get Started!
Convert the BookStore.dtd (next page) to the
XML Schema syntax
for this first example we will make a straight, oneto-one conversion, i.e., Title, Author, Date, ISBN,
and Publisher will hold strings, just like is done in
the DTD
We will gradually modify the XML Schema to use
stronger types
2015/7/16
BUPTSSE
12
BookStore.dtd
<!ELEMENT BookStore (Book+)>
<!ELEMENT Book (Title, Author, Date, ISBN,
Publisher)>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Author (#PCDATA)>
<!ELEMENT Date (#PCDATA)>
<!ELEMENT ISBN (#PCDATA)>
<!ELEMENT Publisher (#PCDATA)>
2015/7/16
BUPTSSE
13
ELEMENT
BookStore
ATTLIST
Author
#PCDATA
Book
ID
CDATA
NMTOKEN
ISBN
Title
Publisher
ENTITY
Date
This is the vocabulary that
DTDs provide to define your
new vocabulary
2015/7/16
BUPTSSE
14
http://www.w3.org/2001/XMLSchema
http://www.books.org (targetNamespace)
complexType
element
schema
string
BookStore
sequence
Book
boolean
Title
Author
Publisher
integer
Date
ISBN
This is the vocabulary that
XML Schemas provide to define your new vocabulary
One difference between XML Schemas and DTDs is that the XML Schema
vocabulary is associated with a name (namespace). Likewise, the new vocabulary
that you define must be associated with a name (namespace). With DTDs neither set
of vocabulary is associated with a name (namespace) [because DTDs pre-dated
namespaces].
2015/7/16
BUPTSSE
15
<?xml version="1.0"?>
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns="http://www.books.org"
elementFormDefault="qualified">
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Book" minOccurs="1"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
2015/7/16
BUPTSSE
<!ELEMENT
BookStore
(Book+)>
16
<xsd:element>
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Title" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="Author" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="Date" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
<!ELEMENT Book (Title, Author, Date, ISBN, Publisher)>
</xsd:complexType>
</xsd:element>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher"
type="xsd:string"/>
</xsd:schema>
2015/7/16
BUPTSSE
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Author (#PCDATA)>
<!ELEMENT Date (#PCDATA)>
<!ELEMENT ISBN (#PCDATA)>
<!ELEMENT Publisher (#PCDATA)>
17
Simple Type Definitions
XSD Elements
XSD Attributes
XSD Restrictions
2015/7/16
BUPTSSE
18
Simple Types: Elements
A simple element is an XML element that can contain
only text. It cannot contain any other elements or
attributes.
However, the "only text" restriction is quite misleading. The text can
be of many different types. It can be one of the types that are included
in the XML Schema definition (boolean, string, date, etc.), or it can be
a custom type that you can define yourself.
You can also add restrictions (facets) to a data type in order to limit
its content, and you can require the data to match a defined pattern.
<xs:element name="xxx" type="yyy"/>
<lastname>Refsnes</lastname>
<age>34</age>
<dateborn>1968-03-27</dateborn>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn"BUPTSSE
type="xs:date"/>
2015/7/16
19
Simple Types: Elements (Cont)
Common XML Schema Data Types
XML Schema has a lot of built-in data types. Here is a list of the most
common types:
xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
Declare Default and Fixed Values for Simple Elements
<xs:element name="color" type="xs:string" default="red"/>
<xs:element name="color" type="xs:string" fixed="red"/>
2015/7/16
BUPTSSE
20
Simple Types: Attributes
Notes:
All attributes are declared as simple types.
Only complex elements can have attributes!
What is Attribute:
Simple elements cannot have attributes. If an element has attributes,
it is considered to be of complex type. But the attribute itself is always
declared as a simple type. This means that an element with attributes always
has a complex type definition.
The syntax for defining an attribute is:
<xs:attribute name="xxx" type="yyy"/>
<lastname lang="EN">Smith</lastname>
<xs:attribute name="lang" type="xs:string"/>
2015/7/16
BUPTSSE
21
Simple Types: Attributes (Cont)
Common XML Schema Data Types
XML Schema has a lot of built-in data types. Here is a list of the most
common types:
xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
Declare Default and Fixed Values for Simple Elements
<xs:attribute name="lang" type="xs:string" default="EN"/>
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
2015/7/16
BUPTSSE
22
Simple Types: Attributes (Cont)
Creating Optional and Required Attributes
All attributes are optional by default. To explicitly specify that the
attribute is optional/required/prohibited , use the "use" attribute:
<xs:attribute name="lang" type="xs:string" use="optional"/>
<xs:attribute name="lang" type="xs:string" use="required"/>
<xs:attribute name="lang" type="xs:string" use="prohibited"/>
Restrictions on Content
When an XML element or attribute has a type defined,
it puts a restriction on the element's or attribute's content.
If an XML element is of type "xs:date" and contains a string like
"Hello Mother", the element will not validate.
With XML Schemas, you can also add your own restrictions to
your XML elements and attributes. These restrictions are called
facets.
2015/7/16
BUPTSSE
23
Simple Types: XSD Restrictions/Facets
Notes: Restrictions are used to control acceptable values for XML elements
or attributes. Restrictions on XML elements are called facets.
Restrictions on Values
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
2015/7/16
BUPTSSE
24
Simple Types: XSD Restrictions/Facets (Cont)
Restrictions on a Set of Values
<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>
The example above could also have been written like this
<xs:element name="car" type="carType"/>
<xs:simpleType name="carType">
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
2015/7/16
BUPTSSE
Note: In this case the type
"carType" can be used by
Other elements because
it is not a part of the "car"
element.
25
Simple Types: XSD Restrictions/Facets (Cont)
Restrictions on a Series of Values
<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-Z][A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
2015/7/16
BUPTSSE
To limit the content of
an XML element to define
a series of numbers
or letters that can be used,
we would use the
pattern constraint. Here
The only acceptable value
is ONE of the LOWERCASE
letters from a to z.
The "initials" element is a
simple type with a restriction.
The only acceptable value
is THREE of the UPPERCASE
letters from a to z.
26
Simple Types: XSD Restrictions/Facets (Cont)
Restrictions on a Series of Values
<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>
The "initials" element is
a simple type with a
restriction. The only
acceptable value is
THREE of the
LOWERCASE OR
UPPERCASE letters
from a to z.
This example defines an element called "choice":
<xs:element name="choice">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[xyz]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
2015/7/16
BUPTSSE
The "choice" element is a
simple type with a restriction.
The only acceptable value is
ONE of the following letters:
x, y, OR z.
27
Simple Types: XSD Restrictions/Facets (Cont)
Restrictions on a Series of Values
<xs:element name="prodid">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Other Restrictions on a Series of Values
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
2015/7/16
BUPTSSE
The example
defines an element
called "prodid“, the
"prodid" element is a
simple type with a
restriction.
The only acceptable
value is FIVE digits in
a sequence, and each
digit must be in a range
from 0 to 9.
The "letter" element is a
simple type with a restriction.
The acceptable value is zero
or more occurrences of
lowercase letters from a to z.
28
Simple Types: XSD Restrictions/Facets (Cont)
Other Restrictions on a Series of Values
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z][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>
2015/7/16
BUPTSSE
The "letter" element is a
simple type with a
restriction. The
acceptable value is one
or more occurrences
of a lowercase letter
followed by a uppercase
letter from a to z.
The acceptable value is 0
or 1 occurrence
of a lowercase letter
followed by a uppercase
letter from a to z.
29
Simple Types: XSD Restrictions/Facets (Cont)
Other Restrictions on a Series of Values
<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>
2015/7/16
BUPTSSE
The "gender" element
is a simple type with a
restriction. The only
acceptable value is male
OR female.
The "password" element is a
simple type with a restriction.
There must be exactly eight
characters in a row and those
characters must be lowercase
or uppercase letters from a
to z,or a number from 0 to 9.
30
Simple Types: XSD Restrictions/Facets (Cont)
Other Restrictions on a Series of Values
<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>
2015/7/16
BUPTSSE
The "gender" element
is a simple type with a
restriction. The only
acceptable value is male
OR female.
The "password" element is a
simple type with a restriction.
There must be exactly eight
characters in a row and those
characters must be lowercase
or uppercase letters from a
to z,or a number from 0 to 9.
31
Simple Types: XSD Restrictions/Facets (Cont)
Restrictions on White Space Characters
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
2015/7/16
BUPTSSE
The "address" element is
a simple type with a
restriction. The
whiteSpace constraint
is set to "preserve",
which means that the
XML processor WILL NOT
remove any white space
characters.
This "address" element is a
simple type with a restriction.
The whiteSpace constraint
is set to "replace", which
means that the XML processor
WILL REPLACE all white
space characters (line feeds,
tabs, spaces, and carriage
returns) with spaces.
32
Simple Types: XSD Restrictions/Facets (Cont)
Restrictions on White Space Characters
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restrictions on Length
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
2015/7/16
BUPTSSE
The whiteSpace constraint
is set to "collapse", which
means that the XML
processor WILL REMOVE
all white space characters
(line feeds, tabs, spaces,
carriage returns are
replaced with spaces,
leading and trailing spaces
are removed, multiple
spaces are reduced to
a single space).
The "password" element is a
simple type with a restriction.
The value must be exactly
eight characters.
33
Simple Types: XSD Restrictions/Facets (Cont)
Restrictions on Length
<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>
2015/7/16
BUPTSSE
This "password" element is
a simple type with a
restriction. The value must
be minimum five characters
and maximum eight
characters.
34
Simple Types: XSD Restrictions/Facets (Cont)
Restrictions for Datatypes
2015/7/16
BUPTSSE
35
Summary of Declaring simpleTypes
1. simpleType that uses a built-in base type:
<xsd:simpleType name= "EarthSurfaceElevation">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="-1290"/>
<xsd:maxInclusive value="29035"/>
</xsd:restriction>
</xsd:simpleType>
2. simpleType that uses another simpleType as the base type:
<xsd:simpleType name= "BostonSurfaceElevation">
<xsd:restriction base="EarthSurfaceElevation">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="120"/>
</xsd:restriction>
</xsd:simpleType>
2015/7/16
BUPTSSE
36
Complex Types
XSD Elements
XSD Empty
XSD Elements Only
XSD Text Only
XSD Mixed
XSD Indicators
XSD <any>
XSD <anyAttributes>
XSD Substitution
2015/7/16
BUPTSSE
37
XSD Complex Elements
What is a Complex Element?
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
<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>
....
2015/7/16
BUPTSSE
</description>
Note:
Each of
these
elements
May contain
attributes
as well!
Empty element
element contains
only other elements
A complex XML element that
contains only text:
A complex XML element that
contains both elements and text
38
XSD Complex Elements
How to Define a Complex Element
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
1. The "employee" element can be declared directly by naming the element
<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>
Here only the "employee" element can use the specified complex type.
Notice that the child elements, "firstname" and "lastname",
are surrounded by the <sequence> indicator. This means that the
child elements must appear in the same order as they are declared;
"firstname" first and "lastname" second.
2015/7/16
BUPTSSE
39
XSD Complex Elements (Cont)
How to Define a Complex Element
2. The "employee"
element can have a type
attribute
that refers to the name of
the complex type to use
<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>
If you use the method described above,
several elements can refer to the same complex type
<xs:element name="employee" type="personinfo"/>
<xs:element name="student" type="personinfo"/>
<xs:element name="member" 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>
2015/7/16
BUPTSSE
40
XSD Complex Elements (Cont)
<xs:element name="employee" type="fullpersoninfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
2015/7/16
BUPTSSE
You can also base a complex type element on an
existing complex type and add some elements, like
this:
How to Define a Complex Element
41
XSD Complex Empty Elements
An empty complex element can contain attributes; but it cannot
have any content between the opening and closing tags.
<xs:element name="product">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="xs:integer">
<xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:element>
we define a complexType having complexContent, i.e. only elements. The
complexContent element signals that we intend to restrict or extend the
content model of a complex type, and the restriction of integer declares one
attribute but does not introduce any element content.
2015/7/16
BUPTSSE
42
XSD Complex Empty Elements (Cont)
It is possible to declare the product element more compactly
<xs:element name="product">
<xs:complexType>
<xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>
You can give the complexType element a name also, and let the "product“
element have a type attribute that refers to the name of the complexType
(if you use this method, several elements can refer to the same complex type)
<xs:element name="product" type="prodtype"/>
<xs:complexType name="prodtype">
<xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:complexType>
2015/7/16
BUPTSSE
43
XSD Complex Type Elements Only
An "elements only" complex type contains an element that
contains only other elements.
<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>
Notice the <xs:sequence> tag.
It means that the elements defined ("firstname" and "lastname") must appear in
that order inside a "person" element.
2015/7/16
BUPTSSE
44
XSD Complex Type Elements Only (Cont)
Or you can give the complexType element a name, and let the "person"
element have a type attribute that refers to the name of the complexType
(if you use this method, several elements can refer to the same
complex type):
<xs:element name="person" type="persontype"/>
<xs:complexType name="persontype">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
2015/7/16
BUPTSSE
45
XSD Complex Text-Only Elements
A complex text element can contain both attributes and text.
<xs:element name="somename">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="basetype"> .... .... </xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
OR
<xs:element name="somename">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="basetype"> .... .... </xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>
This type contains only simple content (text and attributes), therefore we add a
simpleContent element around the content. When using simple content, you
must define an extension OR a restriction
within the simpleContent element.
2015/7/16
BUPTSSE
46
XSD Complex Text-Only Elements(Cont)
An example of an XML element, "shoesize", that contains text-only:
<shoesize country="france">35</shoesize>
The following example declares a complexType, "shoesize".
The content is defined as an integer data type and the "shoesize" element
also contains an attribute named "country“.
<xs:element name="shoesize">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="country" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
2015/7/16
BUPTSSE
47
XSD Complex Types With Mixed Content
A mixed complex type element can contain attributes, elements, and text.
Notice the text that
<letter> Dear Mr.<name>John Smith
appears between
</name>. Your order <orderid>1032</orderid>
the elements.
will be shipped on <shipdate>2001-07-13</shipdate>. </letter> "name", "orderid",
and "shipdate“ are
all children of "letter".
<xs:element name="letter">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="orderid" type="xs:positiveInteger"/>
<xs:element name="shipdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
</xs:element>
2015/7/16
BUPTSSE
Here the mixed
attribute must be
set to "true".
48
XSD Complex Types With Mixed Content(Cont)
We could also give the complexType element a name,
and let the "letter" element have a type attribute that refers to
the name of the complexType (if you use this method, several
elements can refer to the same complex type)
<xs:element name="letter" type="lettertype"/>
<xs:complexType name="lettertype" mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="orderid" type="xs:positiveInteger"/>
<xs:element name="shipdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
2015/7/16
BUPTSSE
49
complexContent versus simpleContent
With complexContent you extend or restrict a complexType
With simpleContent you extend or restrict a simpleType
<xsd:complexType
name="…">
<xsd:complexContent>
<extension base="X">
…
</extension>
</xsd:complexContent>
</xsd:complexType>
versus
Y must be a simpleType
X must be a complexType
2015/7/16
<xsd:complexType
name="…">
<xsd:simpleContent>
<extension base="Y">
…
</extension>
</xsd:simpleContent>
</xsd:complexType>
BUPTSSE
50
complexContent Vs simpleContent
• A complexType can be extended or restricted using
either simpleContent or complexContent.
• A complexType with simpleContent declares a
TextOnly content model,with or without attributes.
• A complexType with complexContent can be used to
declare the remaining three content Models (with or
without attributes)
•ElementOnly
•Mixed
• Empty
2015/7/16
BUPTSSE
51
XSD Complex Types Indicators
We can control HOW elements are to be used in documents with indicators.
Indicators
•Order indicators:
•All
•Choice
•Sequence
•Occurrence indicators:
•maxOccurs
•minOccurs
•Group indicators:
•Group name
•attributeGroup name
2015/7/16
BUPTSSE
52
XSD Complex Types Indicators
Order Indicators
Order indicators are used to define how elements should occur.
All Indicator
The <all> indicator specifies by default that the child elements can
appear in any order and that each child element must occur once
and only once.
<xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
Note: When using the <all> indicator you can set the <minOccurs>
indicator to 0 or 1 and the <maxOccurs> indicator can only be set to
1 (the <minOccurs> and <maxOccurs> are described later).
2015/7/16
BUPTSSE
53
XSD Complex Types Indicators (Cont)
Order Indicators
Order indicators are used to define how elements should occur.
Choice Indicator
The <choice> indicator specifies that either one child element or
another can occur:
<xs:element name="person">
<xs:complexType>
<xs:choice>
<xs:element name="employee" type="employee"/>
<xs:element name="member" type="member"/>
</xs:choice>
</xs:complexType>
</xs:element>
2015/7/16
BUPTSSE
54
XSD Complex Types Indicators (Cont)
Order Indicators
Order indicators are used to define how elements should occur.
Sequence Indicator
The <sequence> indicator specifies that the child elements must
appear in a specific order:
<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>
2015/7/16
BUPTSSE
55
XSD Complex Types Indicators (Cont)
Occurrence indicators:
Occurrence indicators are used to define how often an element can occur.
Note: For all "Order" and "Group" indicators(any, all, choice, sequence,
group name, and group reference) the default value for maxOccurs and
minOccurs is 1!!!!!
maxOccurs Indicator
The <maxOccurs> indicator specifies the maximum number of
times an element can occur:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string" maxOccurs="10"/>
</xs:sequence>
</xs:complexType>
</xs:element>
2015/7/16
BUPTSSE
56
XSD Complex Types Indicators (Cont)
Occurrence indicators:
Occurrence indicators are used to define how often an element can occur.
maxOccurs Indicator
Note: For all "Order" and "Group" indicators
(any, all, choice, sequence, group name,
and group reference) the default value for
maxOccurs and minOccurs is 1!!!!!
The <maxOccurs> indicator specifies the maximum number of
times an element can occur:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string" maxOccurs="10"/>
</xs:sequence>
"child_name" element can occur a
</xs:complexType>
minimum of one time (the default value
</xs:element>
for minOccurs is 1)
2015/7/16
BUPTSSE
57
XSD Complex Types Indicators (Cont)
Occurrence indicators:
Occurrence indicators are used to define how often an element can occur.
minOccurs Indicator
The <minOccurs> indicator specifies the minimum number of times
an element can occur:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string"
maxOccurs="10" minOccurs="0"/>
</xs:sequence>
The example above indicates that the
</xs:complexType>
"child_name" element can occur a
</xs:element>
minimum of zero times and a maximum
of ten times in a "person" element.
To allow an element to appear an
unlimited number of times, use the
maxOccurs="unbounded"
statement BUPTSSE
2015/7/16
58
Class Work
An XML file called "Myfamily.xml"
<?xml version="1.0" encoding="ISO-8859-1"?>
<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="family.xsd">
<person>
<full_name>Hege Refsnes</full_name>
<child_name>Cecilie</child_name>
</person>
<person>
<full_name>Tove Refsnes</full_name>
<child_name>Hege</child_name>
<child_name>Stale</child_name>
<child_name>Jim</child_name>
<child_name>Borge</child_name>
</person>
<person>
<full_name>Stale Refsnes</full_name>
</person>
</persons>
2015/7/16
BUPTSSE
59
answer
An XML file called "Myfamily.xsd"
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="persons">
<xs:complexType>
<xs:sequence>
<xs:element name="person" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string“
minOccurs="0" maxOccurs="5"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
2015/7/16
BUPTSSE
60
XSD Complex Types Indicators (Cont)
Group Indicators
Group indicators are used to define related sets of elements.
Element Groups
Element groups are defined with the group declaration, like this:
<xs:group name="groupname">
...
</xs:group>
You must define an all, choice, or sequence element inside the group
declaration. The following example defines a group named "persongroup",
that defines a group of elements that must occur in an exact sequence:
<xs:group name="persongroup">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="birthday" type="xs:date"/>
</xs:sequence>
</xs:group>
2015/7/16
BUPTSSE
61
XSD Complex Types Indicators (Cont)
Group Indicators
Group indicators are used to define related sets of elements.
Element Groups
After you have defined a group, you can reference it in another group
or complex type definition, like this:
<xs:group name="persongroup">
<xs:sequence> <xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="birthday" type="xs:date"/>
</xs:sequence>
</xs:group>
<xs:element name="person" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:group ref="persongroup"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
2015/7/16
BUPTSSE
62
XSD Complex Types Indicators (Cont)
Group Indicators
Group indicators are used to define related sets of elements.
Attribute Groups
Attribute groups are defined with the attributeGroup declaration, like this:
<xs:attributeGroup name="groupname">
...
</xs:attributeGroup>
The following example defines an attribute group named "personattrgroup":
<xs:attributeGroup name="personattrgroup">
<xs:attribute name="firstname" type="xs:string"/>
<xs:attribute name="lastname" type="xs:string"/>
<xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>
2015/7/16
BUPTSSE
63
XSD Complex Types Indicators (Cont)
Group Indicators
Group indicators are used to define related sets of elements.
Attribute Groups
After you have defined an attribute group, you can reference it in another
group or complex type definition, like this:
<xs:attributeGroup name="personattrgroup">
<xs:attribute name="firstname" type="xs:string"/>
<xs:attribute name="lastname" type="xs:string"/>
<xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>
<xs:element name="person">
<xs:complexType>
<xs:attributeGroup ref="personattrgroup"/>
</xs:complexType>
</xs:element>
2015/7/16
BUPTSSE
64
XSD The <any> Element
The <any> element enables us to extend the XML document with elements
not specified by the schema!
The <any> Element
The following example is a fragment from an XML schema called "family.xsd". It
shows a declaration for the "person" element. By using the <any> element we
can extend (after <lastname>) the content of "person" with any element:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:any minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
2015/7/16
BUPTSSE
65
XSD The <any> Element (Cont)
The <any> Element
Now we want to extend the "person" element with a "children" element.In this
case we can do so, even if the author of the schema above never declared any
"children" element!
Look at this schema file, called "children.xsd":
<?xml version="1.0" encoding="ISO-8859-1"?>
<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="children">
<xs:complexType>
<xs:sequence>
<xs:element name="childname" type="xs:string"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
2015/7/16
BUPTSSE
66
XSD The <any> Element (Cont)
The <any> Element
The XML file below (called "Myfamily.xml"), uses components from two
different schemas; "family.xsd" and "children.xsd":
<?xml version="1.0" encoding="ISO-8859-1"?>
<persons xmlns="http://www.microsoft.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:SchemaLocation="http://www.microsoft.com family.xsd
http://www.w3schools.com children.xsd">
<person>
<firstname>Hege</firstname>
The XML file above is valid
<lastname>Refsnes</lastname>
because the schema
<children>
"family.xsd" allows us to
<childname>Cecilie</childname>
extend the "person" element
</children>
with an optional element
</person>
after the "lastname"
<person>
element!
<firstname>Stale</firstname>
<lastname>Refsnes</lastname>
</person>
</persons>
2015/7/16
BUPTSSE
67
XSD The <anyAttribute> Element
The <anyAttribute> element enables us to extend the XML document
with attributes not specified by the schema!
The <anyAttribute> Element
The following example is a fragment from an XML schema called "family.xsd".
It shows a declaration for the "person" element. By using the <anyAttribute>
element we can add any number of attributes to the "person" element:
<xs:element name="prson">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>
2015/7/16
BUPTSSE
68
XSD The <anyAttribute> Element (Cont)
The <anyAttribute> Element
Now we want to extend the "person" element with a "gender" attribute.
In this case we can do so, even if the author of the schema above never
declared any "gender" attribute!
Look at this schema file, called "attribute.xsd":
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:attribute name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:schema>
2015/7/16
BUPTSSE
69
XSD The <anyAttribute> Element (Cont)
The <anyAttribute> Element
The XML file below (called "Myfamily.xml"), uses components from two
different schemas; "family.xsd" and "attribute.xsd":
<?xml version="1.0" encoding="ISO-8859-1"?>
<persons xmlns="http://www.microsoft.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:SchemaLocation="http://www.microsoft.com family.xsd
http://www.w3schools.com attribute.xsd">
<person gender="female">
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
</person>
The XML file above is valid
<person gender="male">
because the schema
<firstname>Stale</firstname>
"family.xsd" allows us to add
<lastname>Refsnes</lastname>
an attribute to the "person"
</person>
element!
</persons>
The <any> and <anyAttribute> elements are used to make EXTENSIBLE
documents! They allow documents to contain additional elements that are
BUPTSSE
not2015/7/16
declared in the main XML schema!
70
XSD Element Substitution
With XML Schemas one element can substitute another element.
Element Substitution
Let's say that we have users from two different countries: England and Norway
We would like the ability to let the user choose whether he or she would like
to use the Norwegian element names or the English element names in the
XML document.
To solve this problem, we could define a substitutionGroup in the XML
schema. First, we declare a head element and then we declare the other
elements which state that they are substitutable for the head element.
<xs:element name="name" type="xs:string"/>
<xs:element name="navn" substitutionGroup="name"/>
In the example above, the "name" element is the head element
and the "navn" element is substitutable for "name".
2015/7/16
BUPTSSE
71
XSD Element Substitution (Cont)
Element Substitution
Look at this fragment of an XML schema:
<xs:element name="name" type="xs:string"/>
<xs:element name="navn" substitutionGroup="name"/>
<xs:complexType name="custinfo">
<xs:sequence>
<xs:element ref="name"/>
</xs:sequence>
</xs:complexType>
<xs:element name="customer" type="custinfo"/>
<xs:element name="kunde" substitutionGroup="customer"/>
A valid XML document (according to the schema above) could look like this:
<customer>
<name>John Smith</name>
</customer>
OR
<kunde>
<navn>John Smith</navn>
</kunde>
2015/7/16
BUPTSSE
72
XSD Element Substitution (Cont)
To prevent other elements from substituting with a specified element,
use the block attribute
Blocking Element Substitution
<xs:element name="name" type="xs:string" block="substitution"/>
Look at this fragment of an XML schema:
<xs:element name="name" type="xs:string" block="substitution"/>
Look at this fragment of an XML schema:
<xs:element name="name" type="xs:string" block="substitution"/>
<xs:element name="navn" substitutionGroup="name"/>
<xs:complexType name="custinfo">
<xs:sequence>
<xs:element ref="name"/>
</xs:sequence>
</xs:complexType>
<xs:element name="customer" type="custinfo" block="substitution"/>
<xs:element name="kunde" substitutionGroup="customer"/>
2015/7/16
BUPTSSE
73
XSD Element Substitution (Cont)
Blocking Element Substitution
A valid XML document (according to the schema above) looks like this:
<customer>
<name>John Smith</name>
</customer>
BUT THIS IS NO LONGER VALID:
<kunde>
<navn>John Smith</navn>
</kunde>
Using substitutionGroup
The type of the substitutable elements must be the same as,
or derived from, the type of the head element. If the type of the
substitutable element is the same as the type of the head element you
will not have to specify the type of the substitutable element.
Note that all elements in the substitutionGroup (the head element and the
substitutable elements) must be declared as global elements, otherwise it
will not work!
2015/7/16
BUPTSSE
74
XSD Element Substitution (Cont)
Element Substitution with Derived Types
<xsd:element name="Publication" type="PublicationType"/>
<xsd:element name="Book" substitutionGroup="Publication" type="BookType"/>
<xsd:element name="Magazine" substitutionGroup="Publication" type="MagazineType"/>
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Publication" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
2015/7/16
BUPTSSE
75
XSD Element Substitution (Cont)
BookType and MagazineType Derive from PublicationType
PublicationType
BookType
MagazineType
In order for Book and Magazine to be in a substitutionGroup with
Publication, their type (BookType and MagazineType, respectively)
must be the same as, or derived from Publication's type (PublicationType)
2015/7/16
BUPTSSE
76
<xsd:complexType name="PublicationType">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string" minOccurs="0“ maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:gYear"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="BookType">
<xsd:complexContent>
<xsd:extension base="PublicationType" >
<xsd:sequence>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="MagazineType">
<xsd:complexContent>
<xsd:restriction base="PublicationType">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Date" type="xsd:gYear"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
2015/7/16
BUPTSSE
77
<?xml version="1.0"?>
<BookStore …>
<Book>
<Title>Illusions: The Adventures of a Reluctant Messiah</Title>
<Author>Richard Bach</Author>
<Date>1977</Date>
<ISBN>0-440-34319-4</ISBN>
<Publisher>Dell Publishing Co.</Publisher>
</Book>
<Magazine>
<Title>Natural Health</Title>
<Date>1999</Date>
</Magazine>
<Book>
<Title>The First and Last Freedom</Title>
<Author>J. Krishnamurti</Author>
<Date>1954</Date>
<ISBN>0-06-064831-7</ISBN>
<Publisher>Harper &amp; Row</Publisher>
</Book>
</BookStore>
<BookStore> can contain any element in the substitutionGroup with Publication!
2015/7/16
BUPTSSE
78
XSD Element Substitution (Cont)
Notes about using substitutionGroup
The elements that are declared to be in the substitution group (e.g., name
and navn) must be declared as global elements
1. Transitive:
if element A can substitute for element B, and element B can substitute
for element C, then element A can substitute for element C.
A --> B --> C then A --> C
2. Non-symmetric: if element A can substitute for element B, it is not the
case that element B can substitute for element A.
2015/7/16
BUPTSSE
79
Terminology: Global versus Local
Global element declarations, global type definitions:
These are element declarations/type definitions that are
immediate children of <schema>
Local element declarations, local type definitions:
These are element declarations/type definitions that are nested
within other elements/types.
2015/7/16
BUPTSSE
80
Global versus Local (Cont)
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns="http://www.books.org"
elementFormDefault="qualified">
<xsd:complexType name="Publication">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"
maxOccurs="unbounded"/>
<xsd:element name="Author" type="xsd:string"
maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:gYear"/>
</xsd:sequence>
</xsd:complexType>
……
Global Type
Declaration
Local Element
Declarations
2015/7/16
BUPTSSE
81
Global versus Local (Cont)
…..
<xsd:complexType name="BookPublication">
<xsd:complexContent>
<xsd:extension base="Publication" >
<xsd:sequence>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" type="BookPublication"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
2015/7/16
BUPTSSE
Global Type
Declaration
Global Element
Declaration
Local Element
Declarations
Local Type
Declarations
82
Global versus Local (Cont)
So what if an element or type is global or local. What practical
impact does it have?
Answer: only global elements/types can be referenced (i.e.,
reused). Thus, if an element/type is local then it is effectively
invisible to the rest of the schema (and to other schemas).
2015/7/16
BUPTSSE
83
Data Types
AnyType
Simple Type
Atomic
List
Complex Type
Union
Sequence
All
Choice
Buildin
2015/7/16
BUPTSSE
84
W3C XML Schema type hierarchy
2015/7/16
BUPTSSE
85
Strings and similar datatypes
Boolean
2015/7/16
BUPTSSE
86
XSD String Data Types
String data types are used for values that contains character strings.
String Data Type
The following is an example of a string declaration in a schema:
<xs:element name="customer" type="xs:string"/>
An element in your document might look like this:
<customer>John Smith</customer>
OR
<customer>
John Smith
</customer>
2015/7/16
BUPTSSE
87
XSD String Data Types (Cont)
NormalizedString Data Type
The normalizedString data type is derived from the String data type.
The normalizedString data type also contains characters, but the XML
processor will remove line feeds, carriage returns, and tab characters.
The following is an example of a normalizedString declaration in a schema:
<xs:element name="customer" type="xs:normalizedString"/>
An element in your document might look like this:
<customer>John Smith</customer>
OR
<customer> John Smith </customer>
Note: In the example above the XML processor will replace the
tabs with spaces.
2015/7/16
BUPTSSE
88
XSD String Data Types (Cont)
Token Data Type
The token data type is also derived from the String data type.
The token data type also contains characters, but the XML processor
will remove line feeds, carriage returns, tabs, leading and trailing spaces,
The following is an example of a token declaration in a schema:
and multiple spaces.
<xs:element name="customer" type="xs:token"/>
An element in your document might look like this:
<customer>John Smith</customer>
OR
<customer>
John Smith
</customer>
Note: In the example above the XML processor will remove the
tabs.
2015/7/16
BUPTSSE
89
XSD String Data Types (Cont)
String Data Types
Note that all of the data types below derive
from the String data type
(except for string itself)!
90
BUPTSSE
2015/7/16
XSD String Data Types (Cont)
Restrictions on String Data Types
Restrictions that can be used with String data types:
enumeration
length
maxLength
minLength
pattern (NMTOKENS, IDREFS, and ENTITIES cannot use this constraint)
whiteSpace
2015/7/16
BUPTSSE
91
String similar datatypes (Cont)
AnyURI Data Type
The anyURI data type is used to specify a URI.
<xs:attribute name="src" type="xs:anyURI"/>
An element in your document might look like this:
<pic src="http://www.w3schools.com/images/smiley.gif" />
Note: If a URI has spaces, replace them with %20.
2015/7/16
BUPTSSE
92
String similar datatypes (Cont)
Boolean Data Type
The boolean data type is used to specify a true or false value.
<xs:attribute name="disabled" type="xs:boolean"/>
An element in your document might look like this:
<prize disabled="true">999</prize>
Note: Legal values for boolean are true, false,
1 (which indicates true), and 0 (which indicates false).
2015/7/16
BUPTSSE
93
String similar datatypes (Cont)
HexBinary And base64Binary Data Type
XML 1.0 is unable to hold binary content, which must be string-encoded
before it can be included in a XML document. W3C XML Schema has defined
two primary datatypes to support two encodings that are commonly used
(BinHex and base64)
A UTF-8 XML header such as:
<?xml version="1.0" encoding="UTF-8"?>
that is encoded as xs:hexBinary would be:
3f3c6d78206c657673726f693d6e3122302e20226e656f6369646
76e223d54552d4622383e3f
that is encoded as xs: base64Binary would be:
PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4NCg==
2015/7/16
BUPTSSE
94
String similar datatypes (Cont)
HexBinary And base64Binary Data Type
The following is an example of a hexBinary in a schema:
<xsd:simpleType name= "addressType " >
<xsd:restriction base= "xsd:hexBinary " >
<xsd:minLength value= "1 "/ >
<xsd:maxLength value= "8 "/ >
</xsd:restriction >
</xsd:simpleType >
2015/7/16
BUPTSSE
95
String similar datatypes (Cont)
Annotation
The <annotation> element is used for documenting the schema, both for
humans and for programs.
Use <documentation> for providing a comment to humans
Use <appinfo> for providing a comment to programs
• The content is any well-formed XML
Note that annotations have no effect on schema validation
The following is an example of an annotationin a schema:
<xsd:annotation>
<xsd:documentation>
The following constraint is not expressible with XML Schema:
The value of element A should be greater than the value of element B.
So, we need to use a separate tool (e.g., Schematron) to check this constraint.
We will express this constraint in the appinfo section (below).
</xsd:documentation>
<xsd:appinfo>
<assert test="A &gt; B">A should be greater than B</assert>
</xsd:appinfo>
<xsd:/annotation>
2015/7/16
BUPTSSE
96
String similar datatypes (Cont)
Annotation
Where Can You Put Annotations?
You cannot put annotations at just any random
location in the schema.
Here are the rules for where an annotation
element can go:
• annotations may occur before and after any
global component
• annotations may occur only at the
beginning of non-global components
2015/7/16
BUPTSSE
97
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns="http://www.books.org"
elementFormDefault="qualified">
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
2015/7/16
BUPTSSE
Can put
annotations
only
at
these
locations
How to put
annotate to
Date
declaration
. See next
page ...
98
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns="http://www.books.org"
elementFormDefault="qualified">
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
…….
<xsd:element name="Date" type="xsd:string">
<xsd:annotation>
<xsd:documentation>This is how to annotate the Date
element!</xsd:documentation>
</xsd:annotation>
</xsd:element>
…….
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Inline the annotation within the Date element declaration.
2015/7/16
BUPTSSE
99
String similar datatypes (Cont)
Two Optional Attributes for the documentation Element
source: this attribute contains a URL to a file which contains
supplemental information
xml:lang: this attribute specifies the language that the
documentation was written in
See example:
<xsd:documentation
source="http://www.xfront.com/BookReview.txt" xml:lang="FR"/>
2015/7/16
BUPTSSE
100
String similar datatypes (Cont)
One Optional Attribute for the appinfo Element
source: this attribute contains a URL to a file which contains
supplemental information
See example:
<xsd:appinfo source="http://www.xfront.com/Assertions.xml"/>
2015/7/16
BUPTSSE
101
Numeric datatypes
2015/7/16
BUPTSSE
102
XSD Numeric Data Types
Decimal data types are used for numeric values.
Decimal Data Type
The following is an example of a decimal declaration in a schema:
<xs:element name="prize" type="xs:decimal"/>
An element in your document might look like this:
<prize>999.50</prize>
OR
<prize>+999.5450</prize>
OR
<prize>-999.5230</prize>
OR
<prize>0</prize>
OR
<prize>14</prize>
Note: The maximum number of decimal digits you can specify is 18!
2015/7/16
BUPTSSE
103
XSD Numeric Data Types (Cont)
Integer Data Type
The integer data type is used to specify a numeric value without a
fractional component.
The following is an example of an integer declaration in a schema:
<xs:element name="prize" type="xs:integer"/>
An element in your document might look like this:
<prize>999</prize>
OR
<prize>+999</prize>
OR
<prize>-999</prize>
OR
<prize>0</prize>
Note: The maximum number of decimal digits you can specify is 18!
2015/7/16
BUPTSSE
104
XSD Numeric Data Types (Cont)
105
BUPTSSE
2015/7/16
Note that all of the data types below derive
from the Decimal
data type (except for decimal itself)!
Numeric Data Types
XSD Numeric Data Types (Cont)
Numeric Data Types
Restrictions that can be used with Numeric data types:
•enumeration
•fractionDigits
•maxExclusive
•maxInclusive
•minExclusive
•minInclusive
•pattern
•totalDigits
•whiteSpace
2015/7/16
BUPTSSE
106
Date and time datatypes
2015/7/16
BUPTSSE
107
XSD Date and Time Data Types
Date and time data types are used for values that contain date and time.
Date Data Type
The date data type is used to specify a date.
The date is specified in the following form "YYYY-MM-DD" where:
•YYYY indicates the year
•MM indicates the month
•DD indicates the day
The following is an example of a date declaration in a schema:
<xs:element name="start" type="xs:date"/>
The following is an example of a date declaration in a schema:
<start>2002-09-24</start>
2015/7/16
BUPTSSE
108
XSD Date and Time Data Types (Cont)
Time Zones
To specify a time zone, you can either enter a date in UTC time
by adding a "Z" behind the date - like this:
<start>2002-09-24Z</start>
or you can specify an offset from the UTC time by adding
a positive or negative time behind the date - like this:
<start>2002-09-24-06:00</start>
Or
<start>2002-09-24+06:00</start>
2015/7/16
BUPTSSE
109
XSD Date and Time Data Types (Cont)
Time Data Type
The time data type is used to specify a time. The time is specified in
the following form "hh:mm:ss" where:
•hh indicates the hour
•mm indicates the minute
•ss indicates the second
Note: All components are required!
The following is an example of a time declaration in a schema:
<xs:element name="start" type="xs:time"/>
An element in your document might look like this:
<start>09:00:00</start>
OR
<start>09:30:10.5</start>
2015/7/16
BUPTSSE
110
XSD Date and Time Data Types (Cont)
Time Zones
To specify a time zone, you can either enter a time in UTC time
by adding a "Z" behind the time - like this:
<start>09:30:10Z</start>
or you can specify an offset from the UTC time by adding
a positive or negative time behind the time - like this:
<start>09:30:10-06:00</start>
Or
<start>09:30:10+06:00</start>
2015/7/16
BUPTSSE
111
XSD Date and Time Data Types (Cont)
DateTime Data Type
The dateTime data type is used to specify a date and a time.
The dateTime is specified in the following form "YYYY-MM-DDThh:mm:ss" where:
•YYYY indicates the year
•MM indicates the month
•DD indicates the day
•T indicates the start of the required time section
•hh indicates the hour
•mm indicates the minute
•ss indicates the second
Note: All components are required!
An example of a dateTime declaration in a schema:
<xs:element name="startdate" type="xs:dateTime"/>
An element in your document might look like this:
<startdate>2002-05-30T09:00:00</startdate>
OR
<startdate>2002-05-30T09:30:10.5</startdate>
2015/7/16
BUPTSSE
112
XSD Date and Time Data Types (Cont)
DateTime Zones Type
To specify a time zone, you can either enter a dateTime in UTC time
by adding a "Z" behind the time - like this:
<startdate>2002-05-30T09:30:10Z</startdate>
or you can specify an offset from the UTC time by adding
a positive or negative time behind the time - like this:
<startdate>2002-05-30T09:30:10-06:00</startdate>
Or
<startdate>2002-05-30T09:30:10+06:00</startdate>
2015/7/16
BUPTSSE
113
XSD Date and Time Data Types (Cont)
Duration Data Type
The duration data type is used to specify a time interval.
The time interval is specified in the following form "PnYnMnDTnHnMnS" where:
•P indicates the period (required)
•nY indicates the number of years
•nM indicates the number of months
•nD indicates the number of days
•T indicates the start of a time section (required if you are going
•
to specify hours, minutes, or seconds)
•nH indicates the number of hours
•nM indicates the number of minutes
•nS indicates the number of seconds
The following is an example of a duration declaration in a schema:
<xs:element name="period" type="xs:duration"/>
The following is an example of a duration declaration in a schema:
<period>P5Y</period>
Or
<period>P5Y2M10D</period>
Or
<period>P5Y2M10DT15H</period>
2015/7/16
BUPTSSE
114
XSD Date and Time Data Types (Cont)
Date and Time Data Types
Restrictions that can be used with Date data types:
•enumeration
•maxExclusive
•maxInclusive
•minExclusive
•minInclusive
•pattern
•whiteSpace
2015/7/16
BUPTSSE
115
XSD Date and Time Data Types (Cont)
A built-in datatype (i.e., schema validators know about this datatype)
This datatype is used to represent a specific day (year-month-day)
Elements declared to be of type date must follow this form: CCYY-MM-DD
range for CC is: 00-99
range for YY is: 00-99
range for MM is: 01-12
range for DD is:
• 01-28 if month is 2
• 01-29 if month is 2 and the gYear is a leap gYear
• 01-30 if month is 4, 6, 9, or 11
• 01-31 if month is 1, 3, 5, 7, 8, 10, or 12
Example: 1999-05-31 represents May 31, 1999
2015/7/16
BUPTSSE
116
XSD Date and Time Data Types (Cont)
The gYear Datatype
A built-in datatype (Gregorian calendar year)
Elements declared to be of type gYear must follow this form: CCYY
range for CC is: 00-99
range for YY is: 00-99
Example: 1999 indicates the gYear 1999
2015/7/16
BUPTSSE
117
Group Element
The group element enables you to group together element declarations.
Note: the group element is just for grouping together element declarations, no
attribute declarations allowed!
Group definitions must be global
2015/7/16
BUPTSSE
118
<xsd:element name="Book" >
<xsd:complexType>
<xsd:sequence>
<xsd:group ref="PublicationElements"/>
<xsd:element name="ISBN" type="string"/>
<xsd:element name="Reviewer" type="string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="CD" >
<xsd:complexType>
<xsd:sequence>
<xsd:group ref="PublicationElements"/>
<xsd:element name="RecordingStudio" type="string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:group name="PublicationElements">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:string"/>
</xsd:sequence>
</xsd:group>
An example showing the use of the <group> element
2015/7/16
BUPTSSE
119
Lists
There are times when you will want an element to contain a list of values,
e.g., "The contents of the Numbers element is a list of numbers".
Example: For a document containing a Lottery drawing we might have
<Numbers>12 49 37 99 20 67</Numbers>
How do we declare the element Numbers ...
(1) To contain a list of integers, and
(2) Each integer is restricted to be between 1 and 99, and
(3) The total number of integers in the list is exactly six.
2015/7/16
BUPTSSE
120
<?xml version="1.0"?>
<LotteryDrawings xmlns="http://www.lottery.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.lottery.org
Lottery.xsd">
<Drawing>
<Week>July 1</Week>
<Numbers>21 3 67 8 90 12</Numbers>
</Drawing>
<Drawing>
<Week>July 8</Week>
<Numbers>55 31 4 57 98 22</Numbers>
</Drawing>
<Drawing>
<Week>July 15</Week>
<Numbers>70 77 19 35 44 11</Numbers>
</Drawing>
</LotteryDrawings>
Lottery.xml
2015/7/16
BUPTSSE
121
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.lottery.org"
xmlns="http://www.lottery.org"
elementFormDefault="qualified">
<xsd:simpleType name="LotteryNumbers">
<xsd:list itemType="xsd:positiveInteger"/>
</xsd:simpleType>
<xsd:element name="LotteryDrawings">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Drawing" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Week" type="xsd:string"/>
<xsd:element name="Numbers" type="LotteryNumbers"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Lottery.xsd
</xsd:schema>
2015/7/16
BUPTSSE
122
Lists (Cont)
LotteryNumbers --> Need Stronger Datatyping
The list in the previous schema has two problems:
It allows <Numbers> to contain an arbitrarily long list
The numbers in the list may be any positiveInteger
We need to:
Restrict the list to length value="6"
Restrict the numbers to maxInclusive value="99"
2015/7/16
BUPTSSE
123
Lists (Cont)
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.lottery.org"
xmlns="http://www.lottery.org"
elementFormDefault="qualified">
<xsd:simpleType name="OneToNinetyNine">
<xsd:restriction base="xsd:positiveInteger">
<xsd:maxInclusive value="99"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="NumbersList">
<xsd:list itemType="OneToNinetyNine"/>
</xsd:simpleType>
<xsd:simpleType name="LotteryNumbers">
<xsd:restriction base="NumbersList">
<xsd:length value="6"/>
</xsd:restriction>
</xsd:simpleType>
…….
2015/7/16
BUPTSSE
Lottery.xsd
124
Lists (Cont)
…..
<xsd:element name="LotteryDrawings">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Drawing" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Week" type="xsd:string"/>
<xsd:element name="Numbers" type="LotteryNumbers"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Lottery.xsd
2015/7/16
BUPTSSE
125
Lists (Cont)
<xsd:simpleType name="OneToNinetyNine">
<xsd:restriction base="xsd:positiveInteger">
<xsd:maxInclusive value="99"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="NumbersList">
<xsd:list itemType="OneToNinetyNine"/>
</xsd:simpleType>
<xsd:simpleType name="LotteryNumbers">
<xsd:restriction base="NumbersList">
<xsd:length value="6"/>
</xsd:restriction>
</xsd:simpleType>
NumbersList is a list where the type of each item is OneToNinetyNine.
LotteryNumbers restricts NumbersList to a length of six (i.e., an element
declared to be of type LotteryNumbers must hold a list of numbers,
between 1 and 99, and the length of the list must be exactly six).
2015/7/16
BUPTSSE
126
Lists (Cont)
Notes about the list type
You cannot create a list of lists
i.e., you cannot create a list type from another list type.
You cannot create a list of complexTypes
i.e., lists only apply to simpleTypes
In the instance document, you must separate each item in a list with white
space (blank space, tab, or carriage return)
The only facets that you may use with a list type are:
length: use this to specify the length of the list
minLength: use this to specify the minimum length of the list
maxLength: use this to specify the maximum length of the list
enumeration: use this to specify the values that the list may have
pattern: use this to specify the values that the list may have
2015/7/16
BUPTSSE
127
Note about using Include and Import
The <include> and <import> elements must come before any
element declarations or type definitions.
2015/7/16
BUPTSSE
128
Write AN XML Schema
<?xml version="1.0" encoding="ISO-8859-1"?>
<shiporder orderid="889923“ xmlns:xsi=http://www.w3.org/2001/XMLSchemainstance xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
2015/7/16
BUPTSSE
129
</shiporder>
Create an XML Schema (1)
Step1: defines a schema
<?xml version="1.0" encoding="ISO-8859-1" ?><xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
......
</xs:schema>
In the schema above we use the standard namespace (xs), and the URI
associated with this namespace is the Schema language definition, which has
the standard value of http://www.w3.org/2001/XMLSchema. </xs:sche
2015/7/16
BUPTSSE
130
Create an XML Schema (2)
Step 2: define the "shiporder" element
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
... ...
</xs:sequence>
...
</xs:complexType>
</xs:element>
Step 3: define orderperson element
<xs:element name="orderperson" type="xs:string"/>
2015/7/16
BUPTSSE
131
Create an XML Schema (3)
Step 4:
Define the "shipto" element:
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
With schemas we can define the number of possible occurrences for
an element with the maxOccurs and minOccurs attributes.
maxOccurs specifies the maximum number of occurrences for an
element and minOccurs specifies the minimum number of occurrences
for an element.
The
default value for both maxOccurs
and minOccurs is 1!
2015/7/16
BUPTSSE
132
Create an XML Schema (4)
Step 5:
Define the "item" element:
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
2015/7/16
BUPTSSE
133
Create an XML Schema (5)
Step 6:
Define the "item" element:
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
2015/7/16
BUPTSSE
134
Create an XML Schema (6)
Step 5:
Declare the attribute of the "shiporder" element, Since
this is a required attribute we specify use="required".
Note: The attribute declarations must always come last:
<xs:attribute name="orderid" type="xs:string" use="required"/>
2015/7/16
BUPTSSE
135
Here is the complete listing of the schema file called
"shiporder.xsd"
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
(Go to next page)
2015/7/16
BUPTSSE
136
Here is the complete listing of the schema file called
"shiporder.xsd"
(Continued from last page)
<xs:element name="item" maxOccurs="unbounded"> <xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/> </xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
2015/7/16
BUPTSSE
137
Second way for "shiporder.xsd"
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- definition of simple elements -->
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
<xs:attribute name="orderid" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType> <xs:sequence>
<xs:element ref="name"/>
<xs:element ref="address"/>
<xs:element ref="city"/>
<xs:element ref="country"/>
</xs:sequence> </xs:complexType></xs:element> (Go
2015/7/16
BUPTSSE
to next page)
138
Second way for "shiporder.xsd"
(Continued from last page)
<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element ref="title"/>
<xs:element ref="note" minOccurs="0"/>
<xs:element ref="quantity"/>
<xs:element ref="price"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element ref="orderperson"/>
<xs:element ref="shipto"/>
<xs:element ref="item" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="orderid" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
2015/7/16
BUPTSSE
139
XMLSchema Namespace (Cont)
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns="http://www.books.org"
elementFormDefault="qualified">
All XML Schemas have
<xsd:element name="BookStore">
"schema" as the root
……
element.
</xsd:element>
<xsd:element name="Book">
<xsd:complexType>
……
</xsd:complexType>
</xsd:element>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:schema>
2015/7/16
BUPTSSE
140
XMLSchema Namespace (Cont)
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
The elements and
xmlns="http://www.books.org"
elementFormDefault="qualified">
datatypes that
<xsd:element name="BookStore">
are used to construct
……
schemas
</xsd:element>
- schema
<xsd:element name="Book">
- element
<xsd:complexType>
- complexType
……
- sequence
</xsd:complexType>
- string
</xsd:element>
come from the
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
http://…/XMLSchem
<xsd:element name="Date" type="xsd:string"/>
a
<xsd:element name="ISBN" type="xsd:string"/>
namespace
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:schema>
2015/7/16
BUPTSSE
141
XMLSchema Namespace (Cont)
http://www.w3.org/2001/XMLSchema
complexType
element
sequence
schema
boolean
string
integer
2015/7/16
BUPTSSE
142
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns="http://www.books.org"
Indicates that the
elementFormDefault="qualified">
elements defined
<xsd:element name="BookStore">
by this schema
……
</xsd:element>
- BookStore
<xsd:element name="Book">
- Book
<xsd:complexType>
- Title
……
- Author
</xsd:complexType>
- Date
</xsd:element>
- ISBN
<xsd:element name="Title" type="xsd:string"/>
- Publisher
<xsd:element name="Author" type="xsd:string"/>
are to go in the
<xsd:element name="Date" type="xsd:string"/>
http://…/books.org
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
namespace
</xsd:schema>
2015/7/16
BUPTSSE
143
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
The default namespace is
xmlns="http://www.books.org"
http://www.books.org
elementFormDefault="qualified">
which is the
<xsd:element name="BookStore">
targetNamespace!
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
This is referencing a Book element
<xsd:element name="Book">
declaration. The Book in what
<xsd:complexType>
namespace? Since there is no
…..
namespace qualifier it is referencing
</xsd:complexType>
the Book element in the default
</xsd:element>
namespace, which is the target
<xsd:element name="Title" type="xsd:string"/>
Namespace! Thus, this is a reference
<xsd:element name="Author" type="xsd:string"/> to the Book element declaration
<xsd:element name="Date" type="xsd:string"/>
in this schema.
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:schema>
2015/7/16
BUPTSSE
144
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns="http://www.books.org"
elementFormDefault="qualified">
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Book" minOccurs="1"
maxOccurs="unbounded"/>
This is a directive to any
</xsd:sequence>
instance documents which
</xsd:complexType>
conform to this schema:
</xsd:element>
Any elements used by the
<xsd:element name="Book">
instance document which
<xsd:complexType>
were declared in this
<xsd:sequence>
schema must be namespace
…..
qualified.
</xsd:sequence>
</xsd:complexType>
</xsd:element>
….
</xsd:schema>
2015/7/16
BUPTSSE
145
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns="http://www.books.org"
elementFormDefault="qualified">
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Book" minOccurs="1"
maxOccurs="unbounded"/>
The declaration of a
</xsd:sequence>
If we want to avoid
target namespace gives
</xsd:complexType>
confusion while
us the possibility of
</xsd:element>
writing a schema for
defining elements and
<xsd:element name="Book">
this instance
attributes that belong to
<xsd:complexType>
document, we can
the target namespace
<xsd:sequence>
define prefixes in the
(called "qualified") and
…..
schema for both our
elements and attributes
</xsd:sequence>
target namespace
that don't belong to any
</xsd:complexType>
and for the W3C XML
namespace (called
</xsd:element>
Schema namespace.
"unqualified").
….
</xsd:schema>
2015/7/16
BUPTSSE
146
Referencing a schema in an XML
instance document
<?xml version="1.0"?>
<BookStore xmlns ="http://www.books.org" 1
xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" 3
xsi:schemaLocation="http://www.books.org
BookStore.xsd">
2
<Book>
<Title>My Life and Times</Title>
<Author>Paul McCartney</Author>
<Date>July, 1998</Date>
<ISBN>94303-12021-43892</ISBN>
<Publisher>McMillin Publishing</Publisher>
</Book>
...
</BookStore>
2015/7/16
BUPTSSE
147
Referencing a schema in an XML
instance document
1. First, using a default namespace declaration, tell the schemavalidator that all of the elements used in this instance document
come from the http://www.books.org namespace.
2. Second, with schemaLocation tell the schema-validator that the
http://www.books.org namespace is defined by BookStore.xsd
(i.e., schemaLocation contains a pair of values).
3. Third, tell the schema-validator that the schemaLocation
attribute we are using is the one in the XMLSchema-instance
namespace.
2015/7/16
BUPTSSE
148
Note multiple levels of checking
BookStore.xml
BookStore.xsd
Validate that the xml document
conforms to the rules described
in BookStore.xsd
2015/7/16
BUPTSSE
XMLSchema.xsd
(schema-for-schemas)
Validate that BookStore.xsd is a valid
schema document, i.e., it conforms
to the rules described in the
schema-for-schemas
149
XMLSchema Namespace (Cont)
Qualify XMLSchema, Default targetNamespace
In the first example, we explicitly qualified all elements from the XML
Schema namespace. The targetNamespace was the default namespace.
http://www.w3.org/2001/XMLSchema
http://www.books.org (targetNamespace)
BookStore
complexType
element
Book
sequence
Title
schema
Publisher
boolean
Author
ISBN
string
Date
integer
2015/7/16
BUPTSSE
150
XMLSchema Namespace (Cont)
Default XMLSchema,Qualify targetNamespace
Alternatively (equivalently), we can design our schema so that
XMLSchema is the default namespace.
http://www.w3.org/2001/XMLSchema
http://www.books.org (targetNamespace)
complexType
BookStore
element
Author
sequence
Book
schema
Title
string
Publisher ISBN
Date
boolean
integer
2015/7/16
BUPTSSE
151
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns:bk="http://www.books.org"
elementFormDefault="qualified">
<element name="BookStore">
<complexType>
<sequence>
<element ref="bk:Book" maxOccurs="unbounded"/>
</sequence>
</complexType>
Note that
</element>
http://…/XMLSchema
<element name="Book">
is the default
<complexType>
namespace.
<sequence>
Consequently, there
<element ref="bk:Title"/>
are no namespace
<element ref="bk:Author"/>
qualifiers on
<element ref="bk:Date"/>
- schema
<element ref="bk:ISBN"/>
- element
<element ref="bk:Publisher"/>
- complexType
</sequence>
- sequence
</complexType>
- string
</element>
2015/7/16
BUPTSSE
(see example02)
……
152
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns:bk="http://www.books.org"
elementFormDefault="qualified">
<element name="BookStore">
<complexType>
<sequence>
<element ref="bk:Book" minOccurs="1" maxOccurs="unbounded"/>
</sequence>
</complexType>
Here we are referencing a
</element>
Book element. Where is
<element name="Book">
that Book element defined?
<complexType>
In what namespace?
<sequence>
The bk: prefix indicates
<element ref="bk:Title"/>
what namespace this
<element ref="bk:Author"/>
element is in. bk: has been
<element ref="bk:Date"/>
set to be the same as the
<element ref="bk:ISBN"/>
targetNamespace.
<element ref="bk:Publisher"/>
</sequence>
</complexType>
</element>
2015/7/16
BUPTSSE
153
……
XMLSchema Namespace (Cont)
"bk:" References the targetNamespace
http://www.w3.org/2001/XMLSchema
http://www.books.org (targetNamespace)
BookStore
complexType
Book
element
Author
sequence
Title
schema
string
ISBN
boolean
Date
Publisher
integer
bk
Consequently, bk:Book refers to the Book element in the targetNamespace.
2015/7/16
BUPTSSE
154
Classic use of XML Schemas
(Trading Partners - B2B)
Software
to Process
P.O.
P.O.
"P.O. is
okay"
P.O.
Consumer
Supplier
Schema
Validator
P.O.
Schema
2015/7/16
(Schema at third-party,
neutral web site)
BUPTSSE
155
XML Schema --> GUI
P.O.
Schema
2015/7/16
GUI
Builder
P.O.
HTML
BUPTSSE
Supplier
Web
Server
156
XML Schema --> API
P.O.
Schema
2015/7/16
API
Builder
BUPTSSE
P.O.
API
157
XML Schema --> Smart Editor
P.O.
Schema
2015/7/16
Smart Editor
(e.g., XML Spy)
BUPTSSE
Helps you build your
instance documents.
For example, it pops
up a menu showing
you what is valid next.
It knows this by looking
at the XML Schema!
158
Automatic
API generation
Validate
XML
documents
XML Schema
Smart Editor
Automatic
GUI generation
Semantic Web???
2015/7/16
BUPTSSE
159
Referencing a schema in an XML
instance document
1. First, using a default namespace declaration, tell the schemavalidator that all of the elements used in this instance document
come from the http://www.books.org namespace.
2. Second, with schemaLocation tell the schema-validator that the
http://www.books.org namespace is defined by BookStore.xsd
(i.e., schemaLocation contains a pair of values).
3. Third, tell the schema-validator that the schemaLocation
attribute we are using is the one in the XMLSchema-instance
namespace.
2015/7/16
BUPTSSE
160