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>
Choice
A <xsd:choice/> model group also contains a series
of particles, with the same options as for sequence.
The element information validated by this model
should match exactly one of the particles in the
choice.
E.g.
<xsd:choice minOccurs="0" maxOccurs="unbounded"/>
<xsd:element ref="paragraph">
<xsd:sequence>
<xsd:element ref="figure"/>
<xsd:element ref="caption"/>
<xsd:sequence>
</xsd:choice>
xsd:Choice
Choice : One of them is allowed
(PRODUCT, NUMBER, (PRICE | CHARGEACCT | SAMPLE))
<xsd:complexType name="itemType">
<xsd:sequence>
<xsd:element ref="PRODUCT"/>
<xsd:element ref="NUMBER"/>
<xsd:choice>
<xsd:element ref="PRICE"/>
<xsd:element ref="CHARGEACCT"/>
<xsd:element ref="SAMPLE"/>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
xsd:Choice
In DTD syntax: <!ELEMENT name ((First Last) | (Last, First)) >
<xs:complexType name="name.type">
<xs:choice>
<xs:sequence>
<xs:element ref=“Frst"/>
<xs:element ref=“Last"/>
</xs:sequence>
<xs:sequence>
<xs:element ref=“Last"/>
<xs:element ref=“First"/>
</xs:sequence>
</xs:choice>
</xs:complexType>
<xs:element name="First" type="xs:string"/>
<xs:element name="Last" type="xs:string"/>
<xs:element name="name" type="name.type"/>
Validates both
<name><First>Shakti</Frst><Last>Kapoor</Last></name>
and
<name><Last>Kapoor</Last><First>Shakti</First></name>
All
The <xsd:all/> model group is peculiar to XML
Schema. All particles it contains must be
<xsd:element/>s.
The element information validated should match a
sequence of the particles in any order.
There are several constraints:
 The maxOccurs attribute of each particle must be 1.
 The minOccurs attribute of each particle must be 0 or 1.
 The <xsd:all/> model group can only occur at the top
level of a complex type’s content model, and must itself
have minOccurs = maxOccurs = 1.
xs:all
xs:all allows elements to appear in any order
<xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element name="firstName" type="xs:string" />
<xs:element name="lastName" type="xs:string" />
</xs:all>
</xs:complexType>
</xs:element>
Element Wildcard
The element wildcard particle <xsd:any/> matches
and validates any element in the instance document.
E.g.
<xsd:sequence minOccurs="0“ maxxOccurs="unbounded">
<xsd:element ref=“header"/>
<xsd:any/>
</xsd:sequence>
matches a sequence of consecutive pairs of elements,
where the first element in each pair is a header, and
the second can be any kind of element.
Options on <xsd:any/>
The <xsd:any/> element takes the usual optional
maxOccurs, minOccurs attributes.
Allows a namespace attribute taking one of the values:
 ##any (the default),
 ##other (any namespace except the target namespace),
 List of namespace names, optionally including either
##targetNamespace or ##local.
Controls what elements the wildcard matches, according
to namespace.
It also allows a processContents attribute taking one of
the values strict, skip, lax (default strict), controlling the
extent to which the contents of the matched element
are validated.
Specifying the Namespace of
Extension Elements
<any namespace="##other"/> allows the instance document to contain a new element,
provided the element comes from a namespace other than the
one the schema is defining (i.e., targetNamespace).
<any namespace="http://www.somewhere.com"/> allows a new element,
provided it's from the specified namespace
Note: you can specify a list of namespaces,
separated by a blank space. One of the
namespaces can be ##targetNamespace (see
next)
<any namespace="##targetNamespace"/> allows a new element,
provided it's from the namespace that the schema
is defining.
<any namespace="##any"/> allows an element from any namespace. This is the default.
<any namespace="##local"/> the new element must come from no namespace
Process Contents
<xs:complexType name=“bodyType”>
...
<xsdsequence>
<xs:any processContents=“skip”/>
</xs:sequence>
...
</xs:complexType>
<xs:complexType name=“bodyType”>
...
<xs:sequence>
<xs:any processContents=“strict”/>
</xs:sequence>
...
</xs:complexType>
Process Contents
<xs:complexType name=“bodyType”>
...
<xs:sequence>
<xs:any processContents=“lax”/>
</xs:sequence>
...
</xs:complexType>
Process Contents
Declaring Attributes
An attribute is declared by using the xsd:attribute
schema element.
Like elements, attributes must have a name and
type
An attribute always has a simple type.
Attributes can use custom data types
Attributes can be restricted in regard to cardinality
or default values
Attributes can refer to other attribute definitions
<xsd:attribute name = “country”
type = “xsd:string” fixed = “India”/>
Defining an attribute
Attributes themselves 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
 default and fixed are mutually exclusive.
 use="optional" the attribute is not required (default)
 use="required" the attribute must be present
Declaring Attributes
We can use a built-in simple types to an
attribute by assigning the type’s name to the
type attribute of the xsd:attribute element.
<xsd:attribute name=”IndexPages”
type=”xsd:positiveInteger”/>
Attributes with constraints
<xsd:attribute name=”InStock”
type=”xsd:boolean” use=”required”/>
<xsd:attribute name=”PartNum”
type=”xsd:string” default=”0-00-0"/>
<xsd:attribute name=”Priority”
type=”xsd:string” fixed=”high”/>
Declaring Attributes
We can also define a new simple type by
including the xsd:simpleType schema element
within the xsd:attribute element.
<xsd:attribute name=”Type”>
<xsd:simpleType>
<xsd:restriction base=”xsd:string”>
<xsd:enumeration value=”Comdey”/>
<xsd:enumeration value=”Tragedy”/>
<xsd:enumeration value=”Documentary”/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
Simple Content plus Attributes
Here is a declaration of an anchor element that
allows simple content plus an href attribute.
<xsd:element name="anchor">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string”>
<xsd:attribute name="href" type="xsd:anyURI"/>
<xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
Text element with attributes
If a text element has attributes, it is no longer a simple type
 <xs:element name="population">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="year" type="xs:integer">
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
Empty element with a attribute
<xs:complexType name="counter">
<xs:complexContent>
<xs:extension base="xs:anyType"/>
<xs:attribute name="count" type="xs:integer"/>
</xs:complexContent>
</xs:complexType>
Simple Attribute Declarations
Using attribute declaration within a complex
element.
<xsd:element name="figure">
<xsd:complexType>
<xsd:attribute name="source" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
Attributes and Complex Types from Simple Types
DTD:
<!ELEMENT greeting (#PCDATA)>
<!ATTLIST greeting language CDATA "English">
XML Schema:
<xsd:element name="greeting">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="language" type="xsd:string“
default=“English” />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
XML Instance Document:
<greeting language="Spanish"> Hola! </greeting>
Complex Content Plus Attributes
Here is a declaration of a body element that allows
mixed content plus a style attribute.
<xsd:element name="body">
<xsd:complexType mixed="true">
<xsd:choice minOccurs="0"
maxOccurs="unbounded">
<element ref="p"/>
<element ref="a"/>
</xsd:choice>
<xsd:attribute name="style" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
Adding Attributes to an Element with Element,
Mixed, or Empty Content
<xsd:element name=”FILM”>
<xsd:complexType>
<xsd:sequence>
<xsd:element name=”TITLE” type=”xsd:string”/>
<xsd:choice>
<xsd:element name=”STAR” type=”xsd:string”/>
<xsd:element name=”SongName” type=”xsd:string”/>
<xsd:element name=”Producer” type=”xsd:string”/>
</xsd:choice>
</xsd:sequence>
<xsd:attribute name=”Class”>
<xsd:simpleType>
<xsd:restriction base=”xsd:string”>
<xsd:enumeration value=”fictional”/>
<xsd:enumeration value=”documentary”/>
<xsd:enumeration value=”instructional”/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
Attribute Wildcards
An attribute wildcard is represented by an
<xsd:anyAttribute/> element.
There can be at most one such element in a
complex type definition, and it must appear
after any normal attribute declarations.
Such an declaration allows any attribute,
optionally limited by namespace.
The namespace and processContents
attributes on <xsd:anyAttribute/> work as for
<xsd:any/>.
<anyAttribute> Example
<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:anyAttribute/>
</xs:complexType>
</xs:element>
<anyAttribute> Example
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema …>
<xs:attribute name="gender“>
<xs:simpleType>
<xs:restriction base="xs:string“>
<xs:pattern
value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:schema>
<?xml version="1.0" encoding="ISO-8859-1"?>
<persons …>
<person gender="female“>
<firstname>Karishma</firstname>
<lastname>Kapoor</lastname>
</person>
<person gender="male“>
<firstname>Nana</firstname>
<lastname>Patekar</lastname>
</person>
</persons>
<anyAttribute> Example
Attributes and Namespaces
By default, attributes (declared inside an
<xsd:complexType/>) do not become part of
the target namespace.
 Instead these attributes are local properties of
any element they are attached to. The element
itself may or may not belong to a namespace.
In instance documents, names of these
attributes must not be prefixed with a
namespace prefix.
Creating Attributes in a Namespace
There are three ways to put attributes into the target
namespace:
 Declare them “globally”, directly inside the top level
<xsd:schema/> element. Reference the attribute
declaration inside the complex type definition (like
element references), or
 specify the attribute form="qualified" on a local
<xsd:attribute/> declaration, or
 specify the attribute attributeFormDefault="qualified" on
the <xsd:schema/> element.
After this, these attributes must be prefixed in
instance documents with a namespace prefix.
 Recall default namespace declarations
(xmlns="namespace") don’t work for attributes: you must
introduce a non-empty prefix.
Explicit Type vs. Implicit Type
Explicit type
 One in which a name is given to the type
 Element that uses the type is generally defined in
a different section of the file
 Object-oriented in that same explicit type is used
as the type for several different elements
Implicit type (nameless type)
 Use when the type is not needed by multiple
elements
Example of Explicit Type
<!-- Type has a name zipUnion -->
<xsd:simpleType name="zipUnion">
<xsd:union memberTypes="State listOfMyIntType"/>
</xsd:simpleType>
<!-- zipUnion type is used in other parts of Schema document -->
<element name=zips type=“zipUnion”>
…
<element name=theOtherZips type=“zipUnion”>
…
<element name=theThirdZips type=“zipUnion”>
Anonymous User-Defined Simple Type
<xsd:element name="elevation">
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="-1290"/>
<xsd:maxInclusive value="29035"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
The simpleType definition is
defined inline, it is an
anonymous
simpleType definition.
The disadvantage of this
approach is
that this simpleType may
not be
reused by other elements.
Example of Implicit Type
<xsd:complexType name="Items"> <!– Explicit complexType -->
<xsd:sequence>
<xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType> <!-- Implicit complexType -->
<xsd:sequence>
<xsd:element name="productName" type="xsd:string"/>
<xsd:element name="quantity">
<xsd:simpleType> <!-- Implicit 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"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
Types
Simple vs. Complex
Simple types
Complex types
Can be either:
global and named, or
local and anonymous
Local and Global declarations
Global
 Declared at top level of a schema, just immediately below
xsd:schema element. They are available to be used throughout rest
of the schema.
 Global element declaration do not determine where an element can
appear in the XML document – they only determine what the
element will look like.
 A global element declaration must be explicitly referenced in order to
have it appear in a corresponding XML document.
Local
 Locally declared elements. Limited to the complex type definition in
which they are declared and may not be used elsewhere in the
schema.
 Such locally declared elements are automatically referenced.
Global and local definitions
Elements declared at the “top level” of a <schema> are available for use
throughout the schema
Elements declared within a xs:complexType are local to that type
Thus, in
<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>
the elements firstName and lastName are only locally declared
The order of declarations at the “top level” of a <schema> do not specify
the order in the XML data document
Declaration and use
To use a type we have declared, use it as the value of
type="..."
 Examples:
 <xs:element name="student" type="person"/>
 <xs:element name="professor" type="person"/>
 Scope is important: you cannot use a type if is local to
some other type
Local and Global Types in XML Schema
Local type:
<xsd:element name=“person”>
[define locally the person’s type]
</xsd:element>
Global type:
<xsd:element name=“person” type=“ttt”/>
<xsd:complexType name=“ttt”>
[define here the type ttt]
</xsd:complexType>
Global types: can be reused in other elements
Local Names in XML-Schema
<xsd:element name=“person”>
<xsd:complexType>
. . . . .
<xsd:element name=“name”>
<xsd:complexType>
<xsd:sequence>
<xsd:element name=“firstname” type=“xsd:string”/>
<xsd:element name=“lastname” type=“xsd:string”/>
</xsd:sequence>
</xsd:element>
. . . .
</xsd:complexType>
</xsd:element>
<xsd:element name=“product”>
<xsd:complexType>
. . . . .
<xsd:element name=“name” type=“xsd:string”/>
</xsd:complexType>
</xsd:element>
name has
different
meanings
in person and
in product
What is ref ?
ref is use to reference an existing element or
attribute rather than declaring a new
element or attribute
Existing element must be global element -
an element that is declared under root
element
ref Example
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<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>
<xs:element name="purchaseOrder">
<xs:complexType>
<xs:sequence>
<xs:element ref="recipient" />
<xs:element ref="order" />
</xs:sequence>
<xs:attribute name="orderDate" type="xs:string" />
</xs:complexType>
</xs:element>
Example: Using refs - 1
<xs:element name = "recipient">
<xs:complexType>
<xs:sequence>
<xs:element ref="name" />
<xs:element ref="street" />
<xs:element ref="city" />
<xs:element ref="state" />
<xs:element ref="postalCode" />
</xs:sequence>
<xs:attribute name="country" type="xs:string" />
</xs:complexType>
</xs:element>
Example: Using refs - 2
<xs:element name = "name" type="xs:string" />
<xs:element name = "street" type="xs:string" />
<xs:element name = "city" type="xs:string" />
<xs:element name = "state" type="xs:string" />
<xs:element name = "postalCode" type="xs:short" />
<xs:element name = "order">
<xs:complexType>
<xs:sequence>
<xs:element ref="cd" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Example: Using refs - 3
<xs:element name="cd">
<xs:complexType>
<xs:attribute name="artist" type="xs:string" />
<xs:attribute name="title" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:schema>
Example: Using refs - 4
Referencing
Once you have defined an element or attribute
(with name="..."), you can refer to it with
ref="..."
Example:
 <xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element name="firstName" type="xs:string" />
<xs:element name="lastName" type="xs:string" />
</xs:all>
</xs:complexType>
</xs:element>
 <xs:element name="student" ref="person">
 Or just: <xs:element ref="person">
References to Elements:
DTD:
<!ELEMENT BOOK (P*)>
<!ELEMENT P (#PCDATA)>
XML Schema
<xsd:element name="P" type="xsd:string"/>
<xsd:element name="BOOK">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="P" minOccurs="0"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Secondary Schema Components
Model Group Definitions
Attribute Group Definitions
Identity-constraint Definitions
 Similar to ID / IDREF
Notations Declarations
Wildcards
 Similar to a DTD with “ANY”
Annotations
Group Element
The group element enables you to group
together element declarations.
Note: the group element is just for grouping
together element declarations, no attribute
declarations allowed!
Named Model Groups
A named model group is a collection, or group,
of elements. The syntax for creating a model
group is
<xs:group name="name">
elements
</xs:group>
Where name is the name of the model group,
and elements is a collection of element
declarations
<xsd:element name="Book" >
<xsd:complexType>
<xsd:sequence>
<xsd:group ref="PublicationElements"/>
<xsd:element name="ISBN" type="string"/>
<xsd:element name="Reviewer" type="string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="CD" >
<xsd:complexType>
<xsd:sequence>
<xsd:group ref="PublicationElements"/>
<xsd:element name="RecordingStudio" type="string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:group name="PublicationElements">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:string"/>
</xsd:sequence>
</xsd:group>
An example showing the use of the <group> element
Note about group
Group definitions must be global
<xsd:element name="Book">
<xsd:complexType>
<xsd:sequence>
<xsd:group name="PublicationElements">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" minOccurs="0"/>
<xsd:element name="Author" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:string"/>
</xsd:sequence>
</xsd:group>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
...
</xsd:complexType>
</xsd:element>
Cannot inline the
group definition.
Instead, you must
use a ref here and
define the group
globally.
Named Attribute Groups
Attributes can be grouped into collections called
named attribute groups.
This is particularly useful for attributes that you want
to use with several different elements in a schema.
The syntax for a named attribute group is
<xs:attributeGroup name="name">
attributes
</xs:attributeGroup>
Where name is the name of the attribute group and
attributes is a collection of attributes assigned to the
group.
Attribute Groups
Promotes logical organization
Encourages reuse – defined once, referenced
many times
Facilitates maintenance
Improves Schema readability
Must be unique within an XML Schema
Referenced from complexType definitions
Declaring Attribute Groups
<!-- Define the unique group: -->
<attributeGroup name = “CreditCardInfo” >
<attribute name = “CardNumber” type = “integer”
use = “required” />
<attribute name = “ExpirationDate” type = “date”
use = “required” />
<attribute name = “CardHolder” type = “FullName”
use = “required” />
</attributeGroup>
<!-- Then you can reference it from a complexType: -->
<complexType name = “CreditInformation” >
<attributeGroup ref = “CreditCardInfo” />
</complexType>
Attribute Groups
DTD:
<!ELEMENT img EMPTY>
<!ATTLIST img alt CDATA #REQUIRED
src CDATA #REQUIRED
width CDATA #IMPLIED
height CDATA #IMPLIED>
XML Schema:
<xsd:attributeGroup name="imgAttributes">
<xsd:attribute name="alt" type="xsd:string" use="required"/>
<xsd:attribute name="src" type="xsd:NMTOKEN" use="required"/>
<xsd:attribute name="width" type="xsd:integer"/>
<xsd:attribute name="height" type="xsd:integer"/>
</xsd:attributeGroup>
<xsd:element name="img">
<xsd:complexType>
<xsd:attributeGroup ref="imgAttributes"/>
<xsd:complexType>
</xsd:element>
Example of attributeGroup
<xsd:attributeGroup name="ItemDelivery">
<xsd:attribute name="partNum" type="SKU"/>
<xsd:attribute name="weightKg" type="xsd:decimal"/>
<xsd:attribute name="shipBy">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="air"/>
<xsd:enumeration value="land"/>
<xsd:enumeration value="any"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:attributeGroup>
<!-- attributeGroup replaces individual declarations -->
<xsd:attributeGroup ref="ItemDelivery"/>
Identity Constraints
Recall that the attribute types ID and IDREF
imply interesting constraints on values of those
attributes:
 Within any individual XML document, every
attribute of type ID must be specified with a
different value from every other attribute of type
ID.
 The value of any attribute of type IDREF must be
the same as the value of an attribute of type ID
specified somewhere in the same document.
Identity Constraints
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="born" type="xs:date"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID"/>
</xs:complexType>
</xs:element>
IDREF Constraints
Relating Person and Book using IDREF
<xsd:element name="Person">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="Book">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author">
<xsd:complexType>
<xsd:attribute name=“PersonID" type="xsd:IDREF" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
IDREF Constraints
Relating Person and Book using IDREF
<xsd:element name="Name" type="xsd:string"/>
<xsd:attribute name="id" type="xsd:ID"/>
<xsd:element name="Person">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Name"/>
</xsd:sequence>
<xsd:attribute ref="id" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="Title" type="xsd:string"/>
<xsd:attribute name=“PID" type="xsd:IDREF"/>
<xsd:element name="Author">
<xsd:complexType>
<xsd:attribute ref=“PID" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="Book">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Title"/>
<xsd:element ref="Author"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Annotation
The annotation element provides a
mechanism for documenting most other
schema elements.
Unlike an XML comment, which looks
like <!-- comment text -->, the annotation is
part of the schema component.
An annotation provides for human
documentation in one or more languages, as
well as programmatic documentation, such as
meaningful URIs
Annotation
Appears at the beginning of most schema
constructions
Can have two sub-elements
 Documentation
 appInfo
Documentation
 For human readable materials
appInfo
 For tools, stylesheets and other applications
XML-Schema : Annotations
XML-schema provides several tags for annotating a schema:
documentation (intended for human readers), appInfo
(intended for applications) and annotation. This mixed
content model allows the inclusion of almost any content,
such as text:
<xs:element name="author" type="author">
<xs:annotation>
<xs:documentation xml:lang="en">
The author of a book.
</xs:documentation>
<xs:documentation xml:lang="fr">
Designe l'auteur d'un livre.
</xs:documentation>
</xs:annotation>
</xs:element>
XML-Schema : Annotations
<xs:simpleType name=“IndianStates">
<xs:annotation>
<xs:documentation>States in India</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:string">
<xs:enumeration value='KA'>
<xs:annotation>
<xs:documentation>Karnataka</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value=‘KL'/>
<xs:annotation>
<xs:documentation>Kerala</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
Example of Annotation
<xsd:element name="internationalPrice">
<xsd:annotation>
<xsd:documentation> element declared with anonymous type
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:annotation>
<xsd:documentation> empty anonymous type with 2 attributes
</xsd:documentation>
</xsd:annotation>
<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>
XML-Schema : AppInfo
Annotations are also a good container for
application-specific metadata, such as those
used by the schema for schema to describe
the list of facets and properties of its
primitive datatypes:
XML-Schema : AppInfo
<xs:simpleType name="string" id="string">
<xs:annotation>
<xs:appinfo>
<hfp:hasFacet name="length"/>
<hfp:hasFacet name="minLength"/>
<hfp:hasFacet name="maxLength"/>
<hfp:hasFacet name="pattern"/>
<hfp:hasFacet name="enumeration"/>
<hfp:hasFacet name="whiteSpace"/>
<hfp:hasProperty name="ordered" value="false"/>
<hfp:hasProperty name="bounded" value="false"/>
<hfp:hasProperty name="cardinality" value="countably
infinite"/>
<hfp:hasProperty name="numeric" value="false"/>
</xs:appinfo>
<xs:documentation source="http://www.w3.org/TR/xmlschema-2/#string"/>
</xs:annotation>
No targetNamespace
(noNamespaceSchemaLocation)
Sometimes you may wish to create a schema but
without associating the elements with a namespace.
The targetNamespace attribute is actually an
optional attribute of <schema>. Thus, if you don’t
want to specify a namespace for your schema then
simply don’t use the targetNamespace attribute.
Consequences of having no namespace
 1. In the instance document don’t namespace
qualify the elements.
 2. In the instance document, instead of using
schemaLocation use
noNamespaceSchemaLocation.
Note that there is no
targetNamespace
attribute, and note that
there is no longer a
default namespace.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Book" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Book">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Title"/>
<xsd:element ref="Author"/>
<xsd:element ref="Date"/>
<xsd:element ref="ISBN"/>
<xsd:element ref="Publisher"/>
</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>
<?xml version="1.0"?>
<BookStore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation= "BookStore.xsd">
<Book>
<Title>My Life and Times</Title>
<Author>Paul McCartney</Author>
<Date>1998</Date>
<ISBN>1-56592-235-2</ISBN>
<Publisher>McMillin Publishing</Publisher>
</Book>
…
</BookStore>
1. Note that there is no default namespace declaration. So, none of the elements are
associated with a namespace.
2. Note that we do not use xsi:schemaLocation (since it requires a pair of values - a
namespace and a URL to the schema for that namespace). Instead, we use
xsi:noNamespaceSchemaLocation.
Assembling an Instance Document
from Multiple Schema Documents
An instance document may be composed
of elements from multiple schemas.
Validation can apply to the entire XML
instance document, or to a single element.
<?xml version="1.0"?>
<Library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.book.org
Book.xsd
http://www.employee.org
Employee.xsd">
<Books>
<Book xmlns="http://www.book.org">
<Title>My Life and Times</Title>
<Author>Paul McCartney</Author>
<Date>1998</Date>
<ISBN>1-56592-235-2</ISBN>
<Publisher>Macmillan Publishing</Publisher>
</Book>
<Book xmlns="http://www.book.org">
<Title>Illusions: The Adventures of a Reluctant Messiah</Title>
<Author>Richard Bach</Author>
<Date>1977</Date>
<ISBN>0-440-34319-4</ISBN>
<Publisher>Dell Publishing Co.</Publisher>
</Book>
<Book xmlns="http://www.book.org">
<Title>The First and Last Freedom</Title>
<Author>J. Krishnamurti</Author>
<Date>1954</Date>
<ISBN>0-06-064831-7</ISBN>
<Publisher>Harper &amp; Row</Publisher>
</Book>
</Books>
<Employees>
<Employee xmlns="http://www.employee.org">
<Name>John </Name>
<SSN>123-45-6789</SSN>
</Employee>
<Employee xmlns="http://www.employee.org">
<Name>Smith</Name>
<SSN>000-11-2345</SSN>
</Employee>
</Employees>
</Library>
Validating against
two schemas
The <Book> elements are
defined in Book.xsd, and
the <Employee> elements
are defined in Employee.xsd.
The <Library>, <Books>,
and <Employees> elements
are not defined in any schema!
1. A schema validator will
validate each Book element
against Book.xsd.
2. It will validate each
Employee element against
Employee.xsd.
3. It will not validate the other
elements.
Lax Validation vs
Strict Validation
On the previous slide there were elements (Library, Books, and
Employees) for which there was no schema to validate against.
Lax validation is where the schema validator skips over
elements for which no schema is available.
Strict validation is where the schema validator requires
validation of every element
Most of the validators do strict validation. Consequently, they
will reject the instance document on the previous slide.
Assembling a Schema from
Multiple Schema Documents
The include element allows you to access components in other schemas
 All the schemas you include must have the same namespace as your
schema (i.e., the schema that is doing the include)
 The net effect of include is as though you had typed all the definitions
directly into the containing schema
<xsd:schema …>
<xsd:include schemaLocation="LibraryBook.xsd"/>
<xsd:include schemaLocation="LibraryEmployee.xsd"/>
…
</xsd:schema>
LibraryBook.xsd LibraryEmployee.xsd
Library.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.library.org"
xmlns="http://www.library.org"
elementFormDefault="qualified">
<xsd:include schemaLocation="LibraryBook.xsd"/>
<xsd:include schemaLocation="LibraryEmployee.xsd"/>
<xsd:element name="Library">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Books">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Book" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Employees">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Employee" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Library.xsd
These are
referencing
element
declarations
in the other
schemas.
Assembling a Schema from a Schema
with no targetNamespace
A schema can <include> another schema which has
no targetNamespace. The included components
take on the targetNamespace of the schema that is
doing the <include>. This is called the Chameleon
Effect.
The components in the no-namespace schema are
called Chameleon components.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xsd:complexType name="ProductType">
<xsd:sequence>
<xsd:element name="Type" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema> Product.xsd
Note that this schema has no targetNamespace!
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.company.org"
xmlns="http://www.company.org"
elementFormDefault="qualified">
<xsd:include schemaLocation="Person.xsd"/>
<xsd:include schemaLocation="Product.xsd"/>
<xsd:element name="Company">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Person" type="Person" maxOccurs="unbounded"/>
<xsd:element name="Product" type="ProductType" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema> Company.xsd
This schema <include>s Product.xsd. Thus, the components in Product.xsd are
namespace-coerced to the company targetNamespace. Consequently, we can
reference those components just as though they had originally been declared in
a schema with the same targetNamespace.
Assembling a Schema from Multiple Schema
Documents with Different Namespaces
The import element allows you to access
elements and types in a different namespace
<xsd:schema …>
<xsd:import namespace="A"
schemaLocation="A.xsd"/>
<xsd:import namespace="B"
schemaLocation="B.xsd"/>
…
</xsd:schema>
Namespace
A
A.xsd
Namespace
B
B.xsd
C.xsd
Camera Schema
Camera.xsd
Nikon.xsd Olympus.xsd Pentax.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.nikon.com"
xmlns="http://www.nikon.com"
elementFormDefault="qualified">
<xsd:complexType name="body_type">
<xsd:sequence>
<xsd:element name="description" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Nikon.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.olympus.com"
xmlns="http://www.olympus.com"
elementFormDefault="qualified">
<xsd:complexType name="lens_type">
<xsd:sequence>
<xsd:element name="zoom" type="xsd:string"/>
<xsd:element name="f-stop" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Olympus.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.pentax.com"
xmlns="http://www.pentax.com"
elementFormDefault="qualified">
<xsd:complexType name="manual_adapter_type">
<xsd:sequence>
<xsd:element name="speed" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Pentax.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.camera.org"
xmlns:nikon="http://www.nikon.com"
xmlns:olympus="http://www.olympus.com"
xmlns:pentax="http://www.pentax.com"
elementFormDefault="qualified">
<xsd:import namespace="http://www.nikon.com"
schemaLocation="Nikon.xsd"/>
<xsd:import namespace="http://www.olympus.com"
schemaLocation="Olympus.xsd"/>
<xsd:import namespace="http://www.pentax.com"
schemaLocation="Pentax.xsd"/>
<xsd:element name="camera">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="body" type="nikon:body_type"/>
<xsd:element name="lens" type="olympus:lens_type"/>
<xsd:element name="manual_adapter" type="pentax:manual_adapter_type"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:schema>
Camera.xsd
These import
elements give
us access to
the components
in these other
schemas.
Here the
Body type
Is defined in the Nikon
namespace
<?xml version="1.0"?>
<c:camera xmlns:c="http://www.camera.org"
xmlns:nikon="http://www.nikon.com"
xmlns:olympus="http://www.olympus.com"
xmlns:pentax="http://www.pentax.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.camera.org
Camera.xsd
http://www.nikon.com
Nikon.xsd
http://www.olympus.com
Olympus.xsd
http://www.pentax.com
Pentax.xsd">
<c:body>
<nikon:description>Ergonomically designed casing for easy handling</nikon:description>
</c:body>
<c:lens>
<olympus:zoom>300mm</olympus:zoom>
<olympus:f-stop>1.2</olympus:f-stop>
</c:lens>
<c:manual_adapter>
<pentax:speed>1/10,000 sec to 100 sec</pentax:speed>
</c:manual_adapter>
</c:camera>
The Camera
instance
uses elements
from the Nikon,
Olympus, and
Pentax
namespaces.
Camera.xml
Redundant!
On the previous slide, the value of schemaLocation
contained four pairs of values - one for camera, and
three for each schema that it uses. The later three
are redundant. Once you give the schema-validator
the URL to the camera schema it will examine the
camera schema and see the import elements, thus it
will deduce the other schemas being used (Nikon,
Olympus, and Pentax)
The next slide shows the non-redundant version.
<?xml version="1.0"?>
<c:camera xmlns:c="http://www.camera.org"
xmlns:nikon="http://www.nikon.com"
xmlns:olympus="http://www.olympus.com"
xmlns:pentax="http://www.pentax.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.camera.org
Camera.xsd">
<c:body>
<nikon:description>Ergonomically designed casing for easy handling</nikon:description>
</c:body>
<c:lens>
<olympus:zoom>300mm</olympus:zoom>
<olympus:f-stop>1.2</olympus:f-stop>
</c:lens>
<c:manual_adapter>
<pentax:speed>1/10,000 sec to 100 sec</pentax:speed>
</c:manual_adapter>
</c:camera>
Camera.xml (non-redundant version)
Note about using Include and Import
• The <include> and <import> elements must
come before any element declarations or type
definitions.
Questions?
XML Schema.pptx

XML Schema.pptx

  • 1.
    XML Schema Mani BushanD’souza mail2mani@email.com
  • 2.
    Topics Covered What isSchema How to use the XML Schema language in applications
  • 3.
  • 4.
    What is it? AnXML 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 Schemasare extensible. Schemas have more depth and power. Schemas are written in XML. Schemas support data types. Schemas support namespaces.
  • 7.
    Document Type Definition AnInformation 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 ModelingSyntax 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 Differencesin data typing and syntax DTD <!ELEMENT Age (#PCDATA)> XML Schema <element name=“Age” type=“integer”/> XML <Age>2009</Age>
  • 10.
    Why Support DataTypes? 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 canbe 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 DTDsuse their own unique syntax (SGML) XML Schema uses XML syntax DTDs are concise XML Schema is verbose
  • 13.
    Comparison with DTDs XMLSchema 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 XMLSchema 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 Schemasare Extensible  Reuse Schemas in other Schemas.  Create proprietary data types from public types.  Reference multiple Schemas in one document.
  • 16.
    Comparison with DTDs XMLSchema 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 ofchecking 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 SchemasDTDs 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? DTDshave: 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 XMLand 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 XMLDocument Schema Error message Schema Processor Valid Invalid
  • 23.
    XSD (XML SchemaDefinition) 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 <?xmlversion="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> elementexplained 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 Sincethe 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:elementname="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:elementname="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:elementname="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:elementname="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
  • 32.
  • 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:elementname="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
  • 34.
  • 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:elementname="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:elementname="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 idOptional. 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 schemain 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
  • 39.
  • 40.
    Referencing a schemain 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 toa 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 toan 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 toan 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 withSchemas 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 Inthe 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"> <elementname="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"> <elementname="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 thetargetNamespace 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 DataModel 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 ofSchema 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 SchemaComponents 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 XMLNamespaces 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 DeclarationComponents  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 Adefinition 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 InstanceDocument 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 TypeDefinitions? 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 simpleelement 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 Simpleelement using Built-in Simple Types <xsd:element name=“weight” type=“xsd:string”/> <xsd:element name=“population” type=“xsd:integer” />
  • 64.
    Simple Elements Example:  <lastname>AmirKhan</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 andFixed 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 Schemahas 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 simpletype 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 simpletype 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 DescriptionExample(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 simpletype 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-inSimple 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 Acomplex 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 complexelements 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 Definea 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 Example1 <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 bedeclared 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 orcomplexType 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 Attributesare 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:attributename=“size” type=“xsd:integer” default=“12”> Attribute: <xsd:attribute ref=“size” default=“12” use=“optional”>
  • 81.
    Example: Attribute declaration <elementname=“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 cannothave 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 andFixed 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 Simpletypes  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 Typesare 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 Twokinds 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.
  • 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 TimeData 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
  • 91.
  • 92.
    Built-in Datatypes (cont.) PrimitiveDatatypes  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 usedto define acceptable values for XML elements or attributes. When used with XML elements they are known as “facets”.
  • 96.
    Creating your ownDatatypes 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 ofCreating 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 Definesa 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 Definesthe 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 Creatinga 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 ownDatatypes 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 facetcontrols 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 mostpowerful 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 Schemahas 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 Thebackward 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 generalone 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 otherescape 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 simplestpatterns 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 escapesD, S, I, C and W are negative forms, e.g. D matches any character except a decimal digit.
  • 112.
    Category Escapes A largeand 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 Allowyou 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 featureof 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 Anexample 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 isa 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 Chapterd 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, mare 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.) RegularExpression  (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 DerivedSimple 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 – PasswordExample <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 inRestriction 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 to99 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:simpleTypename="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} Aletter, 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 facetallows 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 DerivedSimple 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 restrictsthe 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:restrictionbase="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:simpleTypename=“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 inRestriction 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 Thefacets 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 theinteger 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 TypeExample (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 TypeExample (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 TypeExample (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 TypeExample (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:simpleTypename="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 FacetValue 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:restrictionbase="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"> SamplePurchase 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 definea 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 ListType 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 Typewith 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 Typewith 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 Typewith 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 thelist 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:unionelement 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 simpleTypethat 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 creatingUnion 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 forZipcodes <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:minInclusivevalue="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 twosimple 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 twosimple 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 SchemaDerivations 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 Typesdefine 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 – complextype <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 differentways  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 : Defininga 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 froma 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 elementsmay 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 mixedwith 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:complexTypemixed="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 Schemadoesn’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 specifyEmpty 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 Thereare 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.
  • 183.
    Restriction A restriction ofa 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 ofa 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 restrictsa 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 andSimple 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 andSimple Content Simple Type Complex Type Simple Content Complex Content Type allows attributes or elements? allows elements in content?
  • 188.
    Simple contents A goodexample 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 contentscontain 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:elementname="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 includingchildren 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 ofComplex 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 BaseType 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 SimpleTypes Simple Content Complex Content extension restriction restriction restriction Complex Types restriction list union restriction extension restriction extension restriction
  • 195.
    Defining a ComplexType 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 TypesIndicators 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 Wherewe 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/> modelgroup 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 seenan 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:elementname="title" minOccurs="0"/> <xsd:element name="firstname" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="lastname"/> </xsd:sequence> </xsd:complexType>
  • 203.
    Choice A <xsd:choice/> modelgroup also contains a series of particles, with the same options as for sequence. The element information validated by this model should match exactly one of the particles in the choice. E.g. <xsd:choice minOccurs="0" maxOccurs="unbounded"/> <xsd:element ref="paragraph"> <xsd:sequence> <xsd:element ref="figure"/> <xsd:element ref="caption"/> <xsd:sequence> </xsd:choice>
  • 204.
    xsd:Choice Choice : Oneof them is allowed (PRODUCT, NUMBER, (PRICE | CHARGEACCT | SAMPLE)) <xsd:complexType name="itemType"> <xsd:sequence> <xsd:element ref="PRODUCT"/> <xsd:element ref="NUMBER"/> <xsd:choice> <xsd:element ref="PRICE"/> <xsd:element ref="CHARGEACCT"/> <xsd:element ref="SAMPLE"/> </xsd:choice> </xsd:sequence> </xsd:complexType>
  • 205.
    xsd:Choice In DTD syntax:<!ELEMENT name ((First Last) | (Last, First)) > <xs:complexType name="name.type"> <xs:choice> <xs:sequence> <xs:element ref=“Frst"/> <xs:element ref=“Last"/> </xs:sequence> <xs:sequence> <xs:element ref=“Last"/> <xs:element ref=“First"/> </xs:sequence> </xs:choice> </xs:complexType> <xs:element name="First" type="xs:string"/> <xs:element name="Last" type="xs:string"/> <xs:element name="name" type="name.type"/> Validates both <name><First>Shakti</Frst><Last>Kapoor</Last></name> and <name><Last>Kapoor</Last><First>Shakti</First></name>
  • 206.
    All The <xsd:all/> modelgroup is peculiar to XML Schema. All particles it contains must be <xsd:element/>s. The element information validated should match a sequence of the particles in any order. There are several constraints:  The maxOccurs attribute of each particle must be 1.  The minOccurs attribute of each particle must be 0 or 1.  The <xsd:all/> model group can only occur at the top level of a complex type’s content model, and must itself have minOccurs = maxOccurs = 1.
  • 207.
    xs:all xs:all allows elementsto appear in any order <xs:element name="person"> <xs:complexType> <xs:all> <xs:element name="firstName" type="xs:string" /> <xs:element name="lastName" type="xs:string" /> </xs:all> </xs:complexType> </xs:element>
  • 208.
    Element Wildcard The elementwildcard particle <xsd:any/> matches and validates any element in the instance document. E.g. <xsd:sequence minOccurs="0“ maxxOccurs="unbounded"> <xsd:element ref=“header"/> <xsd:any/> </xsd:sequence> matches a sequence of consecutive pairs of elements, where the first element in each pair is a header, and the second can be any kind of element.
  • 209.
    Options on <xsd:any/> The<xsd:any/> element takes the usual optional maxOccurs, minOccurs attributes. Allows a namespace attribute taking one of the values:  ##any (the default),  ##other (any namespace except the target namespace),  List of namespace names, optionally including either ##targetNamespace or ##local. Controls what elements the wildcard matches, according to namespace. It also allows a processContents attribute taking one of the values strict, skip, lax (default strict), controlling the extent to which the contents of the matched element are validated.
  • 210.
    Specifying the Namespaceof Extension Elements <any namespace="##other"/> allows the instance document to contain a new element, provided the element comes from a namespace other than the one the schema is defining (i.e., targetNamespace). <any namespace="http://www.somewhere.com"/> allows a new element, provided it's from the specified namespace Note: you can specify a list of namespaces, separated by a blank space. One of the namespaces can be ##targetNamespace (see next) <any namespace="##targetNamespace"/> allows a new element, provided it's from the namespace that the schema is defining. <any namespace="##any"/> allows an element from any namespace. This is the default. <any namespace="##local"/> the new element must come from no namespace
  • 211.
    Process Contents <xs:complexType name=“bodyType”> ... <xsdsequence> <xs:anyprocessContents=“skip”/> </xs:sequence> ... </xs:complexType>
  • 212.
  • 213.
  • 214.
    Declaring Attributes An attributeis declared by using the xsd:attribute schema element. Like elements, attributes must have a name and type An attribute always has a simple type. Attributes can use custom data types Attributes can be restricted in regard to cardinality or default values Attributes can refer to other attribute definitions <xsd:attribute name = “country” type = “xsd:string” fixed = “India”/>
  • 215.
    Defining an attribute Attributesthemselves 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  default and fixed are mutually exclusive.  use="optional" the attribute is not required (default)  use="required" the attribute must be present
  • 216.
    Declaring Attributes We canuse a built-in simple types to an attribute by assigning the type’s name to the type attribute of the xsd:attribute element. <xsd:attribute name=”IndexPages” type=”xsd:positiveInteger”/>
  • 217.
    Attributes with constraints <xsd:attributename=”InStock” type=”xsd:boolean” use=”required”/> <xsd:attribute name=”PartNum” type=”xsd:string” default=”0-00-0"/> <xsd:attribute name=”Priority” type=”xsd:string” fixed=”high”/>
  • 218.
    Declaring Attributes We canalso define a new simple type by including the xsd:simpleType schema element within the xsd:attribute element. <xsd:attribute name=”Type”> <xsd:simpleType> <xsd:restriction base=”xsd:string”> <xsd:enumeration value=”Comdey”/> <xsd:enumeration value=”Tragedy”/> <xsd:enumeration value=”Documentary”/> </xsd:restriction> </xsd:simpleType> </xsd:attribute>
  • 219.
    Simple Content plusAttributes Here is a declaration of an anchor element that allows simple content plus an href attribute. <xsd:element name="anchor"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="xsd:string”> <xsd:attribute name="href" type="xsd:anyURI"/> <xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element>
  • 220.
    Text element withattributes If a text element has attributes, it is no longer a simple type  <xs:element name="population"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:integer"> <xs:attribute name="year" type="xs:integer"> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element>
  • 221.
    Empty element witha attribute <xs:complexType name="counter"> <xs:complexContent> <xs:extension base="xs:anyType"/> <xs:attribute name="count" type="xs:integer"/> </xs:complexContent> </xs:complexType>
  • 222.
    Simple Attribute Declarations Usingattribute declaration within a complex element. <xsd:element name="figure"> <xsd:complexType> <xsd:attribute name="source" type="xsd:string"/> </xsd:complexType> </xsd:element>
  • 223.
    Attributes and ComplexTypes from Simple Types DTD: <!ELEMENT greeting (#PCDATA)> <!ATTLIST greeting language CDATA "English"> XML Schema: <xsd:element name="greeting"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="xsd:string"> <xsd:attribute name="language" type="xsd:string“ default=“English” /> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element> XML Instance Document: <greeting language="Spanish"> Hola! </greeting>
  • 224.
    Complex Content PlusAttributes Here is a declaration of a body element that allows mixed content plus a style attribute. <xsd:element name="body"> <xsd:complexType mixed="true"> <xsd:choice minOccurs="0" maxOccurs="unbounded"> <element ref="p"/> <element ref="a"/> </xsd:choice> <xsd:attribute name="style" type="xsd:string"/> </xsd:complexType> </xsd:element>
  • 225.
    Adding Attributes toan Element with Element, Mixed, or Empty Content <xsd:element name=”FILM”> <xsd:complexType> <xsd:sequence> <xsd:element name=”TITLE” type=”xsd:string”/> <xsd:choice> <xsd:element name=”STAR” type=”xsd:string”/> <xsd:element name=”SongName” type=”xsd:string”/> <xsd:element name=”Producer” type=”xsd:string”/> </xsd:choice> </xsd:sequence> <xsd:attribute name=”Class”> <xsd:simpleType> <xsd:restriction base=”xsd:string”> <xsd:enumeration value=”fictional”/> <xsd:enumeration value=”documentary”/> <xsd:enumeration value=”instructional”/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> </xsd:complexType> </xsd:element>
  • 226.
    Attribute Wildcards An attributewildcard is represented by an <xsd:anyAttribute/> element. There can be at most one such element in a complex type definition, and it must appear after any normal attribute declarations. Such an declaration allows any attribute, optionally limited by namespace. The namespace and processContents attributes on <xsd:anyAttribute/> work as for <xsd:any/>.
  • 227.
    <anyAttribute> Example <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:elementname="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> <xs:anyAttribute/> </xs:complexType> </xs:element>
  • 228.
    <anyAttribute> Example <?xml version="1.0"encoding="ISO-8859-1"?> <xs:schema …> <xs:attribute name="gender“> <xs:simpleType> <xs:restriction base="xs:string“> <xs:pattern value="male|female"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:schema>
  • 229.
    <?xml version="1.0" encoding="ISO-8859-1"?> <persons…> <person gender="female“> <firstname>Karishma</firstname> <lastname>Kapoor</lastname> </person> <person gender="male“> <firstname>Nana</firstname> <lastname>Patekar</lastname> </person> </persons> <anyAttribute> Example
  • 230.
    Attributes and Namespaces Bydefault, attributes (declared inside an <xsd:complexType/>) do not become part of the target namespace.  Instead these attributes are local properties of any element they are attached to. The element itself may or may not belong to a namespace. In instance documents, names of these attributes must not be prefixed with a namespace prefix.
  • 231.
    Creating Attributes ina Namespace There are three ways to put attributes into the target namespace:  Declare them “globally”, directly inside the top level <xsd:schema/> element. Reference the attribute declaration inside the complex type definition (like element references), or  specify the attribute form="qualified" on a local <xsd:attribute/> declaration, or  specify the attribute attributeFormDefault="qualified" on the <xsd:schema/> element. After this, these attributes must be prefixed in instance documents with a namespace prefix.  Recall default namespace declarations (xmlns="namespace") don’t work for attributes: you must introduce a non-empty prefix.
  • 232.
    Explicit Type vs.Implicit Type Explicit type  One in which a name is given to the type  Element that uses the type is generally defined in a different section of the file  Object-oriented in that same explicit type is used as the type for several different elements Implicit type (nameless type)  Use when the type is not needed by multiple elements
  • 233.
    Example of ExplicitType <!-- Type has a name zipUnion --> <xsd:simpleType name="zipUnion"> <xsd:union memberTypes="State listOfMyIntType"/> </xsd:simpleType> <!-- zipUnion type is used in other parts of Schema document --> <element name=zips type=“zipUnion”> … <element name=theOtherZips type=“zipUnion”> … <element name=theThirdZips type=“zipUnion”>
  • 234.
    Anonymous User-Defined SimpleType <xsd:element name="elevation"> <xsd:simpleType> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="-1290"/> <xsd:maxInclusive value="29035"/> </xsd:restriction> </xsd:simpleType> </xsd:element> The simpleType definition is defined inline, it is an anonymous simpleType definition. The disadvantage of this approach is that this simpleType may not be reused by other elements.
  • 235.
    Example of ImplicitType <xsd:complexType name="Items"> <!– Explicit complexType --> <xsd:sequence> <xsd:element name="item" minOccurs="0" maxOccurs="unbounded"> <xsd:complexType> <!-- Implicit complexType --> <xsd:sequence> <xsd:element name="productName" type="xsd:string"/> <xsd:element name="quantity"> <xsd:simpleType> <!-- Implicit 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"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType>
  • 236.
    Types Simple vs. Complex Simpletypes Complex types Can be either: global and named, or local and anonymous
  • 237.
    Local and Globaldeclarations Global  Declared at top level of a schema, just immediately below xsd:schema element. They are available to be used throughout rest of the schema.  Global element declaration do not determine where an element can appear in the XML document – they only determine what the element will look like.  A global element declaration must be explicitly referenced in order to have it appear in a corresponding XML document. Local  Locally declared elements. Limited to the complex type definition in which they are declared and may not be used elsewhere in the schema.  Such locally declared elements are automatically referenced.
  • 238.
    Global and localdefinitions Elements declared at the “top level” of a <schema> are available for use throughout the schema Elements declared within a xs:complexType are local to that type Thus, in <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> the elements firstName and lastName are only locally declared The order of declarations at the “top level” of a <schema> do not specify the order in the XML data document
  • 239.
    Declaration and use Touse a type we have declared, use it as the value of type="..."  Examples:  <xs:element name="student" type="person"/>  <xs:element name="professor" type="person"/>  Scope is important: you cannot use a type if is local to some other type
  • 240.
    Local and GlobalTypes in XML Schema Local type: <xsd:element name=“person”> [define locally the person’s type] </xsd:element> Global type: <xsd:element name=“person” type=“ttt”/> <xsd:complexType name=“ttt”> [define here the type ttt] </xsd:complexType> Global types: can be reused in other elements
  • 241.
    Local Names inXML-Schema <xsd:element name=“person”> <xsd:complexType> . . . . . <xsd:element name=“name”> <xsd:complexType> <xsd:sequence> <xsd:element name=“firstname” type=“xsd:string”/> <xsd:element name=“lastname” type=“xsd:string”/> </xsd:sequence> </xsd:element> . . . . </xsd:complexType> </xsd:element> <xsd:element name=“product”> <xsd:complexType> . . . . . <xsd:element name=“name” type=“xsd:string”/> </xsd:complexType> </xsd:element> name has different meanings in person and in product
  • 242.
    What is ref? ref is use to reference an existing element or attribute rather than declaring a new element or attribute Existing element must be global element - an element that is declared under root element
  • 243.
    ref Example <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <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>
  • 244.
    <xs:element name="purchaseOrder"> <xs:complexType> <xs:sequence> <xs:element ref="recipient"/> <xs:element ref="order" /> </xs:sequence> <xs:attribute name="orderDate" type="xs:string" /> </xs:complexType> </xs:element> Example: Using refs - 1
  • 245.
    <xs:element name ="recipient"> <xs:complexType> <xs:sequence> <xs:element ref="name" /> <xs:element ref="street" /> <xs:element ref="city" /> <xs:element ref="state" /> <xs:element ref="postalCode" /> </xs:sequence> <xs:attribute name="country" type="xs:string" /> </xs:complexType> </xs:element> Example: Using refs - 2
  • 246.
    <xs:element name ="name" type="xs:string" /> <xs:element name = "street" type="xs:string" /> <xs:element name = "city" type="xs:string" /> <xs:element name = "state" type="xs:string" /> <xs:element name = "postalCode" type="xs:short" /> <xs:element name = "order"> <xs:complexType> <xs:sequence> <xs:element ref="cd" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> Example: Using refs - 3
  • 247.
    <xs:element name="cd"> <xs:complexType> <xs:attribute name="artist"type="xs:string" /> <xs:attribute name="title" type="xs:string" /> </xs:complexType> </xs:element> </xs:schema> Example: Using refs - 4
  • 248.
    Referencing Once you havedefined an element or attribute (with name="..."), you can refer to it with ref="..." Example:  <xs:element name="person"> <xs:complexType> <xs:all> <xs:element name="firstName" type="xs:string" /> <xs:element name="lastName" type="xs:string" /> </xs:all> </xs:complexType> </xs:element>  <xs:element name="student" ref="person">  Or just: <xs:element ref="person">
  • 249.
    References to Elements: DTD: <!ELEMENTBOOK (P*)> <!ELEMENT P (#PCDATA)> XML Schema <xsd:element name="P" type="xsd:string"/> <xsd:element name="BOOK"> <xsd:complexType> <xsd:sequence> <xsd:element ref="P" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element>
  • 250.
    Secondary Schema Components ModelGroup Definitions Attribute Group Definitions Identity-constraint Definitions  Similar to ID / IDREF Notations Declarations Wildcards  Similar to a DTD with “ANY” Annotations
  • 251.
    Group Element The groupelement enables you to group together element declarations. Note: the group element is just for grouping together element declarations, no attribute declarations allowed!
  • 252.
    Named Model Groups Anamed model group is a collection, or group, of elements. The syntax for creating a model group is <xs:group name="name"> elements </xs:group> Where name is the name of the model group, and elements is a collection of element declarations
  • 253.
    <xsd:element name="Book" > <xsd:complexType> <xsd:sequence> <xsd:groupref="PublicationElements"/> <xsd:element name="ISBN" type="string"/> <xsd:element name="Reviewer" type="string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="CD" > <xsd:complexType> <xsd:sequence> <xsd:group ref="PublicationElements"/> <xsd:element name="RecordingStudio" type="string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:group name="PublicationElements"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:string"/> </xsd:sequence> </xsd:group> An example showing the use of the <group> element
  • 254.
    Note about group Groupdefinitions must be global <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:group name="PublicationElements"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" minOccurs="0"/> <xsd:element name="Author" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:string"/> </xsd:sequence> </xsd:group> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> ... </xsd:complexType> </xsd:element> Cannot inline the group definition. Instead, you must use a ref here and define the group globally.
  • 255.
    Named Attribute Groups Attributescan be grouped into collections called named attribute groups. This is particularly useful for attributes that you want to use with several different elements in a schema. The syntax for a named attribute group is <xs:attributeGroup name="name"> attributes </xs:attributeGroup> Where name is the name of the attribute group and attributes is a collection of attributes assigned to the group.
  • 256.
    Attribute Groups Promotes logicalorganization Encourages reuse – defined once, referenced many times Facilitates maintenance Improves Schema readability Must be unique within an XML Schema Referenced from complexType definitions
  • 257.
    Declaring Attribute Groups <!--Define the unique group: --> <attributeGroup name = “CreditCardInfo” > <attribute name = “CardNumber” type = “integer” use = “required” /> <attribute name = “ExpirationDate” type = “date” use = “required” /> <attribute name = “CardHolder” type = “FullName” use = “required” /> </attributeGroup> <!-- Then you can reference it from a complexType: --> <complexType name = “CreditInformation” > <attributeGroup ref = “CreditCardInfo” /> </complexType>
  • 258.
    Attribute Groups DTD: <!ELEMENT imgEMPTY> <!ATTLIST img alt CDATA #REQUIRED src CDATA #REQUIRED width CDATA #IMPLIED height CDATA #IMPLIED> XML Schema: <xsd:attributeGroup name="imgAttributes"> <xsd:attribute name="alt" type="xsd:string" use="required"/> <xsd:attribute name="src" type="xsd:NMTOKEN" use="required"/> <xsd:attribute name="width" type="xsd:integer"/> <xsd:attribute name="height" type="xsd:integer"/> </xsd:attributeGroup> <xsd:element name="img"> <xsd:complexType> <xsd:attributeGroup ref="imgAttributes"/> <xsd:complexType> </xsd:element>
  • 259.
    Example of attributeGroup <xsd:attributeGroupname="ItemDelivery"> <xsd:attribute name="partNum" type="SKU"/> <xsd:attribute name="weightKg" type="xsd:decimal"/> <xsd:attribute name="shipBy"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="air"/> <xsd:enumeration value="land"/> <xsd:enumeration value="any"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> </xsd:attributeGroup> <!-- attributeGroup replaces individual declarations --> <xsd:attributeGroup ref="ItemDelivery"/>
  • 260.
    Identity Constraints Recall thatthe attribute types ID and IDREF imply interesting constraints on values of those attributes:  Within any individual XML document, every attribute of type ID must be specified with a different value from every other attribute of type ID.  The value of any attribute of type IDREF must be the same as the value of an attribute of type ID specified somewhere in the same document.
  • 261.
    Identity Constraints <xs:element name="author"> <xs:complexType> <xs:sequence> <xs:elementname="name" type="xs:string"/> <xs:element name="born" type="xs:date"/> </xs:sequence> <xs:attribute name="id" type="xs:ID"/> </xs:complexType> </xs:element>
  • 262.
    IDREF Constraints Relating Personand Book using IDREF <xsd:element name="Person"> <xsd:complexType> <xsd:sequence> <xsd:element name="Name" type="xsd:string"/> </xsd:sequence> <xsd:attribute name="id" type="xsd:ID" use="required"/> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author"> <xsd:complexType> <xsd:attribute name=“PersonID" type="xsd:IDREF" use="required"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element>
  • 263.
    IDREF Constraints Relating Personand Book using IDREF <xsd:element name="Name" type="xsd:string"/> <xsd:attribute name="id" type="xsd:ID"/> <xsd:element name="Person"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Name"/> </xsd:sequence> <xsd:attribute ref="id" use="required"/> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> <xsd:attribute name=“PID" type="xsd:IDREF"/> <xsd:element name="Author"> <xsd:complexType> <xsd:attribute ref=“PID" use="required"/> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Title"/> <xsd:element ref="Author"/> </xsd:sequence> </xsd:complexType> </xsd:element>
  • 264.
    Annotation The annotation elementprovides a mechanism for documenting most other schema elements. Unlike an XML comment, which looks like <!-- comment text -->, the annotation is part of the schema component. An annotation provides for human documentation in one or more languages, as well as programmatic documentation, such as meaningful URIs
  • 265.
    Annotation Appears at thebeginning of most schema constructions Can have two sub-elements  Documentation  appInfo Documentation  For human readable materials appInfo  For tools, stylesheets and other applications
  • 266.
    XML-Schema : Annotations XML-schemaprovides several tags for annotating a schema: documentation (intended for human readers), appInfo (intended for applications) and annotation. This mixed content model allows the inclusion of almost any content, such as text: <xs:element name="author" type="author"> <xs:annotation> <xs:documentation xml:lang="en"> The author of a book. </xs:documentation> <xs:documentation xml:lang="fr"> Designe l'auteur d'un livre. </xs:documentation> </xs:annotation> </xs:element>
  • 267.
    XML-Schema : Annotations <xs:simpleTypename=“IndianStates"> <xs:annotation> <xs:documentation>States in India</xs:documentation> </xs:annotation> <xs:restriction base="xs:string"> <xs:enumeration value='KA'> <xs:annotation> <xs:documentation>Karnataka</xs:documentation> </xs:annotation> </xs:enumeration> <xs:enumeration value=‘KL'/> <xs:annotation> <xs:documentation>Kerala</xs:documentation> </xs:annotation> </xs:enumeration> </xs:restriction> </xs:simpleType>
  • 268.
    Example of Annotation <xsd:elementname="internationalPrice"> <xsd:annotation> <xsd:documentation> element declared with anonymous type </xsd:documentation> </xsd:annotation> <xsd:complexType> <xsd:annotation> <xsd:documentation> empty anonymous type with 2 attributes </xsd:documentation> </xsd:annotation> <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>
  • 269.
    XML-Schema : AppInfo Annotationsare also a good container for application-specific metadata, such as those used by the schema for schema to describe the list of facets and properties of its primitive datatypes:
  • 270.
    XML-Schema : AppInfo <xs:simpleTypename="string" id="string"> <xs:annotation> <xs:appinfo> <hfp:hasFacet name="length"/> <hfp:hasFacet name="minLength"/> <hfp:hasFacet name="maxLength"/> <hfp:hasFacet name="pattern"/> <hfp:hasFacet name="enumeration"/> <hfp:hasFacet name="whiteSpace"/> <hfp:hasProperty name="ordered" value="false"/> <hfp:hasProperty name="bounded" value="false"/> <hfp:hasProperty name="cardinality" value="countably infinite"/> <hfp:hasProperty name="numeric" value="false"/> </xs:appinfo> <xs:documentation source="http://www.w3.org/TR/xmlschema-2/#string"/> </xs:annotation>
  • 271.
    No targetNamespace (noNamespaceSchemaLocation) Sometimes youmay wish to create a schema but without associating the elements with a namespace. The targetNamespace attribute is actually an optional attribute of <schema>. Thus, if you don’t want to specify a namespace for your schema then simply don’t use the targetNamespace attribute. Consequences of having no namespace  1. In the instance document don’t namespace qualify the elements.  2. In the instance document, instead of using schemaLocation use noNamespaceSchemaLocation.
  • 272.
    Note that thereis no targetNamespace attribute, and note that there is no longer a default namespace. <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Title"/> <xsd:element ref="Author"/> <xsd:element ref="Date"/> <xsd:element ref="ISBN"/> <xsd:element ref="Publisher"/> </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>
  • 273.
    <?xml version="1.0"?> <BookStore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="BookStore.xsd"> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <ISBN>1-56592-235-2</ISBN> <Publisher>McMillin Publishing</Publisher> </Book> … </BookStore> 1. Note that there is no default namespace declaration. So, none of the elements are associated with a namespace. 2. Note that we do not use xsi:schemaLocation (since it requires a pair of values - a namespace and a URL to the schema for that namespace). Instead, we use xsi:noNamespaceSchemaLocation.
  • 274.
    Assembling an InstanceDocument from Multiple Schema Documents An instance document may be composed of elements from multiple schemas. Validation can apply to the entire XML instance document, or to a single element.
  • 275.
    <?xml version="1.0"?> <Library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.book.org Book.xsd http://www.employee.org Employee.xsd"> <Books> <Bookxmlns="http://www.book.org"> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <ISBN>1-56592-235-2</ISBN> <Publisher>Macmillan Publishing</Publisher> </Book> <Book xmlns="http://www.book.org"> <Title>Illusions: The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </Book> <Book xmlns="http://www.book.org"> <Title>The First and Last Freedom</Title> <Author>J. Krishnamurti</Author> <Date>1954</Date> <ISBN>0-06-064831-7</ISBN> <Publisher>Harper &amp; Row</Publisher> </Book> </Books> <Employees> <Employee xmlns="http://www.employee.org"> <Name>John </Name> <SSN>123-45-6789</SSN> </Employee> <Employee xmlns="http://www.employee.org"> <Name>Smith</Name> <SSN>000-11-2345</SSN> </Employee> </Employees> </Library> Validating against two schemas The <Book> elements are defined in Book.xsd, and the <Employee> elements are defined in Employee.xsd. The <Library>, <Books>, and <Employees> elements are not defined in any schema! 1. A schema validator will validate each Book element against Book.xsd. 2. It will validate each Employee element against Employee.xsd. 3. It will not validate the other elements.
  • 276.
    Lax Validation vs StrictValidation On the previous slide there were elements (Library, Books, and Employees) for which there was no schema to validate against. Lax validation is where the schema validator skips over elements for which no schema is available. Strict validation is where the schema validator requires validation of every element Most of the validators do strict validation. Consequently, they will reject the instance document on the previous slide.
  • 277.
    Assembling a Schemafrom Multiple Schema Documents The include element allows you to access components in other schemas  All the schemas you include must have the same namespace as your schema (i.e., the schema that is doing the include)  The net effect of include is as though you had typed all the definitions directly into the containing schema <xsd:schema …> <xsd:include schemaLocation="LibraryBook.xsd"/> <xsd:include schemaLocation="LibraryEmployee.xsd"/> … </xsd:schema> LibraryBook.xsd LibraryEmployee.xsd Library.xsd
  • 278.
    <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.library.org" xmlns="http://www.library.org" elementFormDefault="qualified"> <xsd:includeschemaLocation="LibraryBook.xsd"/> <xsd:include schemaLocation="LibraryEmployee.xsd"/> <xsd:element name="Library"> <xsd:complexType> <xsd:sequence> <xsd:element name="Books"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Employees"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Employee" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> Library.xsd These are referencing element declarations in the other schemas.
  • 279.
    Assembling a Schemafrom a Schema with no targetNamespace A schema can <include> another schema which has no targetNamespace. The included components take on the targetNamespace of the schema that is doing the <include>. This is called the Chameleon Effect. The components in the no-namespace schema are called Chameleon components.
  • 280.
    <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xsd:complexTypename="ProductType"> <xsd:sequence> <xsd:element name="Type" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> Product.xsd Note that this schema has no targetNamespace!
  • 281.
    <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.company.org" xmlns="http://www.company.org" elementFormDefault="qualified"> <xsd:includeschemaLocation="Person.xsd"/> <xsd:include schemaLocation="Product.xsd"/> <xsd:element name="Company"> <xsd:complexType> <xsd:sequence> <xsd:element name="Person" type="Person" maxOccurs="unbounded"/> <xsd:element name="Product" type="ProductType" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> Company.xsd This schema <include>s Product.xsd. Thus, the components in Product.xsd are namespace-coerced to the company targetNamespace. Consequently, we can reference those components just as though they had originally been declared in a schema with the same targetNamespace.
  • 282.
    Assembling a Schemafrom Multiple Schema Documents with Different Namespaces The import element allows you to access elements and types in a different namespace <xsd:schema …> <xsd:import namespace="A" schemaLocation="A.xsd"/> <xsd:import namespace="B" schemaLocation="B.xsd"/> … </xsd:schema> Namespace A A.xsd Namespace B B.xsd C.xsd
  • 283.
  • 284.
    <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.nikon.com" xmlns="http://www.nikon.com" elementFormDefault="qualified"> <xsd:complexTypename="body_type"> <xsd:sequence> <xsd:element name="description" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> Nikon.xsd <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.olympus.com" xmlns="http://www.olympus.com" elementFormDefault="qualified"> <xsd:complexType name="lens_type"> <xsd:sequence> <xsd:element name="zoom" type="xsd:string"/> <xsd:element name="f-stop" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> Olympus.xsd <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.pentax.com" xmlns="http://www.pentax.com" elementFormDefault="qualified"> <xsd:complexType name="manual_adapter_type"> <xsd:sequence> <xsd:element name="speed" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> Pentax.xsd
  • 285.
    <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.camera.org" xmlns:nikon="http://www.nikon.com" xmlns:olympus="http://www.olympus.com" xmlns:pentax="http://www.pentax.com" elementFormDefault="qualified"> <xsd:importnamespace="http://www.nikon.com" schemaLocation="Nikon.xsd"/> <xsd:import namespace="http://www.olympus.com" schemaLocation="Olympus.xsd"/> <xsd:import namespace="http://www.pentax.com" schemaLocation="Pentax.xsd"/> <xsd:element name="camera"> <xsd:complexType> <xsd:sequence> <xsd:element name="body" type="nikon:body_type"/> <xsd:element name="lens" type="olympus:lens_type"/> <xsd:element name="manual_adapter" type="pentax:manual_adapter_type"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:schema> Camera.xsd These import elements give us access to the components in these other schemas. Here the Body type Is defined in the Nikon namespace
  • 286.
    <?xml version="1.0"?> <c:camera xmlns:c="http://www.camera.org" xmlns:nikon="http://www.nikon.com" xmlns:olympus="http://www.olympus.com" xmlns:pentax="http://www.pentax.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.camera.org Camera.xsd http://www.nikon.com Nikon.xsd http://www.olympus.com Olympus.xsd http://www.pentax.com Pentax.xsd"> <c:body> <nikon:description>Ergonomicallydesigned casing for easy handling</nikon:description> </c:body> <c:lens> <olympus:zoom>300mm</olympus:zoom> <olympus:f-stop>1.2</olympus:f-stop> </c:lens> <c:manual_adapter> <pentax:speed>1/10,000 sec to 100 sec</pentax:speed> </c:manual_adapter> </c:camera> The Camera instance uses elements from the Nikon, Olympus, and Pentax namespaces. Camera.xml
  • 287.
    Redundant! On the previousslide, the value of schemaLocation contained four pairs of values - one for camera, and three for each schema that it uses. The later three are redundant. Once you give the schema-validator the URL to the camera schema it will examine the camera schema and see the import elements, thus it will deduce the other schemas being used (Nikon, Olympus, and Pentax) The next slide shows the non-redundant version.
  • 288.
    <?xml version="1.0"?> <c:camera xmlns:c="http://www.camera.org" xmlns:nikon="http://www.nikon.com" xmlns:olympus="http://www.olympus.com" xmlns:pentax="http://www.pentax.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.camera.org Camera.xsd"> <c:body> <nikon:description>Ergonomicallydesigned casing for easy handling</nikon:description> </c:body> <c:lens> <olympus:zoom>300mm</olympus:zoom> <olympus:f-stop>1.2</olympus:f-stop> </c:lens> <c:manual_adapter> <pentax:speed>1/10,000 sec to 100 sec</pentax:speed> </c:manual_adapter> </c:camera> Camera.xml (non-redundant version)
  • 289.
    Note about usingInclude and Import • The <include> and <import> elements must come before any element declarations or type definitions.
  • 290.