SlideShare a Scribd company logo
1 of 291
XML Schema
Mani Bushan D’souza
mail2mani@email.com
Topics Covered
What is Schema
How to use the XML Schema language in
applications
Structure
What is it?
An XML Schema defines the legal structure that an
XML document may take, much like a blueprint.
This is accomplished by:
defining elements and child elements
defining attributes
defining the order of child elements
defining element content
defining data types for elements and attributes
defining default and fixed values
Definition and Declaration
Definition
 Create new types (both simple and complex
types)
Declaration
 Enable elements and attributes with specific
names and types (both simple and complex) to
appear in document instances
Schema vs. DTD
Schemas are extensible.
Schemas have more depth and power.
Schemas are written in XML.
Schemas support data types.
Schemas support namespaces.
Document Type Definition
An Information Modeling Syntax
a
b
x
y
DTD
“a is the parent of b.
x and y are children of b.”
<a>
<b>
<x>…</x>
<y>…</y>
</b>
</a>
Enforces
Structure
x and y are always interpreted as
String, regardless of how they are used
Schema vs. DTD
XML Schema
Information Modeling Syntax in XML
a
b
x
y
XML
Schema
<a>
<b>
<x>…</x>
<y>…</y>
</b>
</a>
Enforces
Structure
<xsl:element name=“x” type=“boolean” />
<xsl:attribute name=“z” type=“integer” />
x and y now have
enforceable data types
Schema vs. DTD
Comparing Element Declarations
Differences in data typing and syntax
DTD <!ELEMENT Age (#PCDATA)>
XML Schema <element name=“Age” type=“integer”/>
XML <Age>2009</Age>
Why Support Data Types?
Easier to describe allowable document
content.
Easier to validate data.
Easier to work with data from a database.
Easier to define data restrictions.
Easier to define data formats.
Easier to convert data between types.
Schema Validation
Schemas can be used to validate a document in two ways:
Content Model Validation
Checks order and nesting of elements (similar to DTD validation)
DataType Validation
Checks the element content for valid type and range
example: month element is an integer between 1 and 12
<month>5</month> VALID!!
<month>15</month> INVALID!!
Comparison with DTDs
DTDs use their own unique syntax (SGML)
XML Schema uses XML syntax
DTDs are concise
XML Schema is verbose
Comparison with DTDs
XML Schema can be parsed and manipulated programmatically like
any other XML document
DTDs cannot
(at least not without a bit of work)
Note: Custom DTD parsers are available, but the ability to parse a DTD is not inherent in
most XML Parsers.
XML Schema enforce Data Typing
DTDs cannot
(DTDs see everything as Strings)
Comparison with DTDs
XML Schema allows open-ended data models
 Vocabulary extension and inheritance
DTDs support only a closed model
XML Schema supports attribute groups
 DTDs offer only limited attribute group support
Comparison with DTDs
Schemas are Extensible
 Reuse Schemas in other Schemas.
 Create proprietary data types from public types.
 Reference multiple Schemas in one document.
Comparison with DTDs
XML Schema supports namespace
integration
 Allows the association of individual nodes of a
document with type declarations in a schema
DTDs allow only one association (between
the document and the DTD)
Multiple levels of checking
BookCatalogue.xml BookCatalogue1.xsd xml-schema.dtd
Does the xml document
conform to the rules laid
out in the xml-schema?
Is the xml-schema a valid
xml document, i.e., does it
conform to the rules laid
out in the xml-schema DTD?
Schema Features
Rich Datatypes ( integers, time, date, boolean.. )
User-defined types
Extendable types
Open, closed or refinable content models
open: Additional elements may appear
refinable: Additional elements may appear if they are defined in the schema
Grouping
Namespace support
Schemas v. DTDs
Schemas DTDs
Support namespaces n/a
Written in XML syntax n/a
Full, object-oriented extensibility
Very limited
Extensive datatype support
Extended via
string substitutions
Open, closed or refinable
content models Closed only
End of DTDs?
DTDs have:
Widespread use and support
Many legacy applications, documents
Too much time & money invested
Experienced programmers/consultants
There is a DTD to define XML Schema
Benefits of XML and Schemas
You don’t need to learn a new language.
Editor of choice will work on Schemas.
Current parser will work on Schemas.
You can manipulate your Schema with the
XML DOM.
Schemas are transformable with XSLT.
Schema Workflow
XML Document
Normalized
XML Document
Schema
Error message
Schema
Processor
Valid
Invalid
XSD (XML Schema Definition) How To
Look at this simple XML document called "note.xml":
 <?xml version="1.0"?>
<note>
<to>Tom</to>
<from>Jerry</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
This is a simple DTD file called "note.dtd" that defines the
elements of the XML document above ("note.xml"):
 <!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
Simple XML schema
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.aimit.com"
xmlns="http://www.aimit.com"
elementFormDefault="qualified">
<xsd:element name="note">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="to" type="xsd:string"/>
<xsd:element name="from" type="xsd:string"/>
<xsd:element name="heading" type="xsd:string"/>
<xsd:element name="body" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
The <schema> element
The <schema> is the root element of every XML schema
<?xml version="1.0"?>
<xsd:schema>
...
...
</xsd:schema>
The <schema> element may contain some attributes. A
schema declaration often looks something like this:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.aimit.com"
xmlns="http://www.aimit.com"
elementFormDefault="qualified">
... ... </xsd:schema>
The schema element must specify the namespace for
schemas as its xmlns:xsd attribute
XML <schema> element explained
xmlns:xsd="http://www.w3.org/2001/XMLSchema”
 indicates that the elements and data types used in the schema (schema,
element, complexType, sequence, string, boolean, etc.) 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 xsd:
targetNamespace="http://www.aimit.com"
 indicates that the elements defined by this schema (note, to, from, heading,
body.) come from the "http://www.aimit.com" namespace.
xmlns="http://www.aimit.com"
 indicates that the default namespace is "http://www.aimit.com".
elementFormDefault="qualified"
 indicates that any elements used by the XML instance document which were
declared in this schema must be namespace qualified.
The XSD document
Since the XSD is written in XML, it can get confusing, what
we are talking about (XML or XSD?)
The file extension is .xsd
The root element is <schema>
The XSD starts like this:
 <?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.rg/2001/XMLSchema">
<?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>
<xsd:element name="Book">
<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>
</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>
BookStore.xsd
xsd = Xml-Schema Definition
<?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>
<xsd:element name="Book">
<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>
</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>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Author (#PCDATA)>
<!ELEMENT Date (#PCDATA)>
<!ELEMENT ISBN (#PCDATA)>
<!ELEMENT Publisher (#PCDATA)>
<!ELEMENT Book (Title, Author, Date,
ISBN, Publisher)>
<!ELEMENT BookStore (Book+)>
<?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>
<xsd:element name="Book">
<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>
</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>
All XML Schemas have
"schema" as the root
element.
<?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>
<xsd:element name="Book">
<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>
</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>
The elements and
datatypes that
are used to construct
schemas
- schema
- element
- complexType
- sequence
- string
come from the
http://…/XMLSchema
namespace
element
complexType
schema
sequence
http://www.w3.org/2001/XMLSchema
XMLSchema Namespace
string
integer
boolean
<?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>
<xsd:element name="Book">
<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>
</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>
Says that the
elements defined
by this schema
- BookStore
- Book
- Title
- Author
- Date
- ISBN
- Publisher
are to go in this
namespace
BookStore
Book
Title
Author
Date
ISBN
Publisher
http://www.books.org (targetNamespace)
Book Namespace (targetNamespace)
<?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>
<xsd:element name="Book">
<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>
</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>
This is referencing a
Book element declaration.
The Book is in which
namespace?
Since there
is no namespace qualifier
it is referencing the Book
element in the default
namespace, which is the
targetNamespace! Thus,
this is a reference to the
Book element declaration
in this schema.
The default namespace is
http://www.books.org
which is the
targetNamespace!
<?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>
<xsd:element name="Book">
<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>
</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>
This is a directive to any
instance documents which
conform to this schema:
Any elements that are
defined in this schema
must be
namespace-qualified
when used in instance
documents.
Other Attributes
Attribute Description
id Optional. Creates an unique ID for the element.
attributeFormDefault Optional. The form for attributes declared in target name space
of the schema. Must be either “qualified” or “unqualified”
elementFormDefault Optional. The form for elements declared in the target name
space of the schema. Must either be “qualified” or “unqualified”
blockDefault Optional. Specifies the default value of the block attribute on
element and complexType elements in namespace.
finalDefault Optional. Specifies the default value of the final attribute on
element, simpleType, and complexType elements in the
namespace.
targetNamespace Optional. An URI ref to the namespace of this schema.
version Optional. Specifies the version of the schema.
xmlns A URI ref specifying one or more namespaces for the schema.
any attributes Optional. Specifies other attributes with non-schema
namespace.
Referencing a schema in an XML instance
document
<?xml version="1.0"?>
<BookStore xmlns ="http://www.books.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.books.org
BookStore.xsd">
<Book>
<Title>My Life and Times</Title>
<Author>Paul McCartney</Author>
<Date>July, 1998</Date>
<ISBN>94303-12021-43892</ISBN>
<Publisher>McMillan Publishing</Publisher>
</Book>
...
</BookStore>
1. First, using a default namespace declaration, tell the schema-validator 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 XML Schema-instance namespace.
1
2
3
schemaLocation
type
noNamespaceSchemaLocation
http://www.w3.org/2001/XMLSchema-instance
XMLSchema-instance Namespace
nil
Referencing a schema in an XML
instance document
BookStore.xml BookStore.xsd
targetNamespace="http://www.books.org"
schemaLocation="http://www.books.org
BookStore.xsd"
- defines elements in
namespace http://www.books.org
- uses elements from
namespace http://www.books.org
A schema defines a new vocabulary. Instance documents use that new vocabulary.
A Reference to a DTD - Comparison
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "http://www.aimit.com/dtd/note.dtd">
<note>
<to>Tom</to>
<from>Jerry</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
A Reference to an XML Schema …
<?xml version="1.0"?>
<note xmlns="http://www.aimit.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.aimit.com note.xsd">
<to>Tom</to>
<from>Jerry</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
A Reference to an XML Schema
xmlns="http://www.aimit.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.aimit.com" namespace.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 XML Schema Instance namespace
 The XML Schema Instance reference is required
xsi:schemaLocation="http://www.aimit.com note.xsd"
 this schemaLocation 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
Cross-Referencing XML with Schemas
File “Person.xml” File “Person.xsd”
<?xml version="1.0"
encoding="UTF-8"?>
<Person
xmlns:xsi="http://www.w3.org/
2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="
Person.xsd">
<First>Katrina</First>
<Last>Khaif</Last>
<Age>24</Age>
</Person>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/
XMLSchema">
<xs:element name="Person">
<xs:complexType> <xs:sequence>
<xs:element name="First“
type="xs:string"/>
<xs:element name="Middle“
type="xs:string“
minOccurs="0"/>
<xs:element name="Last“
type="xs:string"/>
<xs:element name="Age“
type="xs:integer"/>
</xs:sequence> </xs:complexType>
</xs:element>
</xs:schema>
Qualify XMLSchema,
Default targetNamespace
In the first example, we explicitly qualified all elements from the
XML Schema namespace. The targetNamespace was the default
namespace.
BookStore
Book
Title
Author
Date
ISBN
Publisher
http://www.books.org (targetNamespace)
http://www.w3.org/2001/XMLSchema
element
complexType
schema
sequence
string
integer
boolean
Default XMLSchema,
Qualify targetNamespace
• Alternatively (equivalently), we can design our schema so that
XMLSchema is the default namespace.
BookStore
Book
Title
Author
Date
ISBN
Publisher
http://www.books.org (targetNamespace)
http://www.w3.org/2001/XMLSchema
element
complexType
schema
sequence
string
integer
boolean
<?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>
</element>
<element name="Book">
<complexType>
<sequence>
<element ref="bk:Title"/>
<element ref="bk:Author"/>
<element ref="bk:Date"/>
<element ref="bk:ISBN"/>
<element ref="bk:Publisher"/>
</sequence>
</complexType>
</element>
<element name="Title" type="string"/>
<element name="Author" type="string"/>
<element name="Date" type="string"/>
<element name="ISBN" type="string"/>
<element name="Publisher" type="string"/>
</schema>
Note that
http://…/XMLSchema
is the default
namespace.
Consequently, there
are no namespace
qualifiers on
- schema
- element
- complexType
- sequence
- string
<?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>
</element>
<element name="Book">
<complexType>
<sequence>
<element ref="bk:Title"/>
<element ref="bk:Author"/>
<element ref="bk:Date"/>
<element ref="bk:ISBN"/>
<element ref="bk:Publisher"/>
</sequence>
</complexType>
</element>
<element name="Title" type="string"/>
<element name="Author" type="string"/>
<element name="Date" type="string"/>
<element name="ISBN" type="string"/>
<element name="Publisher" type="string"/>
</schema>
Here we are
referencing a
Book element.
Where is that
Book element
defined? In
which namespace?
The bk: prefix
indicates what
namespace this
element is in. bk:
has been set to
be the same as the
targetNamespace.
"bk:" References the targetNamespace
BookStore
Book
Title
Author
Date
ISBN
Publisher
http://www.books.org (targetNamespace)
http://www.w3.org/2001/XMLSchema
bk
element
complexType
schema
sequence
string
integer
boolean
Consequently, bk:Book refers to the Book element in the targetNamespace.
XML Abstract Data Model
The XML Abstract Data Model
 composes of Schema Components.
 is used to describe XML Schemas.
Schema Component
 is the generic term for the building blocks that
compose the abstract data model of the schema.
13 Kinds of Schema Components
Simple type definitions
Complex type definitions
Attribute declarations
Element declarations
Attribute group definitions
Identity-constraint definitions
Model group definitions
Notation declarations
 Annotations
 Model groups
 Particles
 Wildcards
 Attribute Uses
Secondary
Primary
Helper
Relationships among Schema Components
Identity-
constraint
Definitions
Element
Declaration
Attribute
Declaration
Notation
Declaration
Simple Type
Definition
Complex Type
Definition
Particle
WildCard Model Group
AttributeUse
Attribute group
definition
Model group
definition
Information
Item
type
Conform
to
type
type
use
type
constrain
reference
reference
Primary Schema Components
XML Namespaces
Element Declarations
Attribute Declarations
Simple Type Definitions
Complex Type Definitions
XML Schema
(.xsd)
XML document & XML Schema
Information
Items
…
Elements
Attributes
XML Document
(.xml)
Declaration & Definition
Declaration Components
 are associated by (qualified) names to information
items being validated.
 It is like declaring objects in OOP.
Definition Components
 define internal schema components that can be used
in other schema components.
 Type definition is like defining classes in OOP.
Definitions vs. Declarations
A definition describes a
complex or simple type that
either contains element or
attribute declarations or
references element or attribute
declarations defined elsewhere
in the document.
Here’s a definition:
<xsd:complexType name=”bookType”>
<xsd:sequence>
<xsd:element name=”title”
type=”xsd:string”/>
<xsd:element name=”author”
type=”xsd:string”/>
<xsd:element name=”description”
type=”xsd:string”/>
</xsd:sequence>
</xsd:complexType>
A declaration defines an
element or attribute, specifying
the name and datatype for the
component.
Here’s a declaration:
<xsd:element name=”book”>
<xsd:complexType>
<xsd:sequence>
<xsd:element name=”title”
type=”xsd:string”/>
<xsd:element name=”author”
type=”xsd:string”/>
<xsd:element name=”description”
type=”xsd:string”/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Consider XML Instance Document Example
<book isbn="0-7356-1465-2">
<title> XML Step by Step</title>
<author> Michael young</author>
<publisher> Microsoft press</publisher>
</book>
Examples
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
… …
</xs:sequence>
<xs:attribute name="isbn" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:complexType name="bookType">
<xs:sequence>
<xs:element name="title" type="nameType"/>
… …
</xs:sequence>
<xs:attribute name="isbn" type="isbnType" use="required"/>
</xs:complexType>
Declaration
Type Definition <xs:element name="book" type="bookType"/>
<book isbn="0-7356-1465-2">
<title>
XML Step by Step
</title>
……
</book>
Declaration vs. Definition
Declarations
 Enable elements and attributes with specific names and
types (both simple and complex) to appear in document
instances.
<xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
<xsd:element name="comment" type="xsd:string"/>
Definitions
 create new types (both simple and complex)
<xsd:complexType name="PurchaseOrderType">
<xsd:sequence>
<xsd:element name="shipTo" type="Address"/>
<xsd:element name="billTo" type="Address"/>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="items" type="Items"/>
</xsd:sequence>
<xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>
Type Definitions
Why Type Definitions?
Simple Type Definition VS. Complex Type Definition
Attributes & Elements without
element children
Complex Type
Definition
Simple Type
Definition
Elements
“Simple” and “Complex” elements
A “simple” element is one that contains text and
nothing else
 A simple element cannot contain other elements
 A simple element cannot be empty
 However, the text can be of many different types, and
may have various restrictions applied to it
If an element isn’t simple, it’s “complex”
 A complex element may have attributes
 A complex element may be empty, or it may contain text,
other elements, or both text and other elements
Defining a simple element
A simple element is defined as
<xs:element name="name" type="type" />
where:
 name is the name of the element
 the most common values for type are
xs:boolean xs:integer
xs:date xs:string
xs:decimal xs:time
Other attributes a simple element may have:
 default="default value" if no other value is specified
 fixed="value" no other value may be specified
Examples of Simple element
using Built-in Simple Types
<xsd:element name=“weight” type=“xsd:string”/>
<xsd:element name=“population” type=“xsd:integer” />
Simple Elements
Example:
 <lastname>Amir Khan</lastname>
 <age>35</age>
 <dateborn>1975-09-15</dateborn>
 Corresponding simple element definition:
 <xs:element name="lastname" type="xs:string"/>
 <xs:element name="age" type="xs:integer"/>
 <xs:element name="dateborn" type="xs:date"/>
Declare Default and Fixed Values for Simple Elements
Simple elements can have a default value OR a fixed value
set.
A default value is automatically assigned to the element
when no other value is specified. In the following example
the default value is "red":
 <xs:element name="color" type="xs:string" default="red"/>
A fixed value is also automatically assigned to the element.
You cannot specify another value. In the following example
the fixed value is "red":
 <xs:element name="color" type="xs:string" fixed="red"/>
Primitive Type
XML Schema has a complex system of types.
Different types may describe:
1. the allowed values of attributes,
2. the allowed content of elements, or
3. the allowed content and the allowed attributes of
elements.
There is a subset of types, called the simple
types, that can be used in either of the first
two roles.
Simple Types
Built-in simple type Description Example(s)
xsd:string A sequence of any of
the legal XML characters
This is a string.
xsd:boolean
The value true or false, or 1 or
0 (indicating true or false,
respectively)
true
false
1
0
xsd:decimal A number that may contain a
decimal component
-5.2
-3.0
1
2.5
xsd:integer A whole number -389
-7
0
5
229
Simple Types
Built-in simple type Description Example(s)
xsd:positivelnteger A positive whole number(not
including 0)
5
229
xsd:negativelnteger
A negative whole number (not
including 0)
-389
-7
xsd:date
A calendar date, represented as
CCYY-MM-DD
1948-05-21
2001-10-15
Simple Types
Built-in
simple type
Description Example(s)
xsd:time A time of day, represented
as hh:mm:ss.ss
11:30:00.00(11:30 A.M.)
14:29:03 (2:29 P.M. and 3
seconds)
05:16:00.0(5:16 A.M.)
xsd:dateTime A date and time of day,
represented as
CCYY-MM-DDThh:mm:ss.ss
1948-05-21T17:28:00.00
xsd:gMonth A Gregorian calendar
month, represented as
-MM-
-05- (May)
-12- (December)
xsd:gYear A Gregorian calendar year,
represented as CCYY
1948
2001
Simple Types
Built-in simple type Description Example(s)
xsd:gDay A day of a Gregorian
calendar month,
represented as -DD
-05
-31
xsd:gYearMonth A Gregorian calendar
year and month,
represented as
CCYY-MM
1948-05 (May, 1948)
xsd:anyURI A URI (Uniform Resource
Identifier).
http://www.myOnline.com
Examples of Built-in Simple Types
<element name=“Title” type=“string”/>
<element name=“Heading” type=“string”/>
<element name=“Topic” type=“string”/>
<element name=“Price” type=“decimal”/>
XSD Complex Elements
A complex element contains other elements
and/or attributes.
● empty elements
● elements that contain only other elements
● elements that contain only text
● elements that contain both other elements and text
<employee>
<firstname>Mani</firstname>
<lastname>Dsouza</lastname>
</employee>
Example of complex elements
A complex XML element, "product", which is empty:
<product pid="1345"/>
A complex XML element, "employee", which contains only other elements:
<employee>
<firstname>Salman</firstname>
<lastname>Khan</lastname>
</employee>
A complex XML element, "food", which contains only text:
<food type=“Vanilla">Ice cream</food>
A complex XML element, "description", which contains both elements and
text:
<description>
It happened on <date lang="norwegian">03.03.99</date> ....
</description
How to Define a Complex Element
Complex XML element:
<employee>
<firstname>Mani</firstname>
<lastname>Dsouza</lastname>
</employee>
XSD:
<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>
Complex Element Example 1
<xsd:element name="Address" >
<xsd:complexType >
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="street" type="xsd:string" />
<xsd:element name="city" type="xsd:string" />
<xsd:element name="state" type="xsd:string" />
<xsd:element name="zip" type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN"
use="fixed" value="IN"/>
</xsd:complexType>
<xsd:element>
Elements can be declared in two ways
<xsd:element name="name" type="type" minOccurs="int" maxOccurs="int"/>
A simple type
(e.g., xsd:string)
or the name of
a complexType
(e.g., BookPublication)
<xsd:element name="name" minOccurs="int" maxOccurs="int">
<xsd:complexType>
…
</xsd:complexType>
</xsd:element>
1
2
A nonnegative
integer
A nonnegative
integer or "unbounded"
Note: minOccurs and maxOccurs can only
be used in nested (local) element declarations.
Note that:
<xsd:element name="A" type="xyz"/>
<xsd:complexType name="xyz">
<xsd:sequence>
<xsd:element name="B" …/>
<xsd:element name="C" …/>
</xsd:sequence>
</xsd:complexType>
is equivalent to:
<xsd:element name="A">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="B" …/>
<xsd:element name="C" …/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Element A references the
complexType xyz.
Element A has the
complexType definition
inlined in the element
declaration.
Type Attribute or complexType Child
Element, but not Both!
An element declaration can have a type
attribute, or a complexType child element, but it
cannot have both a type attribute and a
complexType child element.
<xsd:element name="A" type="xyz">
<xsd:complexType>
…
</xsd:complexType>
</xsd:element>
Defining an attribute
Attributes are always declared as simple types
An attribute is defined as
<xs:attribute name="name" type="type" />
where:
 name and type are the same as for xs:element
Other attributes a simple element may have:
 default="default value" if no other value is specified
 fixed="value" no other value may be specified
Primary Schema Components
<xsd:attribute name=“size” type=“xsd:integer” default=“12”>
Attribute:
<xsd:attribute ref=“size” default=“12” use=“optional”>
Example: Attribute declaration
<element name=“StdRecord”>
<complexType>
<sequence>
<element name=“SName” type=“string”/>
<element name=“Course” type=“string”/>
<element name=“Gender” type=“token”/>
<element name=“DateOfBirth” type=“date”/>
</sequence>
<attribute name=“RegNo” type=“integer”/>
</complexType>
</element>
Instance Document
<Record RegNo=“420”>
<SName>Shakti Kapoor</SName>
<Course>MS</Course>
<Gender>M</Gender>
<DateOfBirth>1963-06-01</DateOfBirth>
</Record>
Attributes
Simple elements cannot have attributes.
If an element has attributes, it is considered to be of
complex type.
The attribute itself is always declared as a simple
type..
Example:
 An XML element with an attribute
 <lastname lang="EN">Khan</lastname>
 A corresponding simple attribute definition:
 <xs:attribute name="lang" type="xs:string"/>
Declare Default and Fixed Values for Attributes
Attributes can have a default value OR a fixed value specified
A default value is automatically assigned to the attribute
when no other value is specified. In the following example the
default value is "EN":
 <xs:attribute name="lang" type="xs:string" default="EN"/>
A fixed value is also automatically assigned to the attribute.
You cannot specify another value. In the following example
the fixed value is "EN":
 <xs:attribute name="lang" type="xs:string" fixed="EN"/>
XML Schema Types
Simple types
 Basic datatypes
 Can be used for attributes and element text
 Extendable
Complex types
 Defines structure of elements
 Extendable
Types can be named or “anonymous”
The Simple Type: <simpleType>
The Simplest Type Declaration:
 <simpleType name = “FirstName” type = “string”/>
Based on a primitive or the derived built-in datatypes
Cannot contain sub-elements or attributes
Can declare constraining properties (“facets”)
 minLength, maxLength, Length, etc
May be used as base type of a complexType
Simple Types
Simple Types are of three varieties:
 Atomic: Built-in or derived, e.g.
<xsd:simpleType name="myInteger">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="10000"/>
<xsd:maxInclusive value="99999"/>
</xsd:restriction>
</xsd:simpleType>
 List: multiple items of the same type
<listOfMyInt>20003 15037 95977
95945</listOfMyInt>
 Union: Union or two or more Simple Types
XML Schema Datatypes
Two kinds of datatypes: Built-in and User-defined
Built-in
 Primitive Datatypes
 string, double, duration, etc
 Derived Datatypes:
 CDATA, integer, date, byte, etc
 Derived from the primitive types
 Example: integer is derived from decimal
User-defined
 Derived from built-in or other user-defined datatypes
 specify constraints on an existing type (the base type)
 Constraints are given in terms of facets
 totalDigits, maxInclusive, etc.
XML Schema built-in Datatypes
Built-in Datatypes
Primitive Datatypes
 string
 boolean
 decimal
 float
 double
 duration
 dateTime
 time
 date
 gYearMonth
 gYear
 gMonthDay
Atomic, built-in
 "Hello World"
 {true, false, 1, 0}
 7.08
 12.56E3, 12, 12560, 0, -0, INF, -INF, NAN
 12.56E3, 12, 12560, 0, -0, INF, -INF, NAN
 P1Y2M3DT10H30M12.3S
 format: CCYY-MM-DDThh:mm:ss
 format: hh:mm:ss.sss
 format: CCYY-MM-DD
 format: CCYY-MM
 format: CCYY
 format: --MM-DD
Note: 'T' is the date/time separator
INF = infinity
NAN = not-a-number
Date and Time Data Types
Code.xsd
<xsd:element name=“gestation”
type=“xsd:timeDuration”/>
(represent a certain amount of time)
Code.xml
<gestation>P3M15D</gestation>
(PnYnMnDTnHnMnS)
Period
n - non negative
T begins optional time section
Code.xsd
xsd:element name=“bedtime” type=“xsd.time:/>
Code.xml
<bedtime>20:15:05-05:00>/bedtime>
Date and Type Data Types
Built-in Datatypes (cont.)
Primitive Datatypes
 gDay
 gMonth
 hexBinary
 base64Binary
 anyURI
 QName
 NOTATION
Atomic, built-in
 format: ---DD
 format: --MM
 a hex string
 a base64 string
 http://www.aimit.ac.in
 a namespace qualified name
 a NOTATION from the XML spec
Built-in Datatypes (cont.)
• Derived types
– normalizedString
– token
– language
– IDREFS
– ENTITIES
– NMTOKEN
– NMTOKENS
– Name
– NCName
– ID
– IDREF
– ENTITY
– integer
– nonPositiveInteger
• Subtype of primitive datatype
– A string without tabs, line feeds, or carriage returns
– String w/o tabs, l/f, leading/trailing spaces, consecutive spaces
– any valid xml:lang value, e.g., EN, FR, ...
– must be used only with attributes
– must be used only with attributes
– must be used only with attributes
– must be used only with attributes
– part (no namespace qualifier)
– must be used only with attributes
– mmust be used only with attributes
– ust be used only with attributes
– 456
– negative infinity to 0
Built-in Datatypes (cont.)
• Derived types
– negativeInteger
– long
– int
– short
– byte
– nonNegativeInteger
– unsignedLong
– unsignedInt
– unsignedShort
– unsignedByte
– positiveInteger
• Subtype of primitive datatype
– negative infinity to -1
– -9223372036854775808 to 9223372036854775807
– -2147483648 to 2147483647
– -32768 to 32767
– -127 to 128
– 0 to infinity
– 0 to 18446744073709551615
– 0 to 4294967295
– 0 to 65535
– 0 to 255
– 1 to infinity
Note: the following types can only be used with attributes :
ID, IDREF, IDREFS, NMTOKEN, NMTOKENS, ENTITY, and ENTITIES.
Restrictions
Restrictions are used to define acceptable
values for XML elements or attributes. When
used with XML elements they are known as
“facets”.
Creating your own Datatypes
A new datatype can be defined from an
existing datatype (called the "base" type) by
specifying values for one or more of the
optional facets for the base type.
General Form of Creating a New
Datatype by Specifying Facet Values
<xsd:simpleType name= "name">
<xsd:restriction base= "xsd:source">
<xsd:facet value= "value"/>
<xsd:facet value= "value"/>
…
</xsd:restriction>
</xsd:simpleType>
Facets:
- length
- minlength
- maxlength
- pattern
- enumeration
- minInclusive
- maxInclusive
- minExclusive
- maxExclusive
...
Sources:
- string
- boolean
- number
- float
- double
- duration
- dateTime
- time
...
Restrictions
Constraint Description
enumeration Defines a list of acceptable values.
fractionDigits Specifies the maximum number of deicmal places allowed.
Must be equal to or greater than zero.
length Specifies the exact number of characters or list items allowed.
Must be absolute.
maxExclusive Specifies the upper bounds for numeric values.
maxInclusive Specifies the upper bounds for numeric values.
maxLength Specifies the maximum number of characters or list items
allowed. Must be absolute.
minExclusive Specifies the lower bounds for numeric values.
minInclusive Specifies the lower bounds for numeric values.
minLength Specifies the minimum number of characters or list items
allowed. Must be absolute.
Restrcitions
Constraint Description
pattern Defines the exact sequence of characters that are
acceptable.
totalDigits Specifies the exact number of digits allowed. Must be
greater than zero.
whiteSpace Specifies how white space is handled.
Example of Creating a New Datatype by
Specifying Facet Values
<xsd:simpleType name="TelephoneNumber">
<xsd:restriction base="xsd:string">
<xsd:length value=“15/>
<xsd:pattern value="d{4}-d{10}"/>
</xsd:restriction>
</xsd:simpleType>
1. This creates a new datatype called 'TelephoneNumber'.
2. Elements of this type can hold string values,
3. But the string length must be exactly 15 characters long and
4. The string must follow the pattern: dddd-dddddddddd, where 'd' represents
a 'digit'.
(Obviously, in this example the regular expression makes the length facet
redundant.)
1
2
3
4
Creating your own Datatypes
The string primitive datatype has six optional facets:
 length
 minLength
 maxLength
 pattern
 enumeration
 whitespace (legal values: preserve, replace, collapse)
Length
The facets length, minLength, maxLength allow to constrain the
length of an item like a string (also allow to constrain the number
of items in a list type).
Values of length, minLength, minLength should be non-negative
integers. Example:
<xsd:simpleType name="state">
<xsd:restriction base="xsd:string">
<xsd:length value="2"/>
</xsd:restriction>
</xsd:simpleType>
defines a type state representing strings containing exactly two
characters.
 These facets supported by all primitive types other than numeric and date-
and time-related types. Also supported by list types.
White Space
This facet controls how white space in a value received from
the parser is processed, prior to Schema validation. It can
take three values:
 preserve: no white space processing, beyond what base XML does.
 replace: Convert every white space character (Line Feed, etc) to a
space character (#x20).
 collapse: Like replace. All leading or trailing spaces are then
removed. Also sequences of spaces are replaced by single spaces.
 Note analogies to “Normalization of Attribute Values” in base
XML.
All simple types except union types have this attribute, but
usually you don’t explicitly set it in restriction: just inherit
values from built in types.
All built in types have collapse, except string which has
preserve and normalizedString which has replace.
Pattern
Perhaps the most powerful facet is pattern, which
allows to specify a regular expression: any allowed
value must satisfy the pattern of this expression.
Example
<xsd:simpleType name="weekday">
<xsd:restriction base="xsd:string">
<xsd:pattern
value="(Mon|Tues|Wednes|Thurs|Fri)day"/>
</xsd:restriction>
</xsd:simpleType>
 defines a type weekday representing the names of the
week days.
Regular Expressions
XML Schema has its own notation for regular
expressions, but very much based on the
corresponding Perl notation.
For the most part Schema use a subset of the
Perl 5 grammar for regular expressions.
 Includes most of the purely “declarative” features
from Perl regular expressions, but omits many
“procedural” features related to search, matching
algorithm, substitution, etc.
Metacharacters
The following characters, called metacharacters,
have special roles in Schema regular expressions:
 .  ? * + | { } ( ) [ ]
To match these characters literally in patterns, must
escape them with , e.g.:
 The pattern “2+2” matches the string “2+2”.
 The pattern “f(x)” matches the string “f(x)”.
Escape Sequences
n
r
t

|
-
^
?
*
+
{
}
(
)
[
]
linefeed
carriage return
tab
The backward slash 
The vertical bar |
The hyphen -
The caret ^
The question mark ?
The asterisk *
The plus sign +
The open curly brace {
The close curly brace }
The open paren (
The close paren )
The open square bracket [
The close square bracket ]
Escape Sequences
In general one should use XML character
references to include hard-to-type characters.
But for convenience Schema regular
expressions allow:
 n matches a newline character (same as &#xA;)
 r matches a carriage return character (same as
&#xD;)
 t matches a tab character (same as &#x9;)
Escape Sequences
All other escape sequences (except - and ^,
used only in character class expressions)
match any single character out of some set of
possible values.
 For example d matches any decimal digit, so the
pattern “Boeing ddd” matches the strings
“Boeing 747”, “Boeing 777”, etc.
Multicharacter Escapes
The simplest patterns matching classes of
characters are:
 . matches any character except carriage return
or newline.
 d matches any decimal digit.
 s matches any white space character.
 i matches any character that can start an XML
name.
 c matches any character that can appear in an
XML name.
 w matches any “word” character (excludes
punctuation, etc.)
Multicharacter Escapes
The escapes D, S, I, C and W are
negative forms, e.g. D matches any character
except a decimal digit.
Category Escapes
A large and interesting family of escapes is based on
the Unicode standard. General form is
p{Name}
where Name is a Unicode-defined class name.
 The negative form P{Name} matches any character not
in the class.
Simple examples include: p{L} (any letter), p{Lu}
(upper case letters), p{Ll} (lower case letters), etc.
More interesting cases are based on the Unicode
block names for alphabets, e.g.:
 p{IsBasicLatin}, p{IsLatin-1Supplement}, p{IsGreek},
p{IsArabic}, p{IsDevanagari}, p{IsHangulJamo},
p{IsCJKUnifiedIdeographs}, etc, etc, etc.
Category Escapes
p{L}
p{Lu}
p{Ll}
p{N}
p{Nd}
p{P}
p{Sc}
A letter, from any language
An uppercase letter, from any language
A lowercase letter, from any language
A number - Roman, fractions, etc
A digit from any language
A punctuation symbol
A currency sign, from any language
Character Class Expressions
Allow you to define terms that match any
character from a custom set of characters.
Basic syntax is familiar from Perl and UNIX:
[List-of-characters]
or the negative form:
[^List-of-characters]
Here List-of-characters can include individual
characters, and also ranges of the form First-
Last where First and Last are characters.
Character Class Expressions
Examples:
 [RGB] matches one of R, G, or B.
 [0-9A-F] or [dA-F] match one of 0, 1, …, 9, A, B,…, F.
 [^rn] matches anything except CR, NL (same as . ).
Restriction Example - Series
<xs:element name=“char">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value=“[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restriction Example - Series
<xs:element name=“choice">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value=“nyNY"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Class Subtractions
A feature of XML Schema, not present in Perl 5.
A class character expression can take the form:
[List-of-characters-Class-char-expr]
or:
[^List-of-characters-Class-char-expr]
where Class-char-expr is another class
character expression.
Example:
 [a-zA-Z-[aeiouAEIOU]] matches any consonant in
the Latin alphabet.
Sequences and Alternatives
Finally, the universal core of regular
expressions. If Pattern1 and Pattern2 are
regular expressions, then:
 Pattern1Pattern2 matches any string made by
putting a string accepted by Pattern1 in front of a
string accepted by Pattern2.
 Pattern1|Pattern2 matches any string that would
be accepted by Pattern1, or any string accepted
by Pattern2.
Parentheses just group things together:
 (Pattern1) matches any string accepted by
Pattern1.
Sequences and Alternatives
An example given earlier:
 (Mon|Tues|Wednes|Thurs|Fri)day matches any
of the strings Monday, Tuesday, Wednesday,
Thursday, or Friday.
 Equivalent to
Monday|Tuesday|Wednesday|Thursday|Friday.
Restriction Example - Series
<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>
Restriction Example - Series
<xs:element name=“areacode">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value=“[0-9][0-9][0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Quantifiers
If Pattern1 is a regular expression:
 Pattern1? matches the empty string or any string
accepted by Pattern1.
 Pattern1+ matches any string accepted by Pattern1,
or by Pattern1Pattern1, or by
Pattern1Pattern1Pattern1, or …
 Pattern1* matches the empty string or any string
accepted by Pattern1+.
Regular Expressions
Regular Expression
Chapter d
Chapter&#x020;d
Chaptersd
a*b
[xyz]b
a?b
a+b
[a-c]x
[-ac]x
[ac-]x
[^0-9]x
Dx
Example
Chapter 1
Chapter 1
Chapter followed by a blank followed by a digit
b, ab, aab, aaab, …
xb, yb, zb
b, ab
ab, aab, aaab, …
ax, bx, cx
-x, ax, cx
ax, cx, -x
any non-digit char followed by x
any non-digit char followed by x
Quantifiers
If n, m are numbers, XML Schema also allow the
shorthand forms:
 Pattern1{n} is equivalent to Pattern1 repeated n
times.
 Pattern1{m,n} matches any string accepted by
Pattern1 repeated m times or m + 1 times or … or n
times.
 Pattern1{m,} matches any string accepted by
Pattern1 repeated m or more times.
Regular Expressions (cont.)
Regular Expression
 (ho){2} there
 (hos){2} there
 .abc
 (a|b)+x
 a{1,3}x
 a{2,}x
 wsw
Example
 hoho there
 ho ho there
 any (one) char followed by abc
 ax, bx, aax, bbx, abx, bax,...
 ax, aax, aaax
 aax, aaax, aaaax, …
 word character
(alphanumeric plus dash)
followed by a space followed by
a word character
Regular Expressions (cont.)
[a-zA-Z-[Ol]]* • A string comprised of any
lower and upper case
letters, except "O" and "l"
. • The period "." (Without the
backward slash the period
means "any character")
Example of Derived Simple Type
(Regular expression)
<xsd:simpleType name=“ProductKey">
<xsd:restriction base="xsd:string">
<xsd:pattern value="d{3}-[A-Z]{2}"/>
</xsd:restriction>
</xsd:simpleType>
Restrictions – Password Example
<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>
This element must contain a literal string composed of
upper or lower case letters, or numerals. It must
have 8 characters in total.
Using Patterns in Restriction
All simple types (including lists and enumerations)
support the pattern facet, e.g.:
<simpleType name=“multiplesOfFive">
<restriction base="xs:integer">
<pattern value=“[+-]?d*[05]"/>
</restriction>
</simpleType>
defines a subtype of integer including all numbers
ending with digits 0 or 5.
Example R.E.
[1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]
0 to 99 100 to 199 200 to 249 250 to 255
This regular expression restricts a string to have
values between 0 and 255.
… Such a R.E. might be useful in describing an
IP address ...
IP Datatype Definition
<xsd:simpleType name="IP">
<xsd:restriction base="xsd:string">
<xsd:pattern value="(([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).){3}
([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])">
<xsd:annotation>
<xsd:documentation>
Datatype for representing IP addresses. Examples,
129.83.64.255, 64.128.2.71, etc.
This datatype restricts each field of the IP address
to have a value between zero and 255, i.e.,
[0-255].[0-255].[0-255].[0-255]
Note: in the value attribute (above) the regular
expression has been split over two lines. This is
for readability purposes only. In practice the R.E.
would all be on one line.
</xsd:documentation>
</xsd:annotation>
</xsd:pattern>
</xsd:restriction>
</xsd:simpleType>
Regular Expressions (concluded)
p{L}
p{Lu}
p{Ll}
p{N}
p{Nd}
p{P}
p{Sc}
A letter, from any language
An uppercase letter, from any language
A lowercase letter, from any language
A number - Roman, fractions, etc
A digit from any language
A punctuation symbol
A currency sign, from any language
<xsd:simpleType name="money">
<xsd:restriction base="xsd:string">
<xsd:pattern value="p{Sc}p{Nd}+(.p{Nd}p{Nd})?"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="cost" type="money"/>
"currency sign from any language, followed
by one or more digits from any language,
optionally followed by a period and
two digits from any language"
<cost>$45.99</cost>
<cost>¥300</cost>
Enumeration
The enumeration facet allows one to select a finite
subset of allowed values from a base type, e.g.:
<xsd:simpleType name="weekday">
<xsd:restriction base="xs:string">
<xsd:enumeration value="Monday"/>
<xsd:enumeration value="Tuesday"/>
<xsd:enumeration value="Wednesday"/>
<xsd:enumeration value="Thursday"/>
<xsd:enumeration value="Friday"/>
</xsd:restriction>
</xsd:simpleType>
 Behaves like a very restricted version of pattern?
 All primitive types except boolean support the enumeration
facet. List and union types also support this facet.
Example of Derived Simple Type 3
(Enumeration)
<xsd:simpleType name="State">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="AP"/>
<xsd:enumeration value=“KA"/>
<xsd:enumeration value="TN/>
<!-- and so on ... -->
</xsd:restriction>
</xsd:simpleType>
enumeration facet limits a simple type to a set of distinct
values
Restriction Example - Set
<xs:element name=“car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value=“Maruthi“ />
<xs:enumeration value=“Fiat“/>
<xs:enumeration value=“Ford“ />
<xs:enumeration value=“Tata“ />
<xs:enumeration value=“Santro” />
</xs:restriction>
</xs:simpleType>
</xs:element>
Enumeration
An enumeration restricts the value to be one of a
fixed set of values
Example:
 <xs:element name="season">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Spring"/>
<xs:enumeration value="Summer"/>
<xs:enumeration value="Autumn"/>
<xs:enumeration value="Fall"/>
<xs:enumeration value="Winter"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Another Example
<xsd:simpleType name="shape">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="circle"/>
<xsd:enumeration value="triangle"/>
<xsd:enumeration value="square"/>
</xsd:restriction>
</xsd:simpleType>
This creates a new type called shape.
An element declared to be of this type
must have either the value circle, or triangle, or square.
Enumeration - Example
<xsd:simpleType name=“GenderType”>
<xsd:restriction base=“token”>
<xsd:enumeration value=“M”/>
<xsd:enumeration value=“F”/>
<xsd:enumeration value=“NK”/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name=“Gender” type=“GenderType”/>
<Gender>NK</Gender>
<Gender>Male</Gender>
Using Patterns in Restriction
The pattern facet can appear more than once
in a single restriction: interpretation is as if
patterns were combined with |.
 Conversely if the pattern facet is specified in
restriction of a base type that was itself defined
using a pattern, allowed values must satisfy both
patterns.
Multiple Facets - "and" them together, or
"or" them together?
An element declared to
be of type
TelephoneNumber must
be a string of length=8
and the string must
follow the pattern: 3
digits, dash, 4 digits.
<xsd:simpleType name="shape">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="circle"/>
<xsd:enumeration value="triangle"/>
<xsd:enumeration value="square"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="TelephoneNumber">
<xsd:restriction base="xsd:string">
<xsd:length value="8"/>
<xsd:pattern value="d{3}-d{4}"/>
</xsd:restriction>
</xsd:simpleType>
An element declared to be
of type shape must be a
string with a value of either
circle, or triangle, or
square.
Patterns, enumerations => "or" them together
All other facets => "and" them together
Numerical - Facets
The facets maxInclusive, maxExclusive,
minExclusive, minInclusive are supported
only by numeric and date- and time-related
types, and define bounds of value ranges.
The facets totalDigits, fractionDigits are
defined for the primitive type decimal, and
thus all numeric types derived from decimal,
and for no other types.
Facets of the integer Datatype
The integer datatype has 8 optional facets:
 totalDigits
 pattern
 whitespace
 enumeration
 maxInclusive
 maxExclusive
 minInclusive
 minExclusive
Restrictions on numbers
minInclusive -- number must be ≥ the given value
minExclusive -- number must be > the given value
maxInclusive -- number must be ≤ the given value
maxExclusive -- number must be < the given value
totalDigits -- number must have exactly value digits
fractionDigits -- number must have no more than value digits
after the decimal point
A Simple Type Example (1 of 4)
Integer with value (1234, 5678]
<xsd:simpleType name=‘MyInteger’>
<xsd:restriction base=‘xsd:integer’>
<xsd:minExclusive value=‘1234’/>
<xsd:maxInclusive value=‘5678’/>
</xsd:restriction>
</xsd:simpleType>
A Simple Type Example (2 of 4)
Integer with value [1234, 5678)
<xsd:simpleType name=‘MyInteger’>
<xsd:restriction base=‘xsd:integer’>
<xsd:minInclusive value=‘1234’/>
<xsd:maxExclusive value=‘5678’/>
</xsd:restriction>
</xsd:simpleType>
A Simple Type Example (3 of 4)
Integer with value [1234, 5678]
<xsd:simpleType name=‘MyInteger’>
<xsd:restriction base=‘xsd:integer’>
<xsd:minInclusive value=‘1234’/>
<xsd:maxInclusive value=‘5678’/>
</xsd:restriction>
</xsd:simpleType>
A Simple Type Example (4 of 4)
Validating integer with value (1234, 5678]
<data xsi:type='MyInteger'></data> INVALID
<data xsi:type='MyInteger'>Sandy</data> INVALID
<data xsi:type='MyInteger'>-32</data> INVALID
<data xsi:type='MyInteger'>1233</data> INVALID
<data xsi:type='MyInteger'>1234</data> INVALID
<data xsi:type='MyInteger'>1235</data>
<data xsi:type='MyInteger'>5678</data>
<data xsi:type='MyInteger'>5679</data> INVALID
<xsd:simpleType name=‘MyInteger’>
<xsd:restriction base=‘xsd:integer’>
<xsd:minExclusive value=‘1234’/>
<xsd:maxInclusive value=‘5678’/>
</xsd:restriction>
</xsd:simpleType>
Restriction Example - Simple
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Simple Type Definition
Example
<xs:simpleType name="farenheitWaterTemp">
<xs:restriction base="xs:number">
<xs:fractionDigits value="2"/>
<xs:minExclusive value="0.00"/>
<xs:maxExclusive value="100.00"/>
</xs:restriction>
</xs:simpleType>
Fixing a Facet Value
Sometimes when we define a simpleType we
may like to have a unchanging value for one (or
more) facet. That is, we want to make the facet
a constant.
<xsd:simpleType name= "ClassSize">
<xsd:restriction base="xsd:nonNegativeInteger">
<xsd:minInclusive value="10" fixed="true"/>
<xsd:maxInclusive value="60"/>
</xsd:restriction>
</xsd:simpleType>
simpleTypes which derive from this simpleType may not change this facet.
<xsd:simpleType name= "ClassSize">
<xsd:restriction base="xsd:nonNegativeInteger">
<xsd:minInclusive value="10" fixed="true"/>
<xsd:maxInclusive value="60"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name= “MCAClassSize">
<xsd:restriction base="ClassSize">
<xsd:minInclusive value="15"/>
<xsd:maxInclusive value="60"/>
</xsd:restriction>
</xsd:simpleType>
Error! Cannot
change the value
of a fixed facet!
PurchaseOrder.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:annotation>
<xsd:documentation xml:lang="en">
Sample Purchase order schema
</xsd:documentation>
</xsd:annotation>
<xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
<xsd:element name="comment" type="xsd:string"/>
<xsd:complexType name="PurchaseOrderType">
<xsd:sequence>
<xsd:element name="shipTo" type="Address"/>
<xsd:element name="billTo" type="Address"/>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="items" type="Items"/>
</xsd:sequence>
<xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>
<xsd:complexType name="Address">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="street" type="xsd:string"/>
<xsd:element name="city" type="xsd:string"/>
<xsd:element name="state" type="xsd:string"/>
<xsd:element name="zip" type="xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN"
fixed="IN"/>
</xsd:complexType>
<xsd:complexType name="Items">
<xsd:sequence>
<xsd:element name="item" minOccurs="0"
maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="productName" type="xsd:string"/>
<xsd:element name="quantity">
<xsd:simpleType>
<xsd:restriction base="xsd:positiveInteger">
<xsd:maxExclusive value="100"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Price" type="xsd:decimal"/>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="shipDate" type="xsd:date"
minOccurs="0"/>
</xsd:sequence>
<xsd:attribute name="partNum" type="SKU"
use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<!-- Stock Keeping Unit, a code for identifying products -->
<xsd:simpleType name="SKU">
<xsd:restriction base="xsd:string">
<xsd:pattern value="d{3}-[A-Z]{2}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
List Type
We define a type representing a white-space-
separated list of items using the list element.
 Comprised of sequences of atomic simple types
A list value is split according to its white space
content prior to validation of the items in the
list
 Three built-in list types
 NMTOKENS, IDREFS, ENTITIES
 User defined List type
 Derive from atomic types
 Facets
 length, minLength, maxLength, enumeration.
Example of List Type
The xsd:list element creates a new simple type that
allows the element to contain a sequence of values
of the base type, which are separated with white
space characters (spaces, tabs, or line breaks).
Schema
<xsd:simpleType name="listOfMyIntType">
<xsd:list itemType="myInteger"/>
</xsd:simpleType>
Instance Document
<listOfMyInt>20003 15037 95977 95945</listOfMyInt>
Example: List Type with Facet
list of integers where 5 is the maximum
number of items allowed in the list.
<xs:simpleType name='derivedlistOfIntegers'>
<xs:restriction base='listOfIntegers'>
<xs:maxLength value='5'>
</xs:restriction>
</xs:simpleType>
 Usage in XML instance document
<myelement listOfIntegers='1 100 9 4000 0'/>
Example: List Type with Facet
List Type for Five Colors
<xsd:simpleType name="ColorList">
<xsd:list itemType="Color"/>
</xsd:simpleType>
<xsd:simpleType name="FiveColors">
<xsd:restriction base="ColorList">
<xsd:length value="5"/>
</xsd:restriction>
</xsd:simpleType>
Use in XML instance document
<fiveColors>white black blue red green</fiveColors>
Example: List Type with Facet
<xsd:simpleType name=“StateList">
<xsd:list itemType=“State"/>
</xsd:simpleType>
<xsd:simpleType name="SixStates">
<xsd:restriction base=“StateList">
<xsd:length value="6"/>
</xsd:restriction>
</xsd:simpleType>
<element name=“SelectedStates” type=“SixStates”>
Define a list of exactly six states (SelectedStates), we first
define a new list type called StateList from State, and then we
derive SixStates by restricting StateList to only six items
< SelectedStates>KA KL GA TN AP MP</SelectedStates>
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
Union Type
The xsd:union element creates a new simple
type that allows the element to contain a
value that conforms to any one of a group of
specified base types.
Creating a simpleType that is a Union of Types
simpleType 1 simpleType 2
simpleType 1
+
simpleType 2
Note: you can create a union of more
than just two simpleTypes
Methods for creating Union simpleType
<xsd:simpleType name="name">
<xsd:union memberTypes="space delimited simpleTypes"/>
</xsd:simpleType>
Alternatively,
<xsd:simpleType name="name">
<xsd:union>
<xsd:simpleType>
…
</xsd:simpleType>
<xsd:simpleType>
…
</xsd:simpleType>
…
</xsd:union>
</xsd:simpleType>
Union Type for Zipcodes
<xsd:simpleType name="zipUnion">
<xsd:union memberTypes=“State listOfMyIntType"/>
</xsd:simpleType>
<element name=zips type=“zipUnion”>
<zips>KA</zips>
<zips>575001 613256 715872</zips>
<zips>UP</zips>
<xs:simpleType name="toddlerCounting">
<xs:union>
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="3"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="many"/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
Simple Types : Union
union of two simple types.
<xs:attribute name="fontsize">
<xs:simpleType>
<xs:union memberTypes="fontbynumber fontbystringname" />
</xs:simpleType>
</xs:attribute>
<xs:simpleType name="fontbynumber">
<xs:restriction base="xs:positiveInteger">
<xs:maxInclusive value="72"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="fontbystringname">
<xs:restriction base="xs:string">
<xs:enumeration value="small"/>
<xs:enumeration value="medium"/>
<xs:enumeration value="large"/>
</xs:restriction>
</xs:simpleType>
union of two simple types.
<xs:attribute name="MonitorSize">
<xs:simpleType>
<xs:union memberTypes="sizebynumber sizebystringname" />
</xs:simpleType>
</xs:attribute>
<xs:simpleType name="sizebynumber">
<xs:restriction base="xs:positiveInteger">
<xs:maxInclusive value="21"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="sizebystringname">
<xs:restriction base="xs:string">
<xs:enumeration value="small"/>
<xs:enumeration value="medium"/>
<xs:enumeration value="large"/>
</xs:restriction>
</xs:simpleType>
Prohibiting Derivation
You may, for some reason, have a simple type that you
don’t want anybody to derive further types from.
Do this by specifying the final attribute on the
<simpleType> element. Its value is a list containing a
subset of values from list, union, restriction,
extension.
Give the final attribute the value “#all” for blanket
prohibition of any derivation from this simple type.
 Can also prevent the value of individual facets from being
changed in subsequent restrictions by specifying
fixed="true" on the facet elements.
Prohibit XML Schema Derivations
The final attribute
 Can be used in
 xs:complexType
 xs:simpleType
 xs:element
 Can take values of
 restriction
 extension
 #all (any derivation)
The fixed attribute
 Can only be used in
xs:simpleType
 When it is set to true, it
cannot be further
modified
<xs:complexType name="characterType" final="#all">
<xs:sequence>
<xs:element name="name" type="nameType"/>
<xs:element name=“age" type=“ageType"/>
<xs:element name="qualification" type="descType"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name=“fixedString">
<xs:restriction base="xs:string">
<xs:maxLength value="32" fixed="true"/>
</xs:restriction>
</xs:simpleType>
Complex Types
Complex Types define logical structures with
attributes and nested elements
They use a sequence, choice or all
containing elements that use Simple Types or
other Complex Types
May reference types defined elsewhere in the
schema or imported using import statement
Definition – complex type
<xsd:complexType name="PurchaseOrderType">
<xsd:sequence>
<xsd:element name="shipTo" type="Address"/>
<xsd:element name="billTo" type="Address"/>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="items" type="Items"/>
</xsd:sequence>
<xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>
Define the complex type “purchaseOrderType”
 Allow elements & attributes
 contain a set of element declarations, element references, and attribute
declarations.
element declarations
attribute declaration
element reference
Element Content
Three different ways
 Complex types from simple types
 Mixed content
 Elements mixed with character content
 Empty content
XML-Schema : Mixed, Empty and Any Content
Mixed content is used if you want to model elements
that includes both subelements and character data
Empty content is used to define elements that do not
include any subelements and character data
Any content (the most basic data type) does not
constrain the content in any way
Example : Defining a complexType
<complexType name= “Customer”>
<sequence>
<element name= “Person” type=“Name” />
<element name= “Address” type=“AddressT” />
</sequence>
</complexType>
<complexType name=“AddressT”>
<sequence>
<element name=“Street” type=“string” />
<element name=“City” type=“string” />
<element name=“State” type=“State_Region” />
<element name=“PostalCode” type=“string” />
<element name=“Country” type=“string” />
</sequence>
<!-- Attributes definition goes here -->
</complexType>
Complex Type from a Simple Type
<xsd:element name="internationalPrice">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:decimal">
<xsd:attribute name="currency” type="xsd:string" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
XML Instance:
<internationalPrice currency="EUR">423.46</internationalPrice>
Mixed elements
Mixed elements may contain both text and
elements
We add mixed="true" to the xs:complexType element
<xs:complexType name="paragraph" mixed="true">
<xs:sequence>
<xs:element name="someName" type="xs:anyType"/>
</xs:sequence>
</xs:complexType>
Mixed Content
Sub-elements mixed with character data
<letterBody>
<salutation>Dear Mr.<name>Anu Malik</name>.
</salutation>Your order of <quantity>1</quantity>
<productName>LCD Monitor</productName>
shipped from our warehouse on <shipDate>2009-09-
18</shipDate>. ....
</letterBody>
Mixed Content
<xsd:element name="letterBody">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="salutation">
<xsd:complexType mixed="true"> <!-- Implicit definition ->
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="quantity" type="xsd:positiveInteger"/>
<xsd:element name="productName" type="xsd:string"/>
<xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
<!-- etc -->
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Empty Elements
XML Schema doesn’t have any unique way of
representing elements that must be empty.
The simplest thing to do this is simply omit the
allowed element content in a complex content
restriction.
Can such an element also be mixed (i.e. have
pure text content)?
 Logically it seems this should be possible
 But it seems to be forbidden by the XML Schema
specification, which singles out this case and says
such an element is strictly empty.
Empty Elements
To specify Empty type of element, define the
element’s type using the xsd:complexType
element, but omit the content model.
<xsd:element name=”BR”>
<xsd:complexType>
</xsd:complexType>
</xsd:element>
The following is a conforming element:
 <BR></BR>
as is this one:
 <BR/>
Empty Content
<img src="xml.gif" alt="Programming XML image"/>
<xsd:element name="img">
<xsd:complexType>
<xsd:attribute name="src“ type="xsd:NMTOKEN"/>
<xsd:attribute name="alt" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
Complex Type Hierarchy
There are no built in complex types, other than
the so-called ur-type, represented as
xsd:anyType.
All other complex types are derived by one or
more steps of restriction and extension from
xsd:anyType.
 Complex types can also be created by extension
of a simple type, but simple types are also
notionally restrictions of xsd:anyType.
XML Schema built-in Datatypes
Restriction
A restriction of a base type is a new type.
All allowed instances of the new type are also
instances of the base type. But the restricted type
doesn’t allow all possibilities allowed by the base
type.
 Think of the example of restricting xsd:string to 4
characters using the length facet. Strings of length 4 are
also allowed by the xsd:string, but the new type is more
restrictive.
 In the complex case, we might have a complex base type
that allows attribute att optionally. A restricted type might
not allow att at all.
 Another restriction of the same base might require att.
 Or we might have a base type that allows 0 or more nested
elm elements. The restricted type might require exactly 1
nested elm element.
Extension
An extension of a base type is a new type.
An extension allows extra attributes or extra content
that are not allowed in instances of the base type.
 At first brush this sounds like the opposite of restriction,
but this isn’t strictly true.
 If, for example, type E extends a type B by adding a
required attribute att, then instances of B are not allowed
instances of E (because they don’t have the required
attribute). So we have that E is an extension of B, but
there is no sense in which B could be a restriction of E.
 Some such inverse relation exists if all extra
attributes and content are optional in the extended
type, but this isn’t a required feature of extension.
Remarks
When one restricts a type one generally must
specify all allowed element content and
attributes.
When one extends a type one generally must
specify just the extra element content and
attributes.
Complex Content and Simple Content
We have seen that XML Schema complex types define
both some allowed nested elements, and some
allowed attribute specifications. Complex types that
allow nested elements are said to have complex
content.
But Schema distinguish as a special case complex
types having simple content—elements with such
types may have attributes, but they cannot have
nested elements.
This is presumably a useful distinction, but it does
introduce one more layer of complexity into the syntax
for complex type derivation.
Complex Content and Simple Content
Simple
Type
Complex
Type
Simple
Content
Complex
Content
Type
allows attributes or elements?
allows elements in
content?
Simple contents
A good example of complex elements with simple
content is the <textarea> element in HTML
documents. It accepts a text string as the content
with no sub (child) element and some attributes.
<xs:element name="textarea">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="cols" type="xs:positiveInteger"/>
<xs:attribute name="rows" type="xs:positiveInteger"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
Simple contents
Simple contents contain only attributes
 E.g.
<xs:element name="organism">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="taxonomy_id" type="xs:integer"
use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
Simple content type
<xsd:element name="internationalPrice">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:decimal">
<xsd:attribute name="currency” type="xsd:string" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<internationalPrice currency="EUR">423.46</internationalPrice>
simpleContent indicates that the content model of the new type
contains only character data and no element declaration
Complex contents
Elements including children are defined as
complex types with complex content
 E.g.
<xs:element name="protein_set">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="xs:anyType">
<xs:sequence>
<xs:element ref="protein" maxOccurs="unbounded"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:element>
Basic Forms of Complex Type Definition
Restriction Extension
<complexType>
<complexContent>
<restriction base="type">
allowed element content
allowed attributes
</restriction>
</complexContent>
</complexType>
<complexType>
<complexContent>
<extension base="type">
extra element content
extra attributes
</extension>
</complexContent>
</complexType>
<complexType>
<simpleContent>
<restriction base="type">
facet restrictions
allowed attributes
</restriction>
</simpleContent>
</complexType>
<complexType>
<simpleContent>
<extension base="type">
extra attributes
</extension>
</simpleContent>
</complexType>
Requirements on Base Type
The base type must be a complex type in all
cases except simpleContent/extension
(lower right in table shown in the previous
slide), in which case the base can be a
simple type.
Schematic Inheritance Diagram
xsd:anyType
Simple Types
Simple
Content
Complex
Content
extension
restriction
restriction
restriction
Complex
Types
restriction
list
union
restriction
extension
restriction
extension
restriction
Defining a Complex Type with no Base?
Actually the XML Schema specification says that:
<complexType>
<complexContent>
<restriction base="xsd:anyType">
allowed element content
allowed attributes
</restriction>
</complexContent>
</complexType>
<complexType>
allowed element
content
allowed attributes
</complexType>
 So in reality we were
directly restricting the
ur-type, which allows
any attributes and any
content!
Is “shorthand” for
Empty Content - 1
<internationalPrice currency=“EUR” value=“345.23”/>
<xsd:element name="internationalPrice">
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:attribute name="currency" type="xsd:string"/>
<xsd:attribute name="value” type="xsd:decimal"/>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
Empty Content - 2
<xsd:element name="internationalPrice">
<xsd:complexType>
<xsd:attribute name="currency” type="xsd:string"/>
<xsd:attribute name="value” type="xsd:decimal"/>
</xsd:complexType>
</xsd:element>
A complex type defined without complexContent is
interpreted as shorthand for complex content that
restricts anyType
XSD Complex Types Indicators
We have seven types of indicators:
 Order indicators:
 All
 Choice
 Sequence
 Occurrence indicators:
 maxOccurs
 minOccurs
 Group indicators:
 Group name
 attributeGroup name
Defining Element Content
Where we wrote allowed element content or
extra element content in the syntax for
complex type definitions, what should appear
is a model group.
A model group is exactly one of:
 an <xsd:sequence/> element, or
 an <xsd:choice/> element, or
 an <xsd:all/> element.
(The element content appearing in the type definition may
also be a globally defined model group, referenced through an
<xsd:group/> element. The global definition—a named
<xsd:group/> element—just contains one of the three
elements above.)
Sequence
A <xsd:sequence/> model group contains a series of
particles. A particle is an <xsd:element/> element,
another model group, or a wildcard.
As expected, this model just says the element content
represented by those items should appear in
sequence.
E.g.
<xsd:sequence>
<xsd:element ref="title"/>
<xsd:element ref="paragraph"
minOccurs="0“ maxOccurs="unbounded"/>
</xsd:sequence>
xs:sequence
We’ve already seen an example of a complex
type whose elements must occur 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>
Complex Types
(title?,firstname*,lastname)
<xsd:complexType name="name">
<xsd:sequence>
<xsd:element name="title" minOccurs="0"/>
<xsd:element name="firstname" minOccurs="0"
maxOccurs="unbounded"/>
<xsd:element name="lastname"/>
</xsd:sequence>
</xsd:complexType>
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained
XML Schema Definition (XSD) Explained

More Related Content

Similar to XML Schema Definition (XSD) Explained

Similar to XML Schema Definition (XSD) Explained (20)

Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
DATA INTEGRATION (Gaining Access to Diverse Data).ppt
DATA INTEGRATION (Gaining Access to Diverse Data).pptDATA INTEGRATION (Gaining Access to Diverse Data).ppt
DATA INTEGRATION (Gaining Access to Diverse Data).ppt
 
Xsd examples
Xsd examplesXsd examples
Xsd examples
 
Oracle soa xml faq
Oracle soa xml faqOracle soa xml faq
Oracle soa xml faq
 
Web Technologies Unit 2 Print.pdf
Web Technologies Unit 2 Print.pdfWeb Technologies Unit 2 Print.pdf
Web Technologies Unit 2 Print.pdf
 
XML Schema
XML SchemaXML Schema
XML Schema
 
Document type definition
Document type definitionDocument type definition
Document type definition
 
XML Databases
XML DatabasesXML Databases
XML Databases
 
Basics of XML
Basics of XMLBasics of XML
Basics of XML
 
Xml by Luqman
Xml by LuqmanXml by Luqman
Xml by Luqman
 
Xml
XmlXml
Xml
 
Xml andweb services
Xml andweb services Xml andweb services
Xml andweb services
 
Xml 215-presentation
Xml 215-presentationXml 215-presentation
Xml 215-presentation
 
CTDA Workshop on XML and MODS
CTDA Workshop on XML and MODSCTDA Workshop on XML and MODS
CTDA Workshop on XML and MODS
 
XPATH_XSLT-1.pptx
XPATH_XSLT-1.pptxXPATH_XSLT-1.pptx
XPATH_XSLT-1.pptx
 
Chapter 18
Chapter 18Chapter 18
Chapter 18
 
Xml 215-presentation
Xml 215-presentationXml 215-presentation
Xml 215-presentation
 
Web data management (chapter-1)
Web data management (chapter-1)Web data management (chapter-1)
Web data management (chapter-1)
 
Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12
 
unit_5_XML data integration database management
unit_5_XML data integration database managementunit_5_XML data integration database management
unit_5_XML data integration database management
 

Recently uploaded

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 

Recently uploaded (20)

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 

XML Schema Definition (XSD) Explained

  • 1. XML Schema Mani Bushan D’souza mail2mani@email.com
  • 2. Topics Covered What is Schema How to use the XML Schema language in applications
  • 4. What is it? An XML Schema defines the legal structure that an XML document may take, much like a blueprint. This is accomplished by: defining elements and child elements defining attributes defining the order of child elements defining element content defining data types for elements and attributes defining default and fixed values
  • 5. Definition and Declaration Definition  Create new types (both simple and complex types) Declaration  Enable elements and attributes with specific names and types (both simple and complex) to appear in document instances
  • 6. Schema vs. DTD Schemas are extensible. Schemas have more depth and power. Schemas are written in XML. Schemas support data types. Schemas support namespaces.
  • 7. Document Type Definition An Information Modeling Syntax a b x y DTD “a is the parent of b. x and y are children of b.” <a> <b> <x>…</x> <y>…</y> </b> </a> Enforces Structure x and y are always interpreted as String, regardless of how they are used Schema vs. DTD
  • 8. XML Schema Information Modeling Syntax in XML a b x y XML Schema <a> <b> <x>…</x> <y>…</y> </b> </a> Enforces Structure <xsl:element name=“x” type=“boolean” /> <xsl:attribute name=“z” type=“integer” /> x and y now have enforceable data types Schema vs. DTD
  • 9. Comparing Element Declarations Differences in data typing and syntax DTD <!ELEMENT Age (#PCDATA)> XML Schema <element name=“Age” type=“integer”/> XML <Age>2009</Age>
  • 10. Why Support Data Types? Easier to describe allowable document content. Easier to validate data. Easier to work with data from a database. Easier to define data restrictions. Easier to define data formats. Easier to convert data between types.
  • 11. Schema Validation Schemas can be used to validate a document in two ways: Content Model Validation Checks order and nesting of elements (similar to DTD validation) DataType Validation Checks the element content for valid type and range example: month element is an integer between 1 and 12 <month>5</month> VALID!! <month>15</month> INVALID!!
  • 12. Comparison with DTDs DTDs use their own unique syntax (SGML) XML Schema uses XML syntax DTDs are concise XML Schema is verbose
  • 13. Comparison with DTDs XML Schema can be parsed and manipulated programmatically like any other XML document DTDs cannot (at least not without a bit of work) Note: Custom DTD parsers are available, but the ability to parse a DTD is not inherent in most XML Parsers. XML Schema enforce Data Typing DTDs cannot (DTDs see everything as Strings)
  • 14. Comparison with DTDs XML Schema allows open-ended data models  Vocabulary extension and inheritance DTDs support only a closed model XML Schema supports attribute groups  DTDs offer only limited attribute group support
  • 15. Comparison with DTDs Schemas are Extensible  Reuse Schemas in other Schemas.  Create proprietary data types from public types.  Reference multiple Schemas in one document.
  • 16. Comparison with DTDs XML Schema supports namespace integration  Allows the association of individual nodes of a document with type declarations in a schema DTDs allow only one association (between the document and the DTD)
  • 17. Multiple levels of checking BookCatalogue.xml BookCatalogue1.xsd xml-schema.dtd Does the xml document conform to the rules laid out in the xml-schema? Is the xml-schema a valid xml document, i.e., does it conform to the rules laid out in the xml-schema DTD?
  • 18. Schema Features Rich Datatypes ( integers, time, date, boolean.. ) User-defined types Extendable types Open, closed or refinable content models open: Additional elements may appear refinable: Additional elements may appear if they are defined in the schema Grouping Namespace support
  • 19. Schemas v. DTDs Schemas DTDs Support namespaces n/a Written in XML syntax n/a Full, object-oriented extensibility Very limited Extensive datatype support Extended via string substitutions Open, closed or refinable content models Closed only
  • 20. End of DTDs? DTDs have: Widespread use and support Many legacy applications, documents Too much time & money invested Experienced programmers/consultants There is a DTD to define XML Schema
  • 21. Benefits of XML and Schemas You don’t need to learn a new language. Editor of choice will work on Schemas. Current parser will work on Schemas. You can manipulate your Schema with the XML DOM. Schemas are transformable with XSLT.
  • 22. Schema Workflow XML Document Normalized XML Document Schema Error message Schema Processor Valid Invalid
  • 23. XSD (XML Schema Definition) How To Look at this simple XML document called "note.xml":  <?xml version="1.0"?> <note> <to>Tom</to> <from>Jerry</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> This is a simple DTD file called "note.dtd" that defines the elements of the XML document above ("note.xml"):  <!ELEMENT note (to, from, heading, body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)>
  • 24. Simple XML schema <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.aimit.com" xmlns="http://www.aimit.com" elementFormDefault="qualified"> <xsd:element name="note"> <xsd:complexType> <xsd:sequence> <xsd:element name="to" type="xsd:string"/> <xsd:element name="from" type="xsd:string"/> <xsd:element name="heading" type="xsd:string"/> <xsd:element name="body" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
  • 25. The <schema> element The <schema> is the root element of every XML schema <?xml version="1.0"?> <xsd:schema> ... ... </xsd:schema> The <schema> element may contain some attributes. A schema declaration often looks something like this: <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.aimit.com" xmlns="http://www.aimit.com" elementFormDefault="qualified"> ... ... </xsd:schema> The schema element must specify the namespace for schemas as its xmlns:xsd attribute
  • 26. XML <schema> element explained xmlns:xsd="http://www.w3.org/2001/XMLSchema”  indicates that the elements and data types used in the schema (schema, element, complexType, sequence, string, boolean, etc.) 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 xsd: targetNamespace="http://www.aimit.com"  indicates that the elements defined by this schema (note, to, from, heading, body.) come from the "http://www.aimit.com" namespace. xmlns="http://www.aimit.com"  indicates that the default namespace is "http://www.aimit.com". elementFormDefault="qualified"  indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.
  • 27. The XSD document Since the XSD is written in XML, it can get confusing, what we are talking about (XML or XSD?) The file extension is .xsd The root element is <schema> The XSD starts like this:  <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.rg/2001/XMLSchema">
  • 28. <?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> <xsd:element name="Book"> <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> </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> BookStore.xsd xsd = Xml-Schema Definition
  • 29. <?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> <xsd:element name="Book"> <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> </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> <!ELEMENT Title (#PCDATA)> <!ELEMENT Author (#PCDATA)> <!ELEMENT Date (#PCDATA)> <!ELEMENT ISBN (#PCDATA)> <!ELEMENT Publisher (#PCDATA)> <!ELEMENT Book (Title, Author, Date, ISBN, Publisher)> <!ELEMENT BookStore (Book+)>
  • 30. <?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> <xsd:element name="Book"> <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> </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> All XML Schemas have "schema" as the root element.
  • 31. <?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> <xsd:element name="Book"> <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> </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> The elements and datatypes that are used to construct schemas - schema - element - complexType - sequence - string come from the http://…/XMLSchema namespace
  • 33. <?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> <xsd:element name="Book"> <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> </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> Says that the elements defined by this schema - BookStore - Book - Title - Author - Date - ISBN - Publisher are to go in this namespace
  • 35. <?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> <xsd:element name="Book"> <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> </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> This is referencing a Book element declaration. The Book is in which namespace? Since there is no namespace qualifier it is referencing the Book element in the default namespace, which is the targetNamespace! Thus, this is a reference to the Book element declaration in this schema. The default namespace is http://www.books.org which is the targetNamespace!
  • 36. <?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> <xsd:element name="Book"> <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> </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> This is a directive to any instance documents which conform to this schema: Any elements that are defined in this schema must be namespace-qualified when used in instance documents.
  • 37. Other Attributes Attribute Description id Optional. Creates an unique ID for the element. attributeFormDefault Optional. The form for attributes declared in target name space of the schema. Must be either “qualified” or “unqualified” elementFormDefault Optional. The form for elements declared in the target name space of the schema. Must either be “qualified” or “unqualified” blockDefault Optional. Specifies the default value of the block attribute on element and complexType elements in namespace. finalDefault Optional. Specifies the default value of the final attribute on element, simpleType, and complexType elements in the namespace. targetNamespace Optional. An URI ref to the namespace of this schema. version Optional. Specifies the version of the schema. xmlns A URI ref specifying one or more namespaces for the schema. any attributes Optional. Specifies other attributes with non-schema namespace.
  • 38. Referencing a schema in an XML instance document <?xml version="1.0"?> <BookStore xmlns ="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.books.org BookStore.xsd"> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>July, 1998</Date> <ISBN>94303-12021-43892</ISBN> <Publisher>McMillan Publishing</Publisher> </Book> ... </BookStore> 1. First, using a default namespace declaration, tell the schema-validator 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 XML Schema-instance namespace. 1 2 3
  • 40. Referencing a schema in an XML instance document BookStore.xml BookStore.xsd targetNamespace="http://www.books.org" schemaLocation="http://www.books.org BookStore.xsd" - defines elements in namespace http://www.books.org - uses elements from namespace http://www.books.org A schema defines a new vocabulary. Instance documents use that new vocabulary.
  • 41. A Reference to a DTD - Comparison <?xml version="1.0"?> <!DOCTYPE note SYSTEM "http://www.aimit.com/dtd/note.dtd"> <note> <to>Tom</to> <from>Jerry</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
  • 42. A Reference to an XML Schema … <?xml version="1.0"?> <note xmlns="http://www.aimit.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.aimit.com note.xsd"> <to>Tom</to> <from>Jerry</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
  • 43. A Reference to an XML Schema xmlns="http://www.aimit.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.aimit.com" namespace. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  XML Schema Instance namespace  The XML Schema Instance reference is required xsi:schemaLocation="http://www.aimit.com note.xsd"  this schemaLocation 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
  • 44. Cross-Referencing XML with Schemas File “Person.xml” File “Person.xsd” <?xml version="1.0" encoding="UTF-8"?> <Person xmlns:xsi="http://www.w3.org/ 2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation=" Person.xsd"> <First>Katrina</First> <Last>Khaif</Last> <Age>24</Age> </Person> <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/ XMLSchema"> <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="First“ type="xs:string"/> <xs:element name="Middle“ type="xs:string“ minOccurs="0"/> <xs:element name="Last“ type="xs:string"/> <xs:element name="Age“ type="xs:integer"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
  • 45. Qualify XMLSchema, Default targetNamespace In the first example, we explicitly qualified all elements from the XML Schema namespace. The targetNamespace was the default namespace. BookStore Book Title Author Date ISBN Publisher http://www.books.org (targetNamespace) http://www.w3.org/2001/XMLSchema element complexType schema sequence string integer boolean
  • 46. Default XMLSchema, Qualify targetNamespace • Alternatively (equivalently), we can design our schema so that XMLSchema is the default namespace. BookStore Book Title Author Date ISBN Publisher http://www.books.org (targetNamespace) http://www.w3.org/2001/XMLSchema element complexType schema sequence string integer boolean
  • 47. <?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> </element> <element name="Book"> <complexType> <sequence> <element ref="bk:Title"/> <element ref="bk:Author"/> <element ref="bk:Date"/> <element ref="bk:ISBN"/> <element ref="bk:Publisher"/> </sequence> </complexType> </element> <element name="Title" type="string"/> <element name="Author" type="string"/> <element name="Date" type="string"/> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/> </schema> Note that http://…/XMLSchema is the default namespace. Consequently, there are no namespace qualifiers on - schema - element - complexType - sequence - string
  • 48. <?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> </element> <element name="Book"> <complexType> <sequence> <element ref="bk:Title"/> <element ref="bk:Author"/> <element ref="bk:Date"/> <element ref="bk:ISBN"/> <element ref="bk:Publisher"/> </sequence> </complexType> </element> <element name="Title" type="string"/> <element name="Author" type="string"/> <element name="Date" type="string"/> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/> </schema> Here we are referencing a Book element. Where is that Book element defined? In which namespace? The bk: prefix indicates what namespace this element is in. bk: has been set to be the same as the targetNamespace.
  • 49. "bk:" References the targetNamespace BookStore Book Title Author Date ISBN Publisher http://www.books.org (targetNamespace) http://www.w3.org/2001/XMLSchema bk element complexType schema sequence string integer boolean Consequently, bk:Book refers to the Book element in the targetNamespace.
  • 50. XML Abstract Data Model The XML Abstract Data Model  composes of Schema Components.  is used to describe XML Schemas. Schema Component  is the generic term for the building blocks that compose the abstract data model of the schema.
  • 51. 13 Kinds of Schema Components Simple type definitions Complex type definitions Attribute declarations Element declarations Attribute group definitions Identity-constraint definitions Model group definitions Notation declarations  Annotations  Model groups  Particles  Wildcards  Attribute Uses Secondary Primary Helper
  • 52. Relationships among Schema Components Identity- constraint Definitions Element Declaration Attribute Declaration Notation Declaration Simple Type Definition Complex Type Definition Particle WildCard Model Group AttributeUse Attribute group definition Model group definition Information Item type Conform to type type use type constrain reference reference
  • 53. Primary Schema Components XML Namespaces Element Declarations Attribute Declarations Simple Type Definitions Complex Type Definitions
  • 54. XML Schema (.xsd) XML document & XML Schema Information Items … Elements Attributes XML Document (.xml)
  • 55. Declaration & Definition Declaration Components  are associated by (qualified) names to information items being validated.  It is like declaring objects in OOP. Definition Components  define internal schema components that can be used in other schema components.  Type definition is like defining classes in OOP.
  • 56. Definitions vs. Declarations A definition describes a complex or simple type that either contains element or attribute declarations or references element or attribute declarations defined elsewhere in the document. Here’s a definition: <xsd:complexType name=”bookType”> <xsd:sequence> <xsd:element name=”title” type=”xsd:string”/> <xsd:element name=”author” type=”xsd:string”/> <xsd:element name=”description” type=”xsd:string”/> </xsd:sequence> </xsd:complexType> A declaration defines an element or attribute, specifying the name and datatype for the component. Here’s a declaration: <xsd:element name=”book”> <xsd:complexType> <xsd:sequence> <xsd:element name=”title” type=”xsd:string”/> <xsd:element name=”author” type=”xsd:string”/> <xsd:element name=”description” type=”xsd:string”/> </xsd:sequence> </xsd:complexType> </xsd:element>
  • 57. Consider XML Instance Document Example <book isbn="0-7356-1465-2"> <title> XML Step by Step</title> <author> Michael young</author> <publisher> Microsoft press</publisher> </book>
  • 58. Examples <xs:element name="book"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> … … </xs:sequence> <xs:attribute name="isbn" type="xs:string"/> </xs:complexType> </xs:element> <xs:complexType name="bookType"> <xs:sequence> <xs:element name="title" type="nameType"/> … … </xs:sequence> <xs:attribute name="isbn" type="isbnType" use="required"/> </xs:complexType> Declaration Type Definition <xs:element name="book" type="bookType"/> <book isbn="0-7356-1465-2"> <title> XML Step by Step </title> …… </book>
  • 59. Declaration vs. Definition Declarations  Enable elements and attributes with specific names and types (both simple and complex) to appear in document instances. <xsd:element name="purchaseOrder" type="PurchaseOrderType"/> <xsd:element name="comment" type="xsd:string"/> Definitions  create new types (both simple and complex) <xsd:complexType name="PurchaseOrderType"> <xsd:sequence> <xsd:element name="shipTo" type="Address"/> <xsd:element name="billTo" type="Address"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="items" type="Items"/> </xsd:sequence> <xsd:attribute name="orderDate" type="xsd:date"/> </xsd:complexType>
  • 60. Type Definitions Why Type Definitions? Simple Type Definition VS. Complex Type Definition Attributes & Elements without element children Complex Type Definition Simple Type Definition Elements
  • 61. “Simple” and “Complex” elements A “simple” element is one that contains text and nothing else  A simple element cannot contain other elements  A simple element cannot be empty  However, the text can be of many different types, and may have various restrictions applied to it If an element isn’t simple, it’s “complex”  A complex element may have attributes  A complex element may be empty, or it may contain text, other elements, or both text and other elements
  • 62. Defining a simple element A simple element is defined as <xs:element name="name" type="type" /> where:  name is the name of the element  the most common values for type are xs:boolean xs:integer xs:date xs:string xs:decimal xs:time Other attributes a simple element may have:  default="default value" if no other value is specified  fixed="value" no other value may be specified
  • 63. Examples of Simple element using Built-in Simple Types <xsd:element name=“weight” type=“xsd:string”/> <xsd:element name=“population” type=“xsd:integer” />
  • 64. Simple Elements Example:  <lastname>Amir Khan</lastname>  <age>35</age>  <dateborn>1975-09-15</dateborn>  Corresponding simple element definition:  <xs:element name="lastname" type="xs:string"/>  <xs:element name="age" type="xs:integer"/>  <xs:element name="dateborn" type="xs:date"/>
  • 65. Declare Default and Fixed Values for Simple Elements Simple elements can have a default value OR a fixed value set. A default value is automatically assigned to the element when no other value is specified. In the following example the default value is "red":  <xs:element name="color" type="xs:string" default="red"/> A fixed value is also automatically assigned to the element. You cannot specify another value. In the following example the fixed value is "red":  <xs:element name="color" type="xs:string" fixed="red"/>
  • 66. Primitive Type XML Schema has a complex system of types. Different types may describe: 1. the allowed values of attributes, 2. the allowed content of elements, or 3. the allowed content and the allowed attributes of elements. There is a subset of types, called the simple types, that can be used in either of the first two roles.
  • 67. Simple Types Built-in simple type Description Example(s) xsd:string A sequence of any of the legal XML characters This is a string. xsd:boolean The value true or false, or 1 or 0 (indicating true or false, respectively) true false 1 0 xsd:decimal A number that may contain a decimal component -5.2 -3.0 1 2.5 xsd:integer A whole number -389 -7 0 5 229
  • 68. Simple Types Built-in simple type Description Example(s) xsd:positivelnteger A positive whole number(not including 0) 5 229 xsd:negativelnteger A negative whole number (not including 0) -389 -7 xsd:date A calendar date, represented as CCYY-MM-DD 1948-05-21 2001-10-15
  • 69. Simple Types Built-in simple type Description Example(s) xsd:time A time of day, represented as hh:mm:ss.ss 11:30:00.00(11:30 A.M.) 14:29:03 (2:29 P.M. and 3 seconds) 05:16:00.0(5:16 A.M.) xsd:dateTime A date and time of day, represented as CCYY-MM-DDThh:mm:ss.ss 1948-05-21T17:28:00.00 xsd:gMonth A Gregorian calendar month, represented as -MM- -05- (May) -12- (December) xsd:gYear A Gregorian calendar year, represented as CCYY 1948 2001
  • 70. Simple Types Built-in simple type Description Example(s) xsd:gDay A day of a Gregorian calendar month, represented as -DD -05 -31 xsd:gYearMonth A Gregorian calendar year and month, represented as CCYY-MM 1948-05 (May, 1948) xsd:anyURI A URI (Uniform Resource Identifier). http://www.myOnline.com
  • 71. Examples of Built-in Simple Types <element name=“Title” type=“string”/> <element name=“Heading” type=“string”/> <element name=“Topic” type=“string”/> <element name=“Price” type=“decimal”/>
  • 72. XSD Complex Elements A complex element contains other elements and/or attributes. ● empty elements ● elements that contain only other elements ● elements that contain only text ● elements that contain both other elements and text <employee> <firstname>Mani</firstname> <lastname>Dsouza</lastname> </employee>
  • 73. Example of complex elements A complex XML element, "product", which is empty: <product pid="1345"/> A complex XML element, "employee", which contains only other elements: <employee> <firstname>Salman</firstname> <lastname>Khan</lastname> </employee> A complex XML element, "food", which contains only text: <food type=“Vanilla">Ice cream</food> A complex XML element, "description", which contains both elements and text: <description> It happened on <date lang="norwegian">03.03.99</date> .... </description
  • 74. How to Define a Complex Element Complex XML element: <employee> <firstname>Mani</firstname> <lastname>Dsouza</lastname> </employee> XSD: <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>
  • 75. Complex Element Example 1 <xsd:element name="Address" > <xsd:complexType > <xsd:sequence> <xsd:element name="name" type="xsd:string" /> <xsd:element name="street" type="xsd:string" /> <xsd:element name="city" type="xsd:string" /> <xsd:element name="state" type="xsd:string" /> <xsd:element name="zip" type="xsd:decimal" /> </xsd:sequence> <xsd:attribute name="country" type="xsd:NMTOKEN" use="fixed" value="IN"/> </xsd:complexType> <xsd:element>
  • 76. Elements can be declared in two ways <xsd:element name="name" type="type" minOccurs="int" maxOccurs="int"/> A simple type (e.g., xsd:string) or the name of a complexType (e.g., BookPublication) <xsd:element name="name" minOccurs="int" maxOccurs="int"> <xsd:complexType> … </xsd:complexType> </xsd:element> 1 2 A nonnegative integer A nonnegative integer or "unbounded" Note: minOccurs and maxOccurs can only be used in nested (local) element declarations.
  • 77. Note that: <xsd:element name="A" type="xyz"/> <xsd:complexType name="xyz"> <xsd:sequence> <xsd:element name="B" …/> <xsd:element name="C" …/> </xsd:sequence> </xsd:complexType> is equivalent to: <xsd:element name="A"> <xsd:complexType> <xsd:sequence> <xsd:element name="B" …/> <xsd:element name="C" …/> </xsd:sequence> </xsd:complexType> </xsd:element> Element A references the complexType xyz. Element A has the complexType definition inlined in the element declaration.
  • 78. Type Attribute or complexType Child Element, but not Both! An element declaration can have a type attribute, or a complexType child element, but it cannot have both a type attribute and a complexType child element. <xsd:element name="A" type="xyz"> <xsd:complexType> … </xsd:complexType> </xsd:element>
  • 79. Defining an attribute Attributes are always declared as simple types An attribute is defined as <xs:attribute name="name" type="type" /> where:  name and type are the same as for xs:element Other attributes a simple element may have:  default="default value" if no other value is specified  fixed="value" no other value may be specified
  • 80. Primary Schema Components <xsd:attribute name=“size” type=“xsd:integer” default=“12”> Attribute: <xsd:attribute ref=“size” default=“12” use=“optional”>
  • 81. Example: Attribute declaration <element name=“StdRecord”> <complexType> <sequence> <element name=“SName” type=“string”/> <element name=“Course” type=“string”/> <element name=“Gender” type=“token”/> <element name=“DateOfBirth” type=“date”/> </sequence> <attribute name=“RegNo” type=“integer”/> </complexType> </element> Instance Document <Record RegNo=“420”> <SName>Shakti Kapoor</SName> <Course>MS</Course> <Gender>M</Gender> <DateOfBirth>1963-06-01</DateOfBirth> </Record>
  • 82. Attributes Simple elements cannot have attributes. If an element has attributes, it is considered to be of complex type. The attribute itself is always declared as a simple type.. Example:  An XML element with an attribute  <lastname lang="EN">Khan</lastname>  A corresponding simple attribute definition:  <xs:attribute name="lang" type="xs:string"/>
  • 83. Declare Default and Fixed Values for Attributes Attributes can have a default value OR a fixed value specified A default value is automatically assigned to the attribute when no other value is specified. In the following example the default value is "EN":  <xs:attribute name="lang" type="xs:string" default="EN"/> A fixed value is also automatically assigned to the attribute. You cannot specify another value. In the following example the fixed value is "EN":  <xs:attribute name="lang" type="xs:string" fixed="EN"/>
  • 84. XML Schema Types Simple types  Basic datatypes  Can be used for attributes and element text  Extendable Complex types  Defines structure of elements  Extendable Types can be named or “anonymous”
  • 85. The Simple Type: <simpleType> The Simplest Type Declaration:  <simpleType name = “FirstName” type = “string”/> Based on a primitive or the derived built-in datatypes Cannot contain sub-elements or attributes Can declare constraining properties (“facets”)  minLength, maxLength, Length, etc May be used as base type of a complexType
  • 86. Simple Types Simple Types are of three varieties:  Atomic: Built-in or derived, e.g. <xsd:simpleType name="myInteger"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="10000"/> <xsd:maxInclusive value="99999"/> </xsd:restriction> </xsd:simpleType>  List: multiple items of the same type <listOfMyInt>20003 15037 95977 95945</listOfMyInt>  Union: Union or two or more Simple Types
  • 87. XML Schema Datatypes Two kinds of datatypes: Built-in and User-defined Built-in  Primitive Datatypes  string, double, duration, etc  Derived Datatypes:  CDATA, integer, date, byte, etc  Derived from the primitive types  Example: integer is derived from decimal User-defined  Derived from built-in or other user-defined datatypes  specify constraints on an existing type (the base type)  Constraints are given in terms of facets  totalDigits, maxInclusive, etc.
  • 88. XML Schema built-in Datatypes
  • 89. Built-in Datatypes Primitive Datatypes  string  boolean  decimal  float  double  duration  dateTime  time  date  gYearMonth  gYear  gMonthDay Atomic, built-in  "Hello World"  {true, false, 1, 0}  7.08  12.56E3, 12, 12560, 0, -0, INF, -INF, NAN  12.56E3, 12, 12560, 0, -0, INF, -INF, NAN  P1Y2M3DT10H30M12.3S  format: CCYY-MM-DDThh:mm:ss  format: hh:mm:ss.sss  format: CCYY-MM-DD  format: CCYY-MM  format: CCYY  format: --MM-DD Note: 'T' is the date/time separator INF = infinity NAN = not-a-number
  • 90. Date and Time Data Types Code.xsd <xsd:element name=“gestation” type=“xsd:timeDuration”/> (represent a certain amount of time) Code.xml <gestation>P3M15D</gestation> (PnYnMnDTnHnMnS) Period n - non negative T begins optional time section
  • 92. Built-in Datatypes (cont.) Primitive Datatypes  gDay  gMonth  hexBinary  base64Binary  anyURI  QName  NOTATION Atomic, built-in  format: ---DD  format: --MM  a hex string  a base64 string  http://www.aimit.ac.in  a namespace qualified name  a NOTATION from the XML spec
  • 93. Built-in Datatypes (cont.) • Derived types – normalizedString – token – language – IDREFS – ENTITIES – NMTOKEN – NMTOKENS – Name – NCName – ID – IDREF – ENTITY – integer – nonPositiveInteger • Subtype of primitive datatype – A string without tabs, line feeds, or carriage returns – String w/o tabs, l/f, leading/trailing spaces, consecutive spaces – any valid xml:lang value, e.g., EN, FR, ... – must be used only with attributes – must be used only with attributes – must be used only with attributes – must be used only with attributes – part (no namespace qualifier) – must be used only with attributes – mmust be used only with attributes – ust be used only with attributes – 456 – negative infinity to 0
  • 94. Built-in Datatypes (cont.) • Derived types – negativeInteger – long – int – short – byte – nonNegativeInteger – unsignedLong – unsignedInt – unsignedShort – unsignedByte – positiveInteger • Subtype of primitive datatype – negative infinity to -1 – -9223372036854775808 to 9223372036854775807 – -2147483648 to 2147483647 – -32768 to 32767 – -127 to 128 – 0 to infinity – 0 to 18446744073709551615 – 0 to 4294967295 – 0 to 65535 – 0 to 255 – 1 to infinity Note: the following types can only be used with attributes : ID, IDREF, IDREFS, NMTOKEN, NMTOKENS, ENTITY, and ENTITIES.
  • 95. Restrictions Restrictions are used to define acceptable values for XML elements or attributes. When used with XML elements they are known as “facets”.
  • 96. Creating your own Datatypes A new datatype can be defined from an existing datatype (called the "base" type) by specifying values for one or more of the optional facets for the base type.
  • 97. General Form of Creating a New Datatype by Specifying Facet Values <xsd:simpleType name= "name"> <xsd:restriction base= "xsd:source"> <xsd:facet value= "value"/> <xsd:facet value= "value"/> … </xsd:restriction> </xsd:simpleType> Facets: - length - minlength - maxlength - pattern - enumeration - minInclusive - maxInclusive - minExclusive - maxExclusive ... Sources: - string - boolean - number - float - double - duration - dateTime - time ...
  • 98. Restrictions Constraint Description enumeration Defines a list of acceptable values. fractionDigits Specifies the maximum number of deicmal places allowed. Must be equal to or greater than zero. length Specifies the exact number of characters or list items allowed. Must be absolute. maxExclusive Specifies the upper bounds for numeric values. maxInclusive Specifies the upper bounds for numeric values. maxLength Specifies the maximum number of characters or list items allowed. Must be absolute. minExclusive Specifies the lower bounds for numeric values. minInclusive Specifies the lower bounds for numeric values. minLength Specifies the minimum number of characters or list items allowed. Must be absolute.
  • 99. Restrcitions Constraint Description pattern Defines the exact sequence of characters that are acceptable. totalDigits Specifies the exact number of digits allowed. Must be greater than zero. whiteSpace Specifies how white space is handled.
  • 100. Example of Creating a New Datatype by Specifying Facet Values <xsd:simpleType name="TelephoneNumber"> <xsd:restriction base="xsd:string"> <xsd:length value=“15/> <xsd:pattern value="d{4}-d{10}"/> </xsd:restriction> </xsd:simpleType> 1. This creates a new datatype called 'TelephoneNumber'. 2. Elements of this type can hold string values, 3. But the string length must be exactly 15 characters long and 4. The string must follow the pattern: dddd-dddddddddd, where 'd' represents a 'digit'. (Obviously, in this example the regular expression makes the length facet redundant.) 1 2 3 4
  • 101. Creating your own Datatypes The string primitive datatype has six optional facets:  length  minLength  maxLength  pattern  enumeration  whitespace (legal values: preserve, replace, collapse)
  • 102. Length The facets length, minLength, maxLength allow to constrain the length of an item like a string (also allow to constrain the number of items in a list type). Values of length, minLength, minLength should be non-negative integers. Example: <xsd:simpleType name="state"> <xsd:restriction base="xsd:string"> <xsd:length value="2"/> </xsd:restriction> </xsd:simpleType> defines a type state representing strings containing exactly two characters.  These facets supported by all primitive types other than numeric and date- and time-related types. Also supported by list types.
  • 103. White Space This facet controls how white space in a value received from the parser is processed, prior to Schema validation. It can take three values:  preserve: no white space processing, beyond what base XML does.  replace: Convert every white space character (Line Feed, etc) to a space character (#x20).  collapse: Like replace. All leading or trailing spaces are then removed. Also sequences of spaces are replaced by single spaces.  Note analogies to “Normalization of Attribute Values” in base XML. All simple types except union types have this attribute, but usually you don’t explicitly set it in restriction: just inherit values from built in types. All built in types have collapse, except string which has preserve and normalizedString which has replace.
  • 104. Pattern Perhaps the most powerful facet is pattern, which allows to specify a regular expression: any allowed value must satisfy the pattern of this expression. Example <xsd:simpleType name="weekday"> <xsd:restriction base="xsd:string"> <xsd:pattern value="(Mon|Tues|Wednes|Thurs|Fri)day"/> </xsd:restriction> </xsd:simpleType>  defines a type weekday representing the names of the week days.
  • 105. Regular Expressions XML Schema has its own notation for regular expressions, but very much based on the corresponding Perl notation. For the most part Schema use a subset of the Perl 5 grammar for regular expressions.  Includes most of the purely “declarative” features from Perl regular expressions, but omits many “procedural” features related to search, matching algorithm, substitution, etc.
  • 106. Metacharacters The following characters, called metacharacters, have special roles in Schema regular expressions:  . ? * + | { } ( ) [ ] To match these characters literally in patterns, must escape them with , e.g.:  The pattern “2+2” matches the string “2+2”.  The pattern “f(x)” matches the string “f(x)”.
  • 107. Escape Sequences n r t | - ^ ? * + { } ( ) [ ] linefeed carriage return tab The backward slash The vertical bar | The hyphen - The caret ^ The question mark ? The asterisk * The plus sign + The open curly brace { The close curly brace } The open paren ( The close paren ) The open square bracket [ The close square bracket ]
  • 108. Escape Sequences In general one should use XML character references to include hard-to-type characters. But for convenience Schema regular expressions allow:  n matches a newline character (same as &#xA;)  r matches a carriage return character (same as &#xD;)  t matches a tab character (same as &#x9;)
  • 109. Escape Sequences All other escape sequences (except - and ^, used only in character class expressions) match any single character out of some set of possible values.  For example d matches any decimal digit, so the pattern “Boeing ddd” matches the strings “Boeing 747”, “Boeing 777”, etc.
  • 110. Multicharacter Escapes The simplest patterns matching classes of characters are:  . matches any character except carriage return or newline.  d matches any decimal digit.  s matches any white space character.  i matches any character that can start an XML name.  c matches any character that can appear in an XML name.  w matches any “word” character (excludes punctuation, etc.)
  • 111. Multicharacter Escapes The escapes D, S, I, C and W are negative forms, e.g. D matches any character except a decimal digit.
  • 112. Category Escapes A large and interesting family of escapes is based on the Unicode standard. General form is p{Name} where Name is a Unicode-defined class name.  The negative form P{Name} matches any character not in the class. Simple examples include: p{L} (any letter), p{Lu} (upper case letters), p{Ll} (lower case letters), etc. More interesting cases are based on the Unicode block names for alphabets, e.g.:  p{IsBasicLatin}, p{IsLatin-1Supplement}, p{IsGreek}, p{IsArabic}, p{IsDevanagari}, p{IsHangulJamo}, p{IsCJKUnifiedIdeographs}, etc, etc, etc.
  • 113. Category Escapes p{L} p{Lu} p{Ll} p{N} p{Nd} p{P} p{Sc} A letter, from any language An uppercase letter, from any language A lowercase letter, from any language A number - Roman, fractions, etc A digit from any language A punctuation symbol A currency sign, from any language
  • 114. Character Class Expressions Allow you to define terms that match any character from a custom set of characters. Basic syntax is familiar from Perl and UNIX: [List-of-characters] or the negative form: [^List-of-characters] Here List-of-characters can include individual characters, and also ranges of the form First- Last where First and Last are characters.
  • 115. Character Class Expressions Examples:  [RGB] matches one of R, G, or B.  [0-9A-F] or [dA-F] match one of 0, 1, …, 9, A, B,…, F.  [^rn] matches anything except CR, NL (same as . ).
  • 116. Restriction Example - Series <xs:element name=“char"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value=“[a-z]"/> </xs:restriction> </xs:simpleType> </xs:element>
  • 117. Restriction Example - Series <xs:element name=“choice"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value=“nyNY"/> </xs:restriction> </xs:simpleType> </xs:element>
  • 118. Class Subtractions A feature of XML Schema, not present in Perl 5. A class character expression can take the form: [List-of-characters-Class-char-expr] or: [^List-of-characters-Class-char-expr] where Class-char-expr is another class character expression. Example:  [a-zA-Z-[aeiouAEIOU]] matches any consonant in the Latin alphabet.
  • 119. Sequences and Alternatives Finally, the universal core of regular expressions. If Pattern1 and Pattern2 are regular expressions, then:  Pattern1Pattern2 matches any string made by putting a string accepted by Pattern1 in front of a string accepted by Pattern2.  Pattern1|Pattern2 matches any string that would be accepted by Pattern1, or any string accepted by Pattern2. Parentheses just group things together:  (Pattern1) matches any string accepted by Pattern1.
  • 120. Sequences and Alternatives An example given earlier:  (Mon|Tues|Wednes|Thurs|Fri)day matches any of the strings Monday, Tuesday, Wednesday, Thursday, or Friday.  Equivalent to Monday|Tuesday|Wednesday|Thursday|Friday.
  • 121. Restriction Example - Series <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>
  • 122. Restriction Example - Series <xs:element name=“areacode"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:pattern value=“[0-9][0-9][0-9]"/> </xs:restriction> </xs:simpleType> </xs:element>
  • 123. Quantifiers If Pattern1 is a regular expression:  Pattern1? matches the empty string or any string accepted by Pattern1.  Pattern1+ matches any string accepted by Pattern1, or by Pattern1Pattern1, or by Pattern1Pattern1Pattern1, or …  Pattern1* matches the empty string or any string accepted by Pattern1+.
  • 124. Regular Expressions Regular Expression Chapter d Chapter&#x020;d Chaptersd a*b [xyz]b a?b a+b [a-c]x [-ac]x [ac-]x [^0-9]x Dx Example Chapter 1 Chapter 1 Chapter followed by a blank followed by a digit b, ab, aab, aaab, … xb, yb, zb b, ab ab, aab, aaab, … ax, bx, cx -x, ax, cx ax, cx, -x any non-digit char followed by x any non-digit char followed by x
  • 125. Quantifiers If n, m are numbers, XML Schema also allow the shorthand forms:  Pattern1{n} is equivalent to Pattern1 repeated n times.  Pattern1{m,n} matches any string accepted by Pattern1 repeated m times or m + 1 times or … or n times.  Pattern1{m,} matches any string accepted by Pattern1 repeated m or more times.
  • 126. Regular Expressions (cont.) Regular Expression  (ho){2} there  (hos){2} there  .abc  (a|b)+x  a{1,3}x  a{2,}x  wsw Example  hoho there  ho ho there  any (one) char followed by abc  ax, bx, aax, bbx, abx, bax,...  ax, aax, aaax  aax, aaax, aaaax, …  word character (alphanumeric plus dash) followed by a space followed by a word character
  • 127. Regular Expressions (cont.) [a-zA-Z-[Ol]]* • A string comprised of any lower and upper case letters, except "O" and "l" . • The period "." (Without the backward slash the period means "any character")
  • 128. Example of Derived Simple Type (Regular expression) <xsd:simpleType name=“ProductKey"> <xsd:restriction base="xsd:string"> <xsd:pattern value="d{3}-[A-Z]{2}"/> </xsd:restriction> </xsd:simpleType>
  • 129. Restrictions – Password Example <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> This element must contain a literal string composed of upper or lower case letters, or numerals. It must have 8 characters in total.
  • 130. Using Patterns in Restriction All simple types (including lists and enumerations) support the pattern facet, e.g.: <simpleType name=“multiplesOfFive"> <restriction base="xs:integer"> <pattern value=“[+-]?d*[05]"/> </restriction> </simpleType> defines a subtype of integer including all numbers ending with digits 0 or 5.
  • 131. Example R.E. [1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5] 0 to 99 100 to 199 200 to 249 250 to 255 This regular expression restricts a string to have values between 0 and 255. … Such a R.E. might be useful in describing an IP address ...
  • 132. IP Datatype Definition <xsd:simpleType name="IP"> <xsd:restriction base="xsd:string"> <xsd:pattern value="(([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).){3} ([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"> <xsd:annotation> <xsd:documentation> Datatype for representing IP addresses. Examples, 129.83.64.255, 64.128.2.71, etc. This datatype restricts each field of the IP address to have a value between zero and 255, i.e., [0-255].[0-255].[0-255].[0-255] Note: in the value attribute (above) the regular expression has been split over two lines. This is for readability purposes only. In practice the R.E. would all be on one line. </xsd:documentation> </xsd:annotation> </xsd:pattern> </xsd:restriction> </xsd:simpleType>
  • 133. Regular Expressions (concluded) p{L} p{Lu} p{Ll} p{N} p{Nd} p{P} p{Sc} A letter, from any language An uppercase letter, from any language A lowercase letter, from any language A number - Roman, fractions, etc A digit from any language A punctuation symbol A currency sign, from any language <xsd:simpleType name="money"> <xsd:restriction base="xsd:string"> <xsd:pattern value="p{Sc}p{Nd}+(.p{Nd}p{Nd})?"/> </xsd:restriction> </xsd:simpleType> <xsd:element name="cost" type="money"/> "currency sign from any language, followed by one or more digits from any language, optionally followed by a period and two digits from any language" <cost>$45.99</cost> <cost>¥300</cost>
  • 134. Enumeration The enumeration facet allows one to select a finite subset of allowed values from a base type, e.g.: <xsd:simpleType name="weekday"> <xsd:restriction base="xs:string"> <xsd:enumeration value="Monday"/> <xsd:enumeration value="Tuesday"/> <xsd:enumeration value="Wednesday"/> <xsd:enumeration value="Thursday"/> <xsd:enumeration value="Friday"/> </xsd:restriction> </xsd:simpleType>  Behaves like a very restricted version of pattern?  All primitive types except boolean support the enumeration facet. List and union types also support this facet.
  • 135. Example of Derived Simple Type 3 (Enumeration) <xsd:simpleType name="State"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="AP"/> <xsd:enumeration value=“KA"/> <xsd:enumeration value="TN/> <!-- and so on ... --> </xsd:restriction> </xsd:simpleType> enumeration facet limits a simple type to a set of distinct values
  • 136. Restriction Example - Set <xs:element name=“car"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value=“Maruthi“ /> <xs:enumeration value=“Fiat“/> <xs:enumeration value=“Ford“ /> <xs:enumeration value=“Tata“ /> <xs:enumeration value=“Santro” /> </xs:restriction> </xs:simpleType> </xs:element>
  • 137. Enumeration An enumeration restricts the value to be one of a fixed set of values Example:  <xs:element name="season"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Spring"/> <xs:enumeration value="Summer"/> <xs:enumeration value="Autumn"/> <xs:enumeration value="Fall"/> <xs:enumeration value="Winter"/> </xs:restriction> </xs:simpleType> </xs:element>
  • 138. Another Example <xsd:simpleType name="shape"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="circle"/> <xsd:enumeration value="triangle"/> <xsd:enumeration value="square"/> </xsd:restriction> </xsd:simpleType> This creates a new type called shape. An element declared to be of this type must have either the value circle, or triangle, or square.
  • 139. Enumeration - Example <xsd:simpleType name=“GenderType”> <xsd:restriction base=“token”> <xsd:enumeration value=“M”/> <xsd:enumeration value=“F”/> <xsd:enumeration value=“NK”/> </xsd:restriction> </xsd:simpleType> <xsd:element name=“Gender” type=“GenderType”/> <Gender>NK</Gender> <Gender>Male</Gender>
  • 140. Using Patterns in Restriction The pattern facet can appear more than once in a single restriction: interpretation is as if patterns were combined with |.  Conversely if the pattern facet is specified in restriction of a base type that was itself defined using a pattern, allowed values must satisfy both patterns.
  • 141. Multiple Facets - "and" them together, or "or" them together? An element declared to be of type TelephoneNumber must be a string of length=8 and the string must follow the pattern: 3 digits, dash, 4 digits. <xsd:simpleType name="shape"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="circle"/> <xsd:enumeration value="triangle"/> <xsd:enumeration value="square"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="TelephoneNumber"> <xsd:restriction base="xsd:string"> <xsd:length value="8"/> <xsd:pattern value="d{3}-d{4}"/> </xsd:restriction> </xsd:simpleType> An element declared to be of type shape must be a string with a value of either circle, or triangle, or square. Patterns, enumerations => "or" them together All other facets => "and" them together
  • 142. Numerical - Facets The facets maxInclusive, maxExclusive, minExclusive, minInclusive are supported only by numeric and date- and time-related types, and define bounds of value ranges. The facets totalDigits, fractionDigits are defined for the primitive type decimal, and thus all numeric types derived from decimal, and for no other types.
  • 143. Facets of the integer Datatype The integer datatype has 8 optional facets:  totalDigits  pattern  whitespace  enumeration  maxInclusive  maxExclusive  minInclusive  minExclusive
  • 144. Restrictions on numbers minInclusive -- number must be ≥ the given value minExclusive -- number must be > the given value maxInclusive -- number must be ≤ the given value maxExclusive -- number must be < the given value totalDigits -- number must have exactly value digits fractionDigits -- number must have no more than value digits after the decimal point
  • 145. A Simple Type Example (1 of 4) Integer with value (1234, 5678] <xsd:simpleType name=‘MyInteger’> <xsd:restriction base=‘xsd:integer’> <xsd:minExclusive value=‘1234’/> <xsd:maxInclusive value=‘5678’/> </xsd:restriction> </xsd:simpleType>
  • 146. A Simple Type Example (2 of 4) Integer with value [1234, 5678) <xsd:simpleType name=‘MyInteger’> <xsd:restriction base=‘xsd:integer’> <xsd:minInclusive value=‘1234’/> <xsd:maxExclusive value=‘5678’/> </xsd:restriction> </xsd:simpleType>
  • 147. A Simple Type Example (3 of 4) Integer with value [1234, 5678] <xsd:simpleType name=‘MyInteger’> <xsd:restriction base=‘xsd:integer’> <xsd:minInclusive value=‘1234’/> <xsd:maxInclusive value=‘5678’/> </xsd:restriction> </xsd:simpleType>
  • 148. A Simple Type Example (4 of 4) Validating integer with value (1234, 5678] <data xsi:type='MyInteger'></data> INVALID <data xsi:type='MyInteger'>Sandy</data> INVALID <data xsi:type='MyInteger'>-32</data> INVALID <data xsi:type='MyInteger'>1233</data> INVALID <data xsi:type='MyInteger'>1234</data> INVALID <data xsi:type='MyInteger'>1235</data> <data xsi:type='MyInteger'>5678</data> <data xsi:type='MyInteger'>5679</data> INVALID <xsd:simpleType name=‘MyInteger’> <xsd:restriction base=‘xsd:integer’> <xsd:minExclusive value=‘1234’/> <xsd:maxInclusive value=‘5678’/> </xsd:restriction> </xsd:simpleType>
  • 149. Restriction Example - Simple <xs:element name="age"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="120"/> </xs:restriction> </xs:simpleType> </xs:element>
  • 150. Simple Type Definition Example <xs:simpleType name="farenheitWaterTemp"> <xs:restriction base="xs:number"> <xs:fractionDigits value="2"/> <xs:minExclusive value="0.00"/> <xs:maxExclusive value="100.00"/> </xs:restriction> </xs:simpleType>
  • 151. Fixing a Facet Value Sometimes when we define a simpleType we may like to have a unchanging value for one (or more) facet. That is, we want to make the facet a constant. <xsd:simpleType name= "ClassSize"> <xsd:restriction base="xsd:nonNegativeInteger"> <xsd:minInclusive value="10" fixed="true"/> <xsd:maxInclusive value="60"/> </xsd:restriction> </xsd:simpleType> simpleTypes which derive from this simpleType may not change this facet.
  • 152. <xsd:simpleType name= "ClassSize"> <xsd:restriction base="xsd:nonNegativeInteger"> <xsd:minInclusive value="10" fixed="true"/> <xsd:maxInclusive value="60"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name= “MCAClassSize"> <xsd:restriction base="ClassSize"> <xsd:minInclusive value="15"/> <xsd:maxInclusive value="60"/> </xsd:restriction> </xsd:simpleType> Error! Cannot change the value of a fixed facet!
  • 153. PurchaseOrder.xsd <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:annotation> <xsd:documentation xml:lang="en"> Sample Purchase order schema </xsd:documentation> </xsd:annotation> <xsd:element name="purchaseOrder" type="PurchaseOrderType"/> <xsd:element name="comment" type="xsd:string"/> <xsd:complexType name="PurchaseOrderType"> <xsd:sequence> <xsd:element name="shipTo" type="Address"/> <xsd:element name="billTo" type="Address"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="items" type="Items"/> </xsd:sequence> <xsd:attribute name="orderDate" type="xsd:date"/> </xsd:complexType> <xsd:complexType name="Address"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="street" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> <xsd:element name="state" type="xsd:string"/> <xsd:element name="zip" type="xsd:decimal"/> </xsd:sequence> <xsd:attribute name="country" type="xsd:NMTOKEN" fixed="IN"/> </xsd:complexType> <xsd:complexType name="Items"> <xsd:sequence> <xsd:element name="item" minOccurs="0" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="productName" type="xsd:string"/> <xsd:element name="quantity"> <xsd:simpleType> <xsd:restriction base="xsd:positiveInteger"> <xsd:maxExclusive value="100"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="Price" type="xsd:decimal"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/> </xsd:sequence> <xsd:attribute name="partNum" type="SKU" use="required"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> <!-- Stock Keeping Unit, a code for identifying products --> <xsd:simpleType name="SKU"> <xsd:restriction base="xsd:string"> <xsd:pattern value="d{3}-[A-Z]{2}"/> </xsd:restriction> </xsd:simpleType> </xsd:schema>
  • 154. List Type We define a type representing a white-space- separated list of items using the list element.  Comprised of sequences of atomic simple types A list value is split according to its white space content prior to validation of the items in the list  Three built-in list types  NMTOKENS, IDREFS, ENTITIES  User defined List type  Derive from atomic types  Facets  length, minLength, maxLength, enumeration.
  • 155. Example of List Type The xsd:list element creates a new simple type that allows the element to contain a sequence of values of the base type, which are separated with white space characters (spaces, tabs, or line breaks). Schema <xsd:simpleType name="listOfMyIntType"> <xsd:list itemType="myInteger"/> </xsd:simpleType> Instance Document <listOfMyInt>20003 15037 95977 95945</listOfMyInt>
  • 156. Example: List Type with Facet list of integers where 5 is the maximum number of items allowed in the list. <xs:simpleType name='derivedlistOfIntegers'> <xs:restriction base='listOfIntegers'> <xs:maxLength value='5'> </xs:restriction> </xs:simpleType>  Usage in XML instance document <myelement listOfIntegers='1 100 9 4000 0'/>
  • 157. Example: List Type with Facet List Type for Five Colors <xsd:simpleType name="ColorList"> <xsd:list itemType="Color"/> </xsd:simpleType> <xsd:simpleType name="FiveColors"> <xsd:restriction base="ColorList"> <xsd:length value="5"/> </xsd:restriction> </xsd:simpleType> Use in XML instance document <fiveColors>white black blue red green</fiveColors>
  • 158. Example: List Type with Facet <xsd:simpleType name=“StateList"> <xsd:list itemType=“State"/> </xsd:simpleType> <xsd:simpleType name="SixStates"> <xsd:restriction base=“StateList"> <xsd:length value="6"/> </xsd:restriction> </xsd:simpleType> <element name=“SelectedStates” type=“SixStates”> Define a list of exactly six states (SelectedStates), we first define a new list type called StateList from State, and then we derive SixStates by restricting StateList to only six items < SelectedStates>KA KL GA TN AP MP</SelectedStates>
  • 159. 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
  • 160. Union Type The xsd:union element creates a new simple type that allows the element to contain a value that conforms to any one of a group of specified base types.
  • 161. Creating a simpleType that is a Union of Types simpleType 1 simpleType 2 simpleType 1 + simpleType 2 Note: you can create a union of more than just two simpleTypes
  • 162. Methods for creating Union simpleType <xsd:simpleType name="name"> <xsd:union memberTypes="space delimited simpleTypes"/> </xsd:simpleType> Alternatively, <xsd:simpleType name="name"> <xsd:union> <xsd:simpleType> … </xsd:simpleType> <xsd:simpleType> … </xsd:simpleType> … </xsd:union> </xsd:simpleType>
  • 163. Union Type for Zipcodes <xsd:simpleType name="zipUnion"> <xsd:union memberTypes=“State listOfMyIntType"/> </xsd:simpleType> <element name=zips type=“zipUnion”> <zips>KA</zips> <zips>575001 613256 715872</zips> <zips>UP</zips>
  • 164. <xs:simpleType name="toddlerCounting"> <xs:union> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="1"/> <xs:maxInclusive value="3"/> </xs:restriction> </xs:simpleType> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="many"/> </xs:restriction> </xs:simpleType> </xs:union> </xs:simpleType> Simple Types : Union
  • 165. union of two simple types. <xs:attribute name="fontsize"> <xs:simpleType> <xs:union memberTypes="fontbynumber fontbystringname" /> </xs:simpleType> </xs:attribute> <xs:simpleType name="fontbynumber"> <xs:restriction base="xs:positiveInteger"> <xs:maxInclusive value="72"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="fontbystringname"> <xs:restriction base="xs:string"> <xs:enumeration value="small"/> <xs:enumeration value="medium"/> <xs:enumeration value="large"/> </xs:restriction> </xs:simpleType>
  • 166. union of two simple types. <xs:attribute name="MonitorSize"> <xs:simpleType> <xs:union memberTypes="sizebynumber sizebystringname" /> </xs:simpleType> </xs:attribute> <xs:simpleType name="sizebynumber"> <xs:restriction base="xs:positiveInteger"> <xs:maxInclusive value="21"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="sizebystringname"> <xs:restriction base="xs:string"> <xs:enumeration value="small"/> <xs:enumeration value="medium"/> <xs:enumeration value="large"/> </xs:restriction> </xs:simpleType>
  • 167. Prohibiting Derivation You may, for some reason, have a simple type that you don’t want anybody to derive further types from. Do this by specifying the final attribute on the <simpleType> element. Its value is a list containing a subset of values from list, union, restriction, extension. Give the final attribute the value “#all” for blanket prohibition of any derivation from this simple type.  Can also prevent the value of individual facets from being changed in subsequent restrictions by specifying fixed="true" on the facet elements.
  • 168. Prohibit XML Schema Derivations The final attribute  Can be used in  xs:complexType  xs:simpleType  xs:element  Can take values of  restriction  extension  #all (any derivation) The fixed attribute  Can only be used in xs:simpleType  When it is set to true, it cannot be further modified <xs:complexType name="characterType" final="#all"> <xs:sequence> <xs:element name="name" type="nameType"/> <xs:element name=“age" type=“ageType"/> <xs:element name="qualification" type="descType"/> </xs:sequence> </xs:complexType> <xs:simpleType name=“fixedString"> <xs:restriction base="xs:string"> <xs:maxLength value="32" fixed="true"/> </xs:restriction> </xs:simpleType>
  • 169. Complex Types Complex Types define logical structures with attributes and nested elements They use a sequence, choice or all containing elements that use Simple Types or other Complex Types May reference types defined elsewhere in the schema or imported using import statement
  • 170. Definition – complex type <xsd:complexType name="PurchaseOrderType"> <xsd:sequence> <xsd:element name="shipTo" type="Address"/> <xsd:element name="billTo" type="Address"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="items" type="Items"/> </xsd:sequence> <xsd:attribute name="orderDate" type="xsd:date"/> </xsd:complexType> Define the complex type “purchaseOrderType”  Allow elements & attributes  contain a set of element declarations, element references, and attribute declarations. element declarations attribute declaration element reference
  • 171. Element Content Three different ways  Complex types from simple types  Mixed content  Elements mixed with character content  Empty content
  • 172. XML-Schema : Mixed, Empty and Any Content Mixed content is used if you want to model elements that includes both subelements and character data Empty content is used to define elements that do not include any subelements and character data Any content (the most basic data type) does not constrain the content in any way
  • 173. Example : Defining a complexType <complexType name= “Customer”> <sequence> <element name= “Person” type=“Name” /> <element name= “Address” type=“AddressT” /> </sequence> </complexType> <complexType name=“AddressT”> <sequence> <element name=“Street” type=“string” /> <element name=“City” type=“string” /> <element name=“State” type=“State_Region” /> <element name=“PostalCode” type=“string” /> <element name=“Country” type=“string” /> </sequence> <!-- Attributes definition goes here --> </complexType>
  • 174. Complex Type from a Simple Type <xsd:element name="internationalPrice"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="xsd:decimal"> <xsd:attribute name="currency” type="xsd:string" /> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element> XML Instance: <internationalPrice currency="EUR">423.46</internationalPrice>
  • 175. Mixed elements Mixed elements may contain both text and elements We add mixed="true" to the xs:complexType element <xs:complexType name="paragraph" mixed="true"> <xs:sequence> <xs:element name="someName" type="xs:anyType"/> </xs:sequence> </xs:complexType>
  • 176. Mixed Content Sub-elements mixed with character data <letterBody> <salutation>Dear Mr.<name>Anu Malik</name>. </salutation>Your order of <quantity>1</quantity> <productName>LCD Monitor</productName> shipped from our warehouse on <shipDate>2009-09- 18</shipDate>. .... </letterBody>
  • 177. Mixed Content <xsd:element name="letterBody"> <xsd:complexType mixed="true"> <xsd:sequence> <xsd:element name="salutation"> <xsd:complexType mixed="true"> <!-- Implicit definition -> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="quantity" type="xsd:positiveInteger"/> <xsd:element name="productName" type="xsd:string"/> <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/> <!-- etc --> </xsd:sequence> </xsd:complexType> </xsd:element>
  • 178. Empty Elements XML Schema doesn’t have any unique way of representing elements that must be empty. The simplest thing to do this is simply omit the allowed element content in a complex content restriction. Can such an element also be mixed (i.e. have pure text content)?  Logically it seems this should be possible  But it seems to be forbidden by the XML Schema specification, which singles out this case and says such an element is strictly empty.
  • 179. Empty Elements To specify Empty type of element, define the element’s type using the xsd:complexType element, but omit the content model. <xsd:element name=”BR”> <xsd:complexType> </xsd:complexType> </xsd:element> The following is a conforming element:  <BR></BR> as is this one:  <BR/>
  • 180. Empty Content <img src="xml.gif" alt="Programming XML image"/> <xsd:element name="img"> <xsd:complexType> <xsd:attribute name="src“ type="xsd:NMTOKEN"/> <xsd:attribute name="alt" type="xsd:string"/> </xsd:complexType> </xsd:element>
  • 181. Complex Type Hierarchy There are no built in complex types, other than the so-called ur-type, represented as xsd:anyType. All other complex types are derived by one or more steps of restriction and extension from xsd:anyType.  Complex types can also be created by extension of a simple type, but simple types are also notionally restrictions of xsd:anyType.
  • 182. XML Schema built-in Datatypes
  • 183. Restriction A restriction of a base type is a new type. All allowed instances of the new type are also instances of the base type. But the restricted type doesn’t allow all possibilities allowed by the base type.  Think of the example of restricting xsd:string to 4 characters using the length facet. Strings of length 4 are also allowed by the xsd:string, but the new type is more restrictive.  In the complex case, we might have a complex base type that allows attribute att optionally. A restricted type might not allow att at all.  Another restriction of the same base might require att.  Or we might have a base type that allows 0 or more nested elm elements. The restricted type might require exactly 1 nested elm element.
  • 184. Extension An extension of a base type is a new type. An extension allows extra attributes or extra content that are not allowed in instances of the base type.  At first brush this sounds like the opposite of restriction, but this isn’t strictly true.  If, for example, type E extends a type B by adding a required attribute att, then instances of B are not allowed instances of E (because they don’t have the required attribute). So we have that E is an extension of B, but there is no sense in which B could be a restriction of E.  Some such inverse relation exists if all extra attributes and content are optional in the extended type, but this isn’t a required feature of extension.
  • 185. Remarks When one restricts a type one generally must specify all allowed element content and attributes. When one extends a type one generally must specify just the extra element content and attributes.
  • 186. Complex Content and Simple Content We have seen that XML Schema complex types define both some allowed nested elements, and some allowed attribute specifications. Complex types that allow nested elements are said to have complex content. But Schema distinguish as a special case complex types having simple content—elements with such types may have attributes, but they cannot have nested elements. This is presumably a useful distinction, but it does introduce one more layer of complexity into the syntax for complex type derivation.
  • 187. Complex Content and Simple Content Simple Type Complex Type Simple Content Complex Content Type allows attributes or elements? allows elements in content?
  • 188. Simple contents A good example of complex elements with simple content is the <textarea> element in HTML documents. It accepts a text string as the content with no sub (child) element and some attributes. <xs:element name="textarea"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="name" type="xs:string"/> <xs:attribute name="cols" type="xs:positiveInteger"/> <xs:attribute name="rows" type="xs:positiveInteger"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element>
  • 189. Simple contents Simple contents contain only attributes  E.g. <xs:element name="organism"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="taxonomy_id" type="xs:integer" use="required"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element>
  • 190. Simple content type <xsd:element name="internationalPrice"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="xsd:decimal"> <xsd:attribute name="currency” type="xsd:string" /> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element> <internationalPrice currency="EUR">423.46</internationalPrice> simpleContent indicates that the content model of the new type contains only character data and no element declaration
  • 191. Complex contents Elements including children are defined as complex types with complex content  E.g. <xs:element name="protein_set"> <xs:complexType> <xs:complexContent> <xs:restriction base="xs:anyType"> <xs:sequence> <xs:element ref="protein" maxOccurs="unbounded"/> </xs:sequence> </xs:restriction> </xs:complexContent> </xs:complexType> </xs:element>
  • 192. Basic Forms of Complex Type Definition Restriction Extension <complexType> <complexContent> <restriction base="type"> allowed element content allowed attributes </restriction> </complexContent> </complexType> <complexType> <complexContent> <extension base="type"> extra element content extra attributes </extension> </complexContent> </complexType> <complexType> <simpleContent> <restriction base="type"> facet restrictions allowed attributes </restriction> </simpleContent> </complexType> <complexType> <simpleContent> <extension base="type"> extra attributes </extension> </simpleContent> </complexType>
  • 193. Requirements on Base Type The base type must be a complex type in all cases except simpleContent/extension (lower right in table shown in the previous slide), in which case the base can be a simple type.
  • 194. Schematic Inheritance Diagram xsd:anyType Simple Types Simple Content Complex Content extension restriction restriction restriction Complex Types restriction list union restriction extension restriction extension restriction
  • 195. Defining a Complex Type with no Base? Actually the XML Schema specification says that: <complexType> <complexContent> <restriction base="xsd:anyType"> allowed element content allowed attributes </restriction> </complexContent> </complexType> <complexType> allowed element content allowed attributes </complexType>  So in reality we were directly restricting the ur-type, which allows any attributes and any content! Is “shorthand” for
  • 196. Empty Content - 1 <internationalPrice currency=“EUR” value=“345.23”/> <xsd:element name="internationalPrice"> <xsd:complexType> <xsd:complexContent> <xsd:restriction base="xsd:anyType"> <xsd:attribute name="currency" type="xsd:string"/> <xsd:attribute name="value” type="xsd:decimal"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> </xsd:element>
  • 197. Empty Content - 2 <xsd:element name="internationalPrice"> <xsd:complexType> <xsd:attribute name="currency” type="xsd:string"/> <xsd:attribute name="value” type="xsd:decimal"/> </xsd:complexType> </xsd:element> A complex type defined without complexContent is interpreted as shorthand for complex content that restricts anyType
  • 198. XSD Complex Types Indicators We have seven types of indicators:  Order indicators:  All  Choice  Sequence  Occurrence indicators:  maxOccurs  minOccurs  Group indicators:  Group name  attributeGroup name
  • 199. Defining Element Content Where we wrote allowed element content or extra element content in the syntax for complex type definitions, what should appear is a model group. A model group is exactly one of:  an <xsd:sequence/> element, or  an <xsd:choice/> element, or  an <xsd:all/> element. (The element content appearing in the type definition may also be a globally defined model group, referenced through an <xsd:group/> element. The global definition—a named <xsd:group/> element—just contains one of the three elements above.)
  • 200. Sequence A <xsd:sequence/> model group contains a series of particles. A particle is an <xsd:element/> element, another model group, or a wildcard. As expected, this model just says the element content represented by those items should appear in sequence. E.g. <xsd:sequence> <xsd:element ref="title"/> <xsd:element ref="paragraph" minOccurs="0“ maxOccurs="unbounded"/> </xsd:sequence>
  • 201. xs:sequence We’ve already seen an example of a complex type whose elements must occur 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>
  • 202. Complex Types (title?,firstname*,lastname) <xsd:complexType name="name"> <xsd:sequence> <xsd:element name="title" minOccurs="0"/> <xsd:element name="firstname" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="lastname"/> </xsd:sequence> </xsd:complexType>