Transcript XML Schemas

Copyright © [2001]. Roger L. Costello. All Rights Reserved.
XML Schemas
http://www.w3.org/TR/xmlschema-0/ (Primer)
http://www.w3.org/TR/xmlschema-1/ (Structures)
http://www.w3.org/TR/xmlschema-2/ (Datatypes)
Roger L. Costello
XML Technologies Course
1
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Acknowledgements
• Special thanks to the following people for
their help in answering my unending
questions and/or for finding errors and
making suggestions:
–
–
–
–
–
Henry Thompson
Jonathan Rich
Francis Norton
Rick Jelliffe
Curt Arnold
2
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Viewing this Tutorial
• This tutorial is best viewed in slide show
mode
– Under the View menu select Slide Show
• Periodically you will see an icon at the
bottom, right of the slide indicating that it is
time to do a lab exercise. I strongly
recommend that you stop and do the lab
exercise to obtain the maximum benefit
from this tutorial.
3
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Schema Validators
• Command Line Only
– XSV by Henry Thompson
• ftp://ftp.cogsci.ed.ac.uk/pub/XSV/XSV11.EXE
• Has a Programmatic API
– xerces by Apache
• http://www.apache.org/xerces-j/index.html
– Oracle Schema Validator
• http://technet.oracle.com/tech/xml/schema_java/index.htm
• GUI Oriented
– XML Spy
• http://www.xmlspy.com
– XML Authority
• http://www.extensibility.com
4
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Purpose of XML Schemas (and
DTDs)
• Specify:
– the structure of instance documents
• "this element contains these elements, which
contains these other elements, etc"
– the datatype of each element/attribute
• "this element shall hold an integer within the range 0
to 12,000"
5
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Motivation for XML Schemas
• People are dissatisfied with DTDs
– It's a different syntax
• You write your XML (instance) document using one syntax
and the DTD using another syntax --> bad, inconsistent
– Limited datatype capability
• DTDs support a very limited capability for specifying
datatypes. You can't, for example, say "I want the <elevation>
element to hold an integer with a range of 0 to 12,000"
• Desire a set of datatypes compatible with those found in
databases
– DTD supports 10 datatypes; XML Schemas supports 41+
datatypes
6
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Highlights of XML Schemas
•
XML Schemas are a tremendous advancement over DTDs:
– Enhanced datatypes
• 41+ versus 10
• Can create your own datatypes
– Example: "This is a new type based on the string type and elements of
this type must follow this pattern: ddd-dddd, where 'd' represents a digit".
– Written in the same syntax as instance documents
• less syntax to remember
– Object-oriented'ish
• Can extend or restrict a type (derive new type definitions on the basis of old
ones)
– Can express sets, i.e., can define the child elements to occur in any order
– Can specify element content as being unique (keys on content) and uniqueness
within a region
– Can define multiple elements with the same name but different content
– Can define elements with null content
– Can define substitutable elements - e.g., the "subway" element is substitutable for
the "train" element.
7
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Let's Get Started!
• Convert BookCatalogue.dtd (next page) to
the XML Schema notation
– for this first example we will make a straight,
one-to-one conversion, i.e., Title, Author, Date,
ISBN, and Publisher will hold strings, just like
is done in the DTD
– We will gradually modify the schema to give
more custom types to these elements
8
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
BookCatalogue.dtd
<!ELEMENT BookCatalogue (Book)*>
<!ELEMENT Book (Title, Author, Date, ISBN, Publisher)>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Author (#PCDATA)>
<!ELEMENT Date (#PCDATA)>
<!ELEMENT ISBN (#PCDATA)>
<!ELEMENT Publisher (#PCDATA)>
9
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
10
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns="http://www.publishing.org"
elementFormDefault="qualified">
<xsd:element name="BookCatalogue">
<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" 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>
BookCatalogue.xsd (see example 01)
(explanations on
succeeding pages)
xsd = Xml-Schema Definition
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
11
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns="http://www.publishing.org"
elementFormDefault="qualified">
<xsd:element name="BookCatalogue">
<xsd:complexType>
<xsd:sequence>
<!ELEMENT BookCatalogue (Book)*>
<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" minOccurs="1" maxOccurs="1"/>
<!ELEMENT Book (Title, Author, Date,
<xsd:element ref="Author" minOccurs="1" maxOccurs="1"/>
ISBN, Publisher)>
<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"/>
<!ELEMENT Title (#PCDATA)>
<xsd:element name="Author" type="xsd:string"/>
<!ELEMENT Author (#PCDATA)>
<xsd:element name="Date" type="xsd:string"/>
<!ELEMENT Date (#PCDATA)>
<xsd:element name="ISBN" type="xsd:string"/>
<!ELEMENT ISBN (#PCDATA)>
<xsd:element name="Publisher" type="xsd:string"/>
<!ELEMENT Publisher (#PCDATA)>
</xsd:schema>
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns="http://www.publishing.org"
elementFormDefault="qualified">
<xsd:element name="BookCatalogue">
<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" 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>
12
All XML Schemas have
"schema" as the root
element.
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns="http://www.publishing.org"
elementFormDefault="qualified">
<xsd:element name="BookCatalogue">
<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" 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>
13
The elements that
are used to create
an XML Schema
come from the
XMLSchema
namespace
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
14
XMLSchema Namespace
http://www.w3.org/2000/10/XMLSchema
complexType
element
sequence
schema
string
integer
boolean
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns="http://www.publishing.org"
elementFormDefault="qualified">
<xsd:element name="BookCatalogue">
<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" 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>
15
Says that the
elements declared
in this schema
(BookCatalogue,
Book, Title,
Author, Date,
ISBN, Publisher)
are to go in this
namespace
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
16
Publishing Namespace (targetNamespace)
http://www.publishing.org (targetNamespace)
BookCatalogue
Author
Book
Title
Publisher ISBN
Date
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns="http://www.publishing.org"
elementFormDefault="qualified">
<xsd:element name="BookCatalogue">
<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" 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>
17
The default namespace is
http://www.publishing.org
which is the
targetNamespace!
This is referencing a
Book element declaration.
The Book in what
namespace? Since there
is no namespace qualifier
it is referencing the Book
element in the default
namespace, which is the
targetNamespace!
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns="http://www.publishing.org"
elementFormDefault="qualified">
<xsd:element name="BookCatalogue">
<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" 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>
18
This is a directive to
instance documents which
use this schema: Any
elements used by the
instance document which
were declared by this
schema must be namespace
qualified by the namespace
specified by
targetNamespace.
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Referencing a schema in an XML
instance document
<?xml version="1.0"?>
<BookCatalogue xmlns ="http://www.publishing.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.publishing.org
BookCatalogue.xsd">
<Book>
<Title>My Life and Times</Title>
<Author>Paul McCartney</Author>
<Date>July, 1998</Date>
<ISBN>94303-12021-43892</ISBN>
<Publisher>McMillin Publishing</Publisher>
</Book>
...
</BookCatalogue>
1. First, using a default namespace declaration, tell the schema-validator that all of the elements
used in this instance document come from the publishing namespace.
2. Second, with schemaLocation tell the schema-validator that the http://www.publishing.org
namespace is defined in BookCatalogue.xsd.
3. Third, tell the schema-validator that schemaLocation attribute we are using is the one in the
schema instance namespace.
19
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
20
Referencing a schema in an XML
instance document
schemaLocation="A
BookCatalogue.xsd"
BookCatalogue.xml
- uses elements from
namespace A
targetNamespace="A"
BookCatalogue.xsd
- defines elements in
namespace A
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
21
Note multiple levels of checking
BookCatalogue.xml
BookCatalogue.xsd
Validate that the xml document
conforms to the rules described
in BookCatalogue.xsd
XMLSchema.xsd
(schema-for-schemas)
Validate that BookCatalogue.xsd is a valid
schema document, i.e., it conforms
to the rules described in the
schema-for-schemas
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
22
Default Value for minOccurs and
maxOccurs
• The default value for minOccurs is "1"
• The default value for maxOccurs is "1"
<element ref="Title" minOccurs="1" maxOccurs="1"/>
Equivalent!
<element ref="Title"/>
Do Lab1
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
23
Qualify XMLSchema,
Default targetNamespace
• In the last example, we explicitly qualified all elements from the XML
Schema namespace. The targetNamespace was the default namespace.
http://www.w3.org/2000/10/XMLSchema
http://www.publishing.org
complexType
BookCatalogue
element
Author
annotation
documentation
Book
Title
Publisher ISBN
sequence
schema
Date
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
24
Default XMLSchema,
Qualify targetNamespace
• Alternatively (equivalently), we can design our schema so that
XMLSchema is the default namespace.
http://www.w3.org/2000/10/XMLSchema
http://www.publishing.org
complexType
BookCatalogue
element
Author
annotation
documentation
Book
Title
Publisher ISBN
sequence
schema
Date
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
25
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns:pub="http://www.publishing.org"
elementFormDefault="qualified">
<element name="BookCatalogue">
<complexType>
<sequence>
<element ref="pub:Book" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
<element name="Book">
<complexType>
<sequence>
<element ref="pub:Title"/>
<element ref="pub:Author"/>
<element ref="pub:Date"/>
<element ref="pub:ISBN"/>
<element ref="pub: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>
(see example01.1)
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns:pub="http://www.publishing.org"
elementFormDefault="qualified">
<element name="BookCatalogue">
<complexType>
<sequence>
<element ref="pub:Book" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
<element name="Book">
<complexType>
<sequence>
<element ref="pub:Title"/>
<element ref="pub:Author"/>
<element ref="pub:Date"/>
<element ref="pub:ISBN"/>
<element ref="pub: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>
26
Here we are
referencing a
Book element.
Where is that
Book element
defined? In
what namespace?
The pub: prefix
indicates what
namespace this
element is in. pub:
has been set to
be the same as the
targetNamespace.
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
27
"pub" References the
targetNamespace
http://www.w3.org/2000/10/XMLSchema
http://www.publishing.org (targetNamespace)
complexType
BookCatalogue
element
Author
annotation
documentation
Book
Title
Publisher ISBN
sequence
Date
schema
pub
Do Lab1.1
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Alternate Schema
• In the previous examples we declared an
element and then we ref’ed that element
declaration. Instead, we can inline the
element declarations.
• On the following slide is an alternate
(equivalent) way of representing the schema
shown previously, using inlined element
declarations.
28
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
29
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns="http://www.publishing.org"
elementFormDefault="qualified">
<xsd:element name="BookCatalogue">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
(see example 02)
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns="http://www.publishing.org"
elementFormDefault="qualified">
<xsd:element name="BookCatalogue">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
30
Anonymous types (no name)
(see example 02)
Do Lab 2
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Named Types
• The following slide shows an alternate
(equivalent) schema which uses a named
type.
31
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
32
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns="http://www.publishing.org"
elementFormDefault="qualified">
<xsd:element name="BookCatalogue">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" type="CardCatalogueEntry" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="CardCatalogueEntry">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
(see example 03)
Named type
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
33
Please note that
<xsd:element name="A" type="foo"/>
<xsd:complexType name="foo">
<xsd:sequence>
<xsd:element name="B" …/>
<xsd:element name="C" …/>
</xsd:sequence>
</xsd:complexType>
Element A references the
complexType foo.
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 has the
complexType definition
inlined in the element
declaration.
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
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="foo">
<xsd:complexType>
…
</xsd:complexType>
</xsd:element>
34
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
35
Summary of Declaring Elements
(two ways to do it)
1
<xsd:element name="name" type="type" minOccurs="int" maxOccurs="int"/>
A simple type
(e.g., xsd:string)
or the name of
a complexType
2
A nonnegative
integer
A nonnegative
integer or "unbounded"
Note: minOccurs and maxOccurs can only be used
in nested (local) element declarations.
<xsd:element name="name" minOccurs="int" maxOccurs="int">
<xsd:complexType>
…
</xsd:complexType>
</xsd:element>
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
36
Problem
• Defining the Date element to be of type string is
unsatisfactory (it allows any string value to be input as the
content of the Date element, including non-date strings).
We would like to constrain the allowable content that Date
can have. Modify the BookCatalogue schema to restrict
the content of the Date element to just date values
(actually, year values. See next two slides).
• Similarly, constrain the content of the ISBN element to
content of this form: ddddd-ddddd-ddddd or d-ddd-dddddd or d-dd-dddddd-d, where 'd' stands for 'digit'
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
The date Datatype
• A built-in datatype
• Elements declared to be of type date must follow this form: CCYYMM-DD
– range for CC is: 00-99
– range for YY is: 00-99
– range for MM is: 01-12
– range for DD is:
• 01-28 if month is 2
• 01-29 if month is 2 and the year is a leap year
• 01-30 if month is 4, 6, 9, or 11
• 01-31 if month is 1, 3, 5, 7, 8, 10, or 12
– Example: 1999-05-31 represents May 31, 1999
37
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
The year Datatype
• A built-in datatype
• Elements declared to be of type year must follow
this form: CCYY
– range for CC is: 00-99
– range for YY is: 00-99
– Example: 1999 indicates the year 1999
38
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
39
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns="http://www.publishing.org"
elementFormDefault="qualified">
<xsd:simpleType name="ISBNType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{5}-\d{5}-\d{5}"/>
<xsd:pattern value="\d{1}-\d{3}-\d{5}-\d{1}"/>
<xsd:pattern value="\d{1}-\d{2}-\d{6}-\d{1}"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="BookCatalogue">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:year"/>
<xsd:element name="ISBN" type="ISBNType"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
(see example 04)
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<xsd:simpleType name="ISBNType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{5}-\d{5}-\d{5}"/>
<xsd:pattern value="\d{1}-\d{3}-\d{5}-\d{1}"/>
<xsd:pattern value="\d{1}-\d{2}-\d{6}-\d{1}"/>
</xsd:restriction>
</xsd:simpleType>
"I am defining a new type that is a restricted form of the string type. Elements
declared of this type must conform to one of the following patterns:
- First Pattern: 5 digits followed by a dash followed by 5
digits followed by another dash followed by 5 more digits, or
- Second Pattern: 1 digit followed by a dash followed by 3
digits followed by another dash followed by 5 digits followed by
another dash followed by 1 more digit, or
- Third Pattern: 1 digit followed by a dash followed by 2
digits followed by another dash followed by 6 digits followed by
another dash followed by 1 more digit."
These patterns are specified using Regular Expressions. In a few slides
we will see more of the Regular Expression syntax.
40
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<xsd:complexType> or
<xsd:simpleType>?
• When do you use the complexType element
and when do you use the simpleType
element?
– Use the complexType element when you want
to define child elements and/or attributes of an
element
– Use the simpleType element when you want to
create a new type that is a refinement of a builtin type (string, integer, etc)
41
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
42
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
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
43
Built-in Datatypes (cont.)
•
Primitive Datatypes
–
–
–
–
–
–
–
gDay
gMonth
hexBinary
base64Binary
anyURI
QName
NOTATION
•
Atomic, built-in
–
–
–
–
–
–
–
format: ---DD (note the 3 dashes)
format: --MM-a hex string
a base64 string
http://www.xfront.com
a namespace qualified name
a NOTATION from the XML spec
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
44
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
must be used only with attributes
must be used only with attributes
456
negative infinity to 0
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
45
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 (which we will discuss later):
ID, IDREF, IDREFS, NMTOKEN, NMTOKENS, ENTITY, and ENTITIES.
Do Lab 3
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
46
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
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.
• Example. The string primitive datatype has six optional
facets:
– pattern
– enumeration
– length
– minLength
– maxlength
– whitespace (legal values: preserve, replace, collapse)
47
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Example of Creating a New
Datatype by Specifying Facet
Values
<xsd:simpleType name="TelephoneNumber">
<xsd:restriction base="xsd:string">
<xsd:length value="8"/>
<xsd:pattern value="\d{3}-\d{4}"/>
</xsd:restriction>
</xsd:simpleType>
This creates a new datatype called 'TelephoneNumber'. Elements of
this type can hold string values, but the string length must be exactly
8 characters long and the string must follow the pattern: ddd-dddd,
where 'd' represents a 'digit'. (Obviously, in this example the regular
expression makes the length facet redundant.)
48
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Another Example
<xsd:simpleType name="US-Flag-Colors">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="red"/>
<xsd:enumeration value="white"/>
<xsd:enumeration value="blue"/>
</xsd:restriction>
</xsd:simpleType>
This creates a new type called US-Flag-Colors.
An element declared to be of this type
must have either the value red, or white, or blue.
49
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Facets of the Integer Datatype
• Facets:
– pattern
– enumeration
– whitespace
– maxInclusive
– maxExclusive
– minInclusive
– minExclusive
– totalDigits
50
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
51
Example
<xsd:simpleType name= "EarthSurfaceElevation">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="-1290"/>
<xsd:maxInclusive value="29035"/>
</xsd:restriction>
</xsd:simpleType>
This creates a new datatype called 'EarthSurfaceElevation'.
Elements declared to be of this type can hold an integer.
However, the integer is restricted to have a value between
-1290 and 29035, inclusive.
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
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:
Sources:
- length
- string
- minlength
- boolean
- maxlength
- float
- pattern
- double
- enumeration
- decimal
- minInclusive
- timeDuration
- maxInclusive
- recurringDuration
- minExclusive
- uriReference
- maxExclusive
...
...
See DatatypeFacets.html for a mapping of datatypes to their facets.
52
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
53
Multiple Facets - "and" them
together, or "or" them together?
<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 TelephoneNumber
must be a string of length=8 and the string must
follow the pattern: 3 digits, dash, 4 digits.
<xsd:simpleType name="US-Flag-Color">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="red"/>
<xsd:enumeration value="white"/>
<xsd:enumeration value="blue"/>
</xsd:restriction>
</xsd:simpleType>
An element declared to be of type US-Flag-Color
must be a string with a value of either red, or white,
or blue.
Patterns, enumerations => "or" them together
All other facets => "and" them together
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Element Containing a UserDefined Simple Type
Example. Create a schema element declaration for an elevation element.
Declare the elevation element to be an integer with a range -1290 to 29035
<elevation>5240</elevation>
Here's one way of declaring the elevation element:
<xsd:simpleType name="EarthSurfaceElevation">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="-1290"/>
<xsd:maxInclusive value="29035"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="elevation" type="EarthSurfaceElevation"/>
54
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Element Containing a UserDefined Simple Type (cont.)
Here's an alternative method for declaring elevation:
<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>
55
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Summary of Declaring Elements
(three ways to do it)
1
<xsd:element name="name" type="type" minOccurs="int" maxOccurs="int"/>
2
<xsd:element name="name" minOccurs="int" maxOccurs="int">
<xsd:complexType>
…
</xsd:complexType>
</xsd:element>
3
<xsd:element name="name" minOccurs="int" maxOccurs="int">
<xsd:simpleType>
<xsd:restriction base="type">
…
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
56
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Annotating Schemas
• The <annotation> element is used for documenting the schema, both
for humans and for programs.
– Use <documentation> for providing a comment to humans
– Use <appinfo> for providing a comment to programs
• The content is any well-formed XML
• Note that annotations have no effect on schema validation
<xsd:annotation>
<xsd:documentation>
This constraint is not expressible with XML Schema: The value of element A must be greater than the
value of element B. So, we need to use a separate tool (e.g., Schematron) to check this constraint. We
will express this constraint in the appinfo section (below).
</xsd:documentation>
<xsd:appinfo>
<assert test="A &gt; B">A must be greater than B</assert>
</xsd:appinfo>
<xsd:/annotation>
57
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Where Can You Put Annotations?
• You cannot put annotations at any location
in the schema.
• Annotations are restricted to occur at the
beginning of the content model of the
component that you are annotating.
58
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns="http://www.publishing.org"
elementFormDefault="qualified">
<xsd:element name="BookCatalogue">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Suppose that we want to annotate, say, the Date element
declaration. What do we do? See next page ...
59
Can only
put
annotations
at these
locations
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns="http://www.publishing.org"
elementFormDefault="qualified">
<xsd:element name="BookCatalogue">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string">
<xsd:annotation>
<xsd:documention>This is how to annotate the Date element!</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
60
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
61
Save $$$ using XML Schemas
Code to actually
do the work
Code to check the
structure and content
of the data
"In a typical program, up to 60% of the code is spent checking the data!"
- source unknown
Continued -->
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
62
Save $$$ using XML Schemas (cont.)
Code to actually
do the work
Code to check the
structure and content
of the data
If your data is structured as
XML, and there is a schema,
then you can hand the
data-checking task off to a
schema validator.
Thus, your code is reduced
by up to 60%!!!
Big $$ savings!
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
63
Classic use of XML Schemas
(Trading Partners - B2B)
Software
to Process
P.O.
"P.O. is
okay"
P.O.
P.O.
Consumer
Supplier
Schema
Validator
P.O.
Schema
(Schema at neutral web site)
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
What are XML Schemas?
• Data Model
• With XML Schemas you specify how your XML data will be
organized, and the datatypes of your data. That is, with XML
Schemas you model your XML data
• A Contract
• Organizations agree to structure their XML documents in
conformance with the XML Schema. Thus, the XML Schema
acts as a contract
• A rich source of metadata
• An XML Schema document contains lots of data about the data
in the XML documents, i.e., XML Schemas contain metadata
64
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
65
No Limits
• The previous slide showed the classic use of XML
Schemas - to validate your data (so that your program
doesn't have to do it)
• However, there are many other uses for XML Schemas.
Schemas are a wonderful source of metadata.
• Truly, your imagination is the only limit on its usefulness.
• On the next slide I show how to use the metadata provided
by XML Schemas to create a GUI. The slide after that
shows how to automatically generate an API using XML
Schema metadata. Following that is a slide showing how
to create a "smart editor" using XML Schemas.
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
66
XML Schema --> GUI
P.O.
Schema
GUI
Builder
cf: one schema
many schemas
P.O.
HTML
Supplier
Web
Server
 many styleSheets  many outputs [format]
 GUI Builder  one uniform output [format]
(one stylesheet for many XML schema instances)
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
schema-for schema
67
GUIBuilder
(a StyleSheet for
instance of
schema-for schema)
Schema A GUI
Schema A API etc.
schema A
XSL StyleSheet
for instance of
schema A
schema A instance
(XML document)
various output
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
68
XML Schema --> API
P.O.
Schema
API
Builder
P.O.
API
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
69
XML Schema --> Smart Editor
P.O.
Schema
Smart Editor
(XML Spy)
Helps you build your
instance documents.
For example, it pops
up a menu showing
you what is valid next.
It knows this by looking
at the XML Schema!
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
70
Automatic
API generation
Validate
XML
documents
XML Schema
Smart Editor
Automatic
GUI generation
??? Limited only
by your imagination
Do Lab 4
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
71
Regular Expressions
• Recall that the string datatype has a pattern
facet. The value of a pattern facet is a
regular expression. Below are some
examples of regular expressions:
Regular Expression
- Chapter \d
- a*b
- [xyz]b
- a?b
- a+b
- [a-c]x
Example
- Chapter 1
- b, ab, aab, aaab, …
- xb, yb, zb
- b, ab
- ab, aab, aaab, …
- ax, bx, cx
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
72
Regular Expressions (cont.)
• Regular Expression
–
–
–
–
–
–
–
–
–
–
[a-c]x
[-ac]x
[ac-]x
[^0-9]x
\Dx
Chapter\s\d
(ho){2} there
(ho\s){2} there
.abc
(a|b)+x
• Example
–
–
–
–
–
–
–
–
–
ax, bx, cx
-x, ax, cx
ax, cx, -x
any non-digit char followed by x
any non-digit char followed by x
Chapter followed by a blank followed by a digit
hoho there
ho ho there
any (one) char followed by abc
– ax, bx, aax, bbx, abx, bax,...
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
73
Regular Expressions (concluded)
• a{1,3}x
• a{2,}x
• \w\s\w
• ax, aax, aaax
• aax, aaax, aaaax, …
• word character
(alphanumeric plus
dash) followed by a
space followed by a
word character
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
74
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 ...
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
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>
75
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
76
Regular Expression Parser
• Want to test your skill in writing regular
expressions? Go to:
http://www.xfront.org/xml-schema/
– Dan Potter has created a nice tool which allows
you to enter a regular expression and then enter
a string. The parser will then determine if your
string conforms to your regular expression.
Do Lab 5
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Derived Types
• We can do a form of subclassing
complexType definitions. We call this
"derived types"
– derive by extension: extend the parent
complexType with more elements
– derive by restriction: constrain the parent
complexType by constraining some of the
elements to have
• a more restricted range of values, or
• a more restricted number of occurrences.
77
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns="http://www.publishing.org"
elementFormDefault="qualified">
<xsd:complexType name="Publication">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:year"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Book">
<xsd:complexContent>
<xsd:extension base="Publication" >
<xsd:sequence>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="BookCatalogue">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" type="Book" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
(see example05)
78
Note that the
Book type extends
the Publication
type, i.e., doing
Derive by Extension
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<xsd:element name="BookCatalogue">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" minOccurs="0" maxOccurs="unbounded" type="Book"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Notice that an element and a type can have the same name!
(Later we will see that element declarations and type definitions
reside in different Symbol Spaces.)
79
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
80
<xsd:complexType name="Publication">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:year"/>
</xsd:sequence>
</xsd:complexType >
<xsd:complexType name="Book">
<xsd:complexContent>
<xsd:extension base="Publication">
<xsd:sequence>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType >
Elements declared to be of type Book will have 5 child elements - Title, Author, Date, ISBN, and Publisher.
Note that the elements in the derived type are appended to the elements in the base type.
Do Lab 6
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Derive by Restriction
<xsd:complexType name="Publication">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:year"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name= "SingleAuthorPublication">
<xsd:complexContent>
<xsd:restriction base="Publication">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:year"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
Elements of type SingleAuthorPublication will have 3 child elements - Title, Author, and Date.
However, there must be exactly one Author element.
Note that in the restriction type you must repeat all the declarations from the base type.
81
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Derive by Restriction (cont.)
•
•
You might (legitimately) ask:
– why do I have to repeat all the declarations from the base type? Why can't
I simply show the delta (i.e., show those declarations that are changed)?
– What's the point of doing derived by restriction if I have to repeat
everything? I'm certainly not saving on typing.
Answer:
– Even though you have to retype everything in the base type there are
advantages to explicitly associating the type with the base type. Later we
will see that an element’s content model may be substituted by the content
model of derived types. Thus, the content of an element that has been
declared to be of type Publication can be substituted with a
SingleAuthorPublication content since SingleAuthorPublication derives
from Publication. We will discuss this type substitutability in detail later.
82
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Prohibiting Derivations
• Sometimes we may want to create a type and disallow all derivations
of it, or just disallow extension derivations, or disallow restriction
derivations.
– Rationale: "For example, I may create a complexType and make it
publicly available for others to use. However, I don't want them to extend
it with their proprietary extensions or constrict it to remove, say, copyright
information." (Jon Cleaver)
<xsd:complexType name="Publication" final="#all" …> Publication cannot be extended nor restricted
<xsd:complexType name="Publication" final="restriction" …> Publication cannot be restricted
<xsd:complexType name="Publication" final="extension" …> Publication cannot be extended
83
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
84
Terminology: Declaration vs
Definition
• In a schema:
– You declare elements and attributes. Schema
components that are declared are used in an XML
instance document.
– You define components that are used just within the
schema document(s)
Declarations:
- element declarations
- attribute declarations
Definitions:
- type definitions
- attribute group definitions
- model group definitions
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Terminology: Global versus
Local
• Global element declarations, global type
definitions:
– These are element declarations/type definitions
that are immediate children of <schema>
• Local element declarations, local type
definitions:
– These are element declarations/type definitions
that are nested within other elements/types.
85
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
86
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns="http://www.publishing.org"
elementFormDefault="qualified">
<xsd:complexType name="Publication">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:year"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Book">
<xsd:complexContent>
<xsd:extension base="Publication" >
<xsd:sequence>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="BookCatalogue">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" type="Book" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Local type definition
Local element declarations
Global type definition
Global type definition
Global element declaration
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Global or Local … What's the
Big Deal?
• So what if an element or type is global or
local. What practical impact does it have?
– Answer: only global elements/types can be
referenced (i.e., reused). Thus, if an
element/type is local then it is effectively
invisible to the rest of the schema (and to other
schemas).
87
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Element Substitutability
• Oftentimes in daily conversation there are several ways to express
something.
– In Boston we use the words "T" and "subway" interchangeably.
For example, "we took the T into town", or "we took the subway
into town".
• Thus, "T" and "subway" are substitutable. Which one is used
may depend upon what part of the state you live in, what mood
you're in, or any number of factors.
• We would like to be able to express this substitutability in XML
Schemas.
– That is, we would like to be able to declare in the schema an element
called "subway", an element called "T", and state that "T"may be
substituted for "subway". Instance documents can then use either
<subway> or <T>, depending on their preference.
88
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
substitutionGroup
• We can define a group of substitutable elements
(called a substitutionGroup) by declaring an
element (called the head) and then declaring other
elements which state that they are substitutable for
the head element.
subway is the head element
<xsd:element name="T" substitutionGroup="subway" type="xsd:string"/> T is substitutable for subway
<xsd:element name="subway" type="xsd:string"/>
So what's the big deal?
- Anywhere a head element can be used in an instance document,
any member of the substitutionGroup can be substituted!
89
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Schema:
Instance doc:
Alternative
instance doc
(substitute T
for subway):
<xsd:element name="subway" type="xsd:string"/>
<xsd:element name="T" substitutionGroup="subway" type="xsd:string"/>
<xsd:element name="transportation">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="subway"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<transportation>
<subway>Red Line</subway>
</transportation>
<transportation>
<T>Red Line</T>
</transportation>
This example shows the <subway> element being substituted with
the <T> element.
90
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
International Clients
• We can use substitutionGroups to create
tags customized for our international
clients. On the next slide is shown a
Spanish version of the tags.
91
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Schema:
Instance doc:
<xsd:element name="subway" type="xsd:string"/>
<xsd:element name="metro" substitutionGroup="subway" type="xsd:string"/>
<xsd:complexType name="transport">
<xsd:sequence>
<xsd:element ref="subway"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="transportation" type="transport/>
<xsd:element name="transportate" substitutionGroup="transportation"/>
<transportation>
<subway>Red Line</subway>
</transportation>
Alternative
<transporte>
instance doc
<metro>Linea Roja</metro>
(customized
</transporte>
for our
Spanish clients):
92
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Notes about using
substitutionGroup
• The elements that are declared to be in the
substitution group (e.g., subway and T) must be
declared as global elements
• If the type of a substitutionGroup element is the
same as the head element then you can omit it (the
type)
– In our Subway example we could have omitted the type
attribute in the declaration of the T element since it is
the same as Subway’s type (string).
93
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
94
Notes about using
substitutionGroup (cont.)
• The type of every element in the
substitutionGroup must be the same as, or derived
from, the type of the head element.
<xsd:element name="A" type="xxx"/>
<xsd:element name="B" substitutionGroup="A" type="yyy"/>
This type must be the same as "xxx" or,
it must be derived from "xxx".
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Blocking Element Substitution
• An element may wish to block other
elements from substituting with it. This is
achieved by adding a block attribute.
<xsd:element name="…" type="…" block="substitution"/>
95
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Schema:
Instance doc:
Not allowed!
96
<xsd:element name="subway" type="xsd:string" block="substitution"/>
<xsd:element name="T" substitutionGroup="subway" type="xsd:string"/>
<xsd:element name="transportation">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="subway"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<transportation>
<subway>Red Line</subway>
</transportation>
<transportation>
<T>Red Line</T>
</transportation>
Do Lab 7
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
97
Attributes
• On the next slide I show a version of the
BookCatalogue DTD that uses attributes.
Then, on the following slide I show how
this is implemented using XML Schemas.
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
98
<!ELEMENT BookCatalogue (Book)*>
<!ELEMENT Book (Title, Author+, Date, ISBN, Publisher)>
<!ATTLIST Book
Category (autobiography | non-fiction | fiction) #REQUIRED
InStock (true | false) "false"
Reviewer CDATA " ">
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Author (#PCDATA)>
<!ELEMENT Date (#PCDATA)>
<!ELEMENT ISBN (#PCDATA)>
<!ELEMENT Publisher (#PCDATA)>
<!ELEMENT Month (#PCDATA)>
<!ELEMENT Year (#PCDATA)>
BookCatalogue2.dtd
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
99
<xsd:element name="BookCatalogue">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
<xsd:attributeGroup ref="BookAttributes"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:attributeGroup name="BookAttributes">
<xsd:attribute name="Category" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
Category (autobiography | non-fiction | fiction) #REQUIRED
<xsd:enumeration value="autobiography"/>
<xsd:enumeration value="non-fiction"/>
<xsd:enumeration value="fiction"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
InStock (true | false) "false"
<xsd:attribute name="InStock" type="xsd:boolean" use=“optional" default="false"/>
Reviewer CDATA " "
<xsd:attribute name="Reviewer" type="xsd:string" default=" "/>
</xsd:attributeGroup>
(see example06)
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<xsd:attribute name="Category" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="autobiography"/>
<xsd:enumeration value="non-fiction"/>
<xsd:enumeration value="fiction"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
"Instance documents are required to have the Category attribute
(as indicated by use="required"). The value of Category must be
either autobiography, non-fiction, or fiction (as specified by the
enumeration facets)."
Note: attributes can only have simpleTypes (i.e., attributes cannot
have child elements).
100
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
101
Summary of Declaring Attributes
(two ways to do it)
1
<xsd:attribute name="name" type="simple-type" use="how-its-used" default|fixed="value"/>
xsd:string
xsd:integer
xsd:boolean
...
2
required
optional
prohibited
<xsd:attribute name="name" use="how-its-used" default|fixed="value">
<xsd:simpleType>
<xsd:restriction base="simple-type">
<xsd:facet value="value"/>
…
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
default value when use
is optional; or fixed value
use =required or optional.
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
use --> use it only with Local
Attribute Declarations
• The "use" attribute only makes sense in the
context of an element declaration. Example:
"for each Book element, the Category
attribute is required".
• When declaring a global attribute do not
specify a "use"
102
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<xsd:element name="Book">
<xsd:complexType>
<xsd:sequence>
…
</xsd:sequence>
<xsd:attribute ref="Category" use="required"/>
…
</xsd:complexType>
</xsd:element>
<xsd:attribute name="Category">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="autobiography"/>
<xsd:enumeration value="fiction"/>
<xsd:enumeration value="non-fiction"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
103
Local attribute declaration. Must have
a "use“ or a default or a fixed.
Global attribute declaration. Must NOT
have a "use" ("use" only makes sense in
the context of an element)
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Alternate Schema
• On the next slide is another way of
expressing the last example - the attributes
are inlined within the Book declaration
rather than being separately defined in an
attributeGroup. (I only show a portion of the
schema - the Book element declaration.)
104
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
105
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
<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:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="Category" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="autobiography"/>
<xsd:enumeration value="non-fiction"/>
<xsd:enumeration value="fiction"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="InStock" type="xsd:boolean" default="false"/>
<xsd:attribute name="Reviewer" type="xsd:string" default=" "/>
</xsd:complexType>
</xsd:element>
(see example07)
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
106
Notes about Attributes
• The attribute declarations always come last,
after the element declarations.
• The attributes are always with respect to the
element that they are defined (nested)
within.
<xsd:element name="foo">
"bar and boo are
attributes of foo"
<xsd:complexType>
<xsd:sequence>
…
</xsd:sequence>
<xsd:attribute name="bar" …/>
<xsd:attribute name="boo" …/>
</xsd:complexType>
</xsd:element>
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
These attributes
apply to the
element they are
nested within (Book)
That is, Book has three
attributes - Category,
InStock, and Reviewer.
107
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
<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:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="Category" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="autobiography"/>
<xsd:enumeration value="non-fiction"/>
<xsd:enumeration value="fiction"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="InStock" type="xsd:boolean" use="default" value="false"/>
<xsd:attribute name="Reviewer" type="xsd:string" use="default" value=" "/>
</xsd:complexType>
</xsd:element>
Do Lab 8.a,
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Element with Simple Content and
Attributes
Example. Consider this:
<elevation units="feet">5440</elevation>
The elevation element has these two constraints:
- it has a simple (integer) content
- it has an attribute called units
How do we declare elevation? (see next slide)
108
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<xsd:element name="elevation">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:integer">
<xsd:attribute name="units" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
elevation contains an attribute.
- therefore, we must use <xsd:complexType>
However, elevation does not contain child elements (which is what we generally
use <complexType> to indicate). Instead, elevation contains simpleContent.
We wish to extend the simpleContent (an integer) with an attribute.
109
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
elevation - use Stronger Datatype
• In the declaration for elevation we allowed
it to hold any integer. Further, we allowed
the units attribute to hold any string.
• Let's restrict elevation to hold an integer
with a range 0 - 12,000 and let's restrict
units to hold either the string "feet" or the
string "meters"
110
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<xsd:simpleType name="elevationType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="12000"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="unitsType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="feet"/>
<xsd:enumeration value="meters"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="elevation">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="elevationType">
<xsd:attribute name="units" type="unitsType" use="required"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
111
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Summary of Declaring Elements
1. Element with Simple Content.
Declaring an element using a built-in type:
<xsd:element name="numStudents" type="xsd:positiveInteger"/>
Declaring an element using a user-defined simpleType:
<xsd:simpleType name="US-Flag-Colors>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="red"/>
<xsd:enumeration value="white"/>
<xsd:enumeration value="blue"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="flag" type="US-Flag-Colors"/>
An alternative formulation of the above flag example is to inline the simpleType definition:
<xsd:element name="flag">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="red"/>
<xsd:enumeration value="white"/>
<xsd:enumeration value="blue"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
112
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Summary of Declaring Elements
(cont.)
2. Element Contains Child Elements
Defining the child elements inline:
<xsd:element name="Person">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="FirstName" type="xsd:string"/>
<xsd:element name="Surname" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
An alternate formulation of the above Person example is to create a named complexType and then use that type:
<xsd:complexType name="PersonType">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="FirstName" type="xsd:string"/>
<xsd:element name="Surname" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="Person" type="PersonType"/>
113
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Summary of Declaring Elements
(cont.)
3. Element Contains a ComplexType that is an Extension of another ComplexType
<xsd:complexType name="Publication">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:year"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Book">
<xsd:complexContent>
<xsd:extension base="Publication" >
<xsd:sequence>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="Book" type="Book"/>
114
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Summary of Declaring Elements
(cont.)
4. Element Contains a ComplexType that is a Restriction of another ComplexType
<xsd:complexType name="Publication">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:year"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name= "SingleAuthorPublication">
<xsd:complexContent>
<xsd:restriction base="Publication">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:year"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="Catalogue" type="SingleAuthorPublication"/>
115
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
116
Summary of Declaring Elements
(concluded)
5. Element Contains Simple Content and Attributes
<xsd:element name="apple">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="variety" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
Example. <apple variety="Cortland">Large, green, sour</apple>
Do Lab 8.b,
8.c
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
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!
117
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
118
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns="http://www.publishing.org"
elementFormDefault="qualified">
<xsd:element name="BookCatalogue">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:group ref="BookElements"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:group name="BookElements">
<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:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:group>
</xsd:schema>
(see example08)
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
119
Note about group
• Group definitions must be global
<xsd:element name="Book">
<xsd:complexType>
<xsd:sequence>
<xsd:group name="BookElements">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" minOccurs="0"/>
<xsd:element name="Author" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
...
</xsd:sequence>
</xsd:group>
</xsd:sequence>
...
</xsd:complexType>
</xsd:element>
Cannot inline the
group definition.
Instead, you must
use a ref here and
define the group
globally.
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
120
Expressing Alternates
DTD:
XML Schema:
<!ELEMENT transportation (train | plane | automobile)>
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.travel.org"
xmlns="http://www.travel.org"
elementFormDefault="qualified">
<xsd:element name="transportation">
<xsd:complexType>
<xsd:choice>
<xsd:element name="train" type="xsd:string"/>
<xsd:element name="plane" type="xsd:string"/>
<xsd:element name="automobile" type="xsd:string"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
(see example 9)
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
121
Expressing Repeats
DTD:
XML Schema:
<!ELEMENT binary-string (zero | one)*>
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.binary.org"
xmlns="http://www.binary.org"
elementFormDefault="qualified">
<xsd:element name="binary-string">
<xsd:complexType>
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="zero" type="xsd:unsignedByte" fixed="0"/>
<xsd:element name="one" type="xsd:unsignedByte" fixed="1"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
(see example 10)
Notes:
1. An element can fix its value, using the fixed attribute.
2. When you don't specify a value for minOccurs, it defaults to "1".
Same for maxOccurs. See the last example (transportation) where
we used a <choice> element with no minOccurs or maxOccurs.
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
122
Expressing Any Order
Problem: create an element, Book, which contains Author, Title, Date, ISBN, and Publisher,
in any order (Note: this cannot be easily done with DTDs).
XML Schema:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.publishing.org"
xmlns="http://www.publishing.org"
elementFormDefault="qualified">
<xsd:element name="BookCatalogue">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:all>
<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:all>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
(see example 11)
<all> means that Book must contain all five child elements, but
they may occur in any order.
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
123
Constraints on using <all>
• Elements declared within <all> must have a
maxOccurs value of "1" (minOccurs can be
either "0" or "1")
Do Lab 9
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
124
Empty Element
DTD:
Schema:
<!ELEMENT image EMPTY>
<!ATTLIST image href CDATA #REQUIRED>
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.photography.org"
xmlns="http://www.photography.org"
elementFormDefault="qualified">
<xsd:element name="gallery">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="image" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="href" type="xsd:uriReference" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
(see example 12)
Do Lab 10
Instance
doc (snippet):
<image href="http://www.xfront.com/Rog.gif"/>
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Using <sequence> and <choice>
DTD:
XML Schema:
<!ELEMENT life ((work, eat)*, (work | play)*, sleep)* >
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.binary.org"
xmlns="http://www.binary.org"
elementFormDefault="qualified">
<xsd:element name="life">
<xsd:complexType>
<xsd:sequence minOccur="0" maxOccur="unbounded">
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
<xsd:element name="work" type="xsd:string"/>
<xsd:element name="eat" type="xsd:string"/>
</xsd: sequence>
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="work" type="xsd:string"/>
<xsd:element name="play" type="xsd:string"/>
</xsd:choice>
<xsd:element name="sleep" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
125
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
No targetNamespace
(noNamespaceSchemaLocation)
• Sometimes you may wish to create a schema but without a
targetNamespace
• 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 qualify the elements.
– 2. In the instance document, instead of using schemaLocation use
noNamespaceSchemaLocation.
126
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
elementFormDefault="qualified">
<xsd:element name="BookCatalogue">
<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>
(see example13)
127
Note that there is no
targetNamespace
attribute, and note that
there is no longer a
default namespace.
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
128
<?xml version="1.0"?>
<BookCatalogue xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation= "BookCatalogue.xsd">
<Book>
<Title>My Life and Times</Title>
<Author>Paul McCartney</Author>
<Date>1998</Date>
<ISBN>94303-12021-43892</ISBN>
<Publisher>McMillin Publishing</Publisher>
</Book>
…
</BookCatalogue>
(see example13)
Note that there is no default namespace declaration. So, none of the elements belong
to a namespace.
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.
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Assembling an Instance
Document from Multiple Schema
Documents
• An instance document may have elements
from multiple schemas.
• Validation can apply to the entire XML
instance document, or to a single element.
129
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Library.xml
(see example 14)
<?xml version="1.0"?>
<Library xmlns:b="http://www.publishing.org"
xmlns:e="http://www.employee.org"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:schemaLocation=
"http://www.publishing.org
BookCatalogue.xsd
http://www.employee.org
Employee.xsd">
<b:BookCatalogue>
<b:Book>
<b:Title>My Life and Times</b:Title>
<b:Author>Paul McCartney</b:Author>
<b:Date>1998</b:Date>
<b:ISBN>94303-12021-43892</b:ISBN>
<b:Publisher>McMillin Publishing</b:Publisher>
</b:Book>
<b:Book>
<b:Title>Illusions The Adventures of a Reluctant Messiah</b:Title>
<b:Author>Richard Bach</b:Author>
<b:Date>1977</b:Date>
<b:ISBN>0-440-34319-4</b:ISBN>
<b:Publisher>Dell Publishing Co.</b:Publisher>
</b:Book>
<b:Book>
<b:Title>The First and Last Freedom</b:Title>
<b:Author>J. Krishnamurti</b:Author>
<b:Date>1954</b:Date>
<b:ISBN>0-06-064831-7</b:ISBN>
<b:Publisher>Harper &amp; Row</b:Publisher>
</b:Book>
</b:BookCatalogue>
<e:Employees>
<e:Employee>
<e:Name>John Doe</e:Name>
<e:SSN>123-45-6789</e:SSN>
</e:Employee>
<e:Employee>
<e:Name>Sally Smith</e:Name>
<e:SSN>000-11-2345</e:SSN>
</e:Employee>
</e:Employees>
</Library>
130
Validating against
two schemas
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
131
Assembling a Schema from
Multiple Schema Documents
• The include element allows you to bring in schema definitions from
other schemas
– All the schemas you include must have the same namespace as
your schema (i.e., the schema that is using the include)
– The net effect of include is as though you had typed all the
definitions directly into the containing schema
LibraryEmployees.xsd
LibraryBookCatalogue.xsd
<xsd:schema …>
<xsd:include schemaLocation="LibraryBookCatalogue.xsd"/>
<xsd:include schemaLocation="LibraryEmployees.xsd"/>
…
</xsd:schema>
Library.xsd
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
132
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.library.org"
xmlns="http://www.library.org"
elementFormDefault="qualified">
<xsd:include schemaLocation="LibraryBookCatalogue.xsd"/>
<xsd:include schemaLocation="LibraryEmployees.xsd"/>
<xsd:element name="Library">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="BookCatalogue"/>
<xsd:element ref="Employees"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Library.xsd (see example 15)
These are
referencing
the element
declarations
in the other
schemas.
Nice!
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Assembling a Schema from a
Schema with no targetNamespace
• A schema can <include> another schema
that has no targetNamespace. The included
components take on the namespace of the
schema that is doing the <include>.
133
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
134
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
elementFormDefault="qualified">
<xsd:complexType name="Product">
<xsd:sequence>
<xsd:element name="Type" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Product.xsd (see example16)
Note that this schema has no targetNamespace!
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
135
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/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="Product" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Company.xsd (see example16)
This schema <include>s Product.xsd. Thus, the components in Product.xsd are namespace-coerced
to the company namespace.
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
136
Assembling a Schema from
Multiple Schema Documents in
Different Namespaces
• The import element allows you to reference
elements in another namespace
Namespace
A
Namespace
B
A.xsd
B.xsd
<xsd:schema …>
<xsd:import namespace="A"
schemaLocation="A.xsd"/>
<xsd:import namespace="B"
schemaLocation="B.xsd"/>
…
</xsd:schema>
C.xsd
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/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>
Camera.xsd (see example 17)
137
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/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 (see example 17 for Olympus.xsd and Pentax.xsd)
138
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<camera xmlns="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/2000/10/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">
<body>
<nikon:description>Ergonomically designed casing for easy handling</nikon:description>
</body>
<lens>
<olympus:zoom>300mm</olympus:zoom>
<olympus:f-stop>1.2</olympus:f-stop>
</lens>
<manual_adapter>
<pentax:speed>1/10,000 sec to 100 sec</pentax:speed>
</manual_adapter>
</camera>
Camera.xml (see example 17)
139
The Camera instance
uses elements
from the Nikon,
Olympus, and
Pentax namespaces.
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
140
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 camera it will look in
the camera schema and see the import elements,
thus it will be aware of the other schemas being
used (Nikon, Olympus, and Pentax)
• The next slide shows the non-redundant version.
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<camera xmlns="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/2000/10/XMLSchema-instance"
xsi:schemaLocation=
"http://www.camera.org
Camera.xsd">
<body>
<nikon:description>Ergonomically designed casing for easy handling</nikon:description>
</body>
<lens>
<olympus:zoom>300mm</olympus:zoom>
<olympus:f-stop>1.2</olympus:f-stop>
</lens>
<manual_adapter>
<pentax:speed>1/10,000 sec to 100 sec</pentax:speed>
</manual_adapter>
</camera>
Camera.xml (non-redundant version)
141
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
142
Note about using Include and
Import
• The <include> and <import> elements must
come before any element declarations or
type definitions.
Do Labs
11.a, 11.b,
11.c
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Creating Lists
• There are times when you will want an
element to contain a list of values, e.g.,
"The contents of the Numbers element is a
list of numbers".
Example: For a document containing the Lottery drawing we might have
<Numbers>12 49 37 99 20 67</Numbers>
How do we declare the element Numbers ...
(1) To contain a list of integers, and
(2) Each integer is restricted to be between 1 and 99, and
(3) The total number of integers in the list is exactly six.
143
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
144
<?xml version="1.0"?>
<LotteryDrawings xmlns="http://www.lottery.org/namespaces/Lottery"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:schemaLocation=
"http://www.lottery.org/namespaces/Lottery
Lottery.xsd">
<Draw>
<Week>July 1</Week>
<Numbers>21 3 67 8 90 12</Numbers>
</Draw>
<Draw>
<Week>July 8</Week>
<Numbers>55 31 4 57 98 22</Numbers>
</Draw>
<Draw>
<Week>July 15</Week>
<Numbers>70 77 19 35 44 11</Numbers>
</Draw>
</LotteryDrawings>
Lottery.xml (see example18)
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
145
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.lottery.org"
xmlns="http://www.lottery.org"
elementFormDefault="qualified">
<xsd:simpleType name="LotteryNumbers">
<xsd:list itemType="xsd:positiveInteger"/>
</xsd:simpleType>
<xsd:element name="LotteryDrawings">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Draw" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Week" type="xsd:string"/>
<xsd:element name="Numbers" type="LotteryNumbers"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Lottery.xsd
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
LotteryNumbers --> Need
Stronger Datatyping
• The list in the previous schema has two problems:
– It allows <Numbers> to contain an arbitrarily long list
– The numbers in the list may be any positiveInteger
• We need to:
– Restrict the list to length value="6"
– Restrict the numbers to maxInclusive value="99"
146
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.lottery.org"
xmlns="http://www.lottery.org"
elementFormDefault="qualified">
<xsd:simpleType name="OneToNinetyNine">
<xsd:restriction base="xsd:positiveInteger">
<xsd:maxInclusive value="99"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="NumbersList">
<xsd:list itemType="OneToNinetyNine"/>
</xsd:simpleType>
<xsd:simpleType name="LotteryNumbers">
<xsd:restriction base="NumbersList">
<xsd:length value="6"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="LotteryDrawings">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Draw" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Week" type="xsd:string"/>
<xsd:element name="Numbers" type="LotteryNumbers"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Lottery.xsd (see example18)
147
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<xsd:simpleType name="OneToNinetyNine">
<xsd:restriction base="xsd:positiveInteger">
<xsd:maxInclusive value="99"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="NumbersList">
<xsd:list itemType="OneToNinetyNine"/>
</xsd:simpleType>
<xsd:simpleType name="LotteryNumbers">
<xsd:restriction base="NumbersList">
<xsd:length value="6"/>
</xsd:restriction>
</xsd:simpleType>
NumbersList is a list where the type of each item is OneToNintyNine.
LotteryNumbers restricts NumbersList to a length of six (i.e., an element
declared to be of type LotteryNumbers must hold a list of numbers,
between 1 and 99, and the length of the list must be exactly six).
148
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<xsd:simpleType name="OneToNinetyNine">
<xsd:restriction base="xsd:positiveInteger">
<xsd:maxInclusive value="99"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="NumbersList">
<xsd:list itemType="OneToNinetyNine"/>
</xsd:simpleType>
<xsd:simpleType name="LotteryNumbers">
<xsd:restriction base="NumbersList">
<xsd:length value="6"/>
</xsd:restriction>
</xsd:simpleType>
Alternatively,
<xsd:simpleType name="LotteryNumbers">
<xsd:restriction>
<xsd:simpleType>
<xsd:list itemType="OneToNinetyNine">
</xsd:simpleType>
<xsd:length value="6"/>
</xsd:restriction>
</xsd:simpleType>
This is read as: "We are creating a new type called LotteryNumbers.
It is a restriction. At this point we can either use the base
attribute or a simpleType child element to indicate the type that
we are restricting (you cannot use both the base attribute and the
simpleType child element). We want to restrict the type that is a
list of OneToNintyNine. We will restrict that type to a length of 6."
149
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
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., this only applies to simpleTypes
• In the instance document, you must separate each item in a
list with white space (blank, tab, carriage return)
• The only facets that you may use with a list type are:
– length
– minLength
– maxLength
Do Lab 11.d
– enumeration
150
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
151
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
that just two simpleTypes
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
152
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.CostelloReunion.org"
xmlns="http://www.CostelloReunion.org"
elementFormDefault="qualified">
<xsd:simpleType name="Parent">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Mary"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="PatsFamily">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Pat"/>
<xsd:enumeration value="Patti"/>
<xsd:enumeration value="Christopher"/>
<xsd:enumeration value="Elizabeth"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="BarbsFamily">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Barb"/>
<xsd:enumeration value="Greg"/>
<xsd:enumeration value="Dan"/>
<xsd:enumeration value="Kimberly"/>
</xsd:restriction>
</xsd:simpleType>
Cont. -->
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
153
<xsd:simpleType name="JudysFamily">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Judy"/>
<xsd:enumeration value="Peter"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="TomsFamily">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Tom"/>
<xsd:enumeration value="Cheryl"/>
<xsd:enumeration value="Marc"/>
<xsd:enumeration value="Joe"/>
<xsd:enumeration value="Brian"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="RogersFamily">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Roger"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="JohnsFamily">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="John"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="CostelloFamily">
<xsd:union memberTypes="Parent PatsFamily BarbsFamily
JudysFamily TomsFamily RogersFamily
JohnsFamily"/>
</xsd:simpleType>
Cont. -->
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<xsd:element name="Y2KFamilyReunion">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Participants">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" type="CostelloFamily" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Y2KFamilyReunion.xsd (see example 19)
154
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<Y2KFamilyReunion xmlns="http://www.CostelloReunion.org"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:schemaLocation=
"http://www.CostelloReunion.org
Y2KFamilyReunion.xsd">
<Participants>
<Name>Mary</Name>
<Name>Pat</Name>
<Name>Patti</Name>
<Name>Christopher</Name>
<Name>Elizabeth</Name>
<Name>Judy</Name>
<Name>Peter</Name>
<Name>Tom</Name>
<Name>Cheryl</Name>
<Name>Marc</Name>
<Name>Joe</Name>
<Name>Roger</Name>
</Participants>
</Y2KFamilyReunion>
Y2KFamilyReunion.xml (see example 19)
155
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
156
Alternative
<xsd:simpleType name="CostelloFamily">
<xsd:union>
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<enumeration value="Mary"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Pat"/>
<xsd:enumeration value="Patti"/>
<xsd:enumeration value="Christopher"/>
<xsd:enumeration value="Elizabeth"/>
</xsd:restriction>
</xsd:simpleType>
…
</xsd:union>
</xsd:simpleType>
Version 2 of Y2KFamilyReunion.xsd (see example 20)
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
157
Review of 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>
"The memberTypes attribute and
the nested simpleTypes are mutually
exclusive, i.e., you can have one or
the other, but not both."
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
158
maxOccurs
• The value space for maxOccurs is the union
of the value space for nonNegativeInteger
with the value space for a simpleType
which contains one enumeration value "unbounded". See next slide for how
maxOccurs is defined in the schema-forschemas
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
159
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.maxOccurs.org"
xmlns="http://www.maxOccurs.org"
elementFormDefault="qualified">
<xsd:simpleType name="unbounded_type>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="unbounded"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="maxOccurs_type">
<xsd:union memberTypes="unbounded_type xsd:nonNegativeInteger"/>
</xsd:simpleType>
<xsd:element name="schema">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="element" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="maxOccurs" type="maxOccurs_type" use=“optional" default="1"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
(see example20.1)
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
any Element
• The <any> element enables the instance document
author to extend his/her document with elements
not specified by the schema.
<xsd:element name="Book" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
<xsd:any minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Now an instance document author can extend the content of <Book> elements
with any element after <Publisher>.
160
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
161
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.PublishingCompany.org"
xmlns="http://www.PublishingCompany.org"
elementFormDefault="qualified">
<xsd:element name="Reviewer">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="First" type="xsd:string"/>
<xsd:element name="Last" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
PublishingCompany.xsd (see example21)
Suppose that the instance document author discovers this schema, and wants to extend
his <Book> elements with a <Reviewer> element. He/she can do so! Thus, the instance document
will be extended with an element never anticipated by the schema author. Wow!
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<BookCatalogue xmlns="http://www.BookCatalogue.org"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:schemaLocation=
"http://www.BookCatalogue.org
BookCatalogue.xsd
http://www.PublishingCompany.org
PublishingCompany.xsd">
<Book>
<Title>My Life and Times</Title>
<Author>Paul McCartney</Author>
<Date>1998</Date>
<ISBN>94303-12021-43892</ISBN>
<Publisher>McMillin Publishing</Publisher>
<Reviewer xmlns="http://www.PublishingCompany.org">
<Name>
<First>Roger</First>
<Last>Costello</Last>
</Name>
</Reviewer>
</Book>
<Book>
<Title>Illusions The Adventures of a Reluctant Messiah</Title>
<Author>Richard Bach</Author>
<Date>1977</Date>
<ISBN>0-440-34319-4</ISBN>
<Publisher>Dell Publishing Co.</Publisher>
</Book>
</BookCatalogue>
162
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Extensible Instance Documents
• The <any> element enables instance
document authors to create instance
documents which contain elements above
and beyond what was specified by the
schema. The instance documents are said to
be extensible. Contrast this schema with
previous schemas where the content of all
our elements were always fixed and static.
163
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
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
164
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
anyAttribute
• The <anyAttribute> element enables the instance
document author to extend his document with
attributes not specified by the schema.
<xsd:element name="Book" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
<xsd:anyAttribute/>
</xsd:complexType>
</xsd:element>
Now an instance document author can add any number of attributes onto a
<Book> element.
165
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.StandardRepository.org"
xmlns="http://www.StandardRepository.org"
elementFormDefault="qualified">
<xsd:attribute name="id" type="xsd:ID"/>
</xsd:schema>
StandardRepository.xsd (see example21.1)
Suppose that the instance document author discovers this schema, and wants to extend
his <Book> elements with an id attribute. He/she can do so! Thus, the instance document
will be extended with an attribute never anticipated by the schema author. Wow!
166
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
167
<?xml version="1.0"?>
<BookCatalogue xmlns="http://www.BookCatalogue.org"
xmlns:sr="http://www.StandardRepository.org"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:schemaLocation=
"http://www.BookCatalogue.org
BookCatalogue.xsd
http://www.StandardRepository.org
StandardRepository.xsd">
<Book sr:id="P.M.">
<Title>My Life and Times</Title>
<Author>Paul McCartney</Author>
<Date>1998</Date>
<ISBN>94303-12021-43892</ISBN>
<Publisher>McMillin Publishing</Publisher>
</Book>
<Book sr:id="R.B.">
<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>
</BookCatalogue>
BookCatalogue.xml (see example29)
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Extensible Instance Documents
• The <anyAttribute> element enables
instance document authors to create
instance documents which contain attributes
above and beyond what was specified by
the schema. The instance documents are
said to be extensible. Contrast this schema
with previous schemas where the content of
all our elements were always fixed and
static.
168
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Specifying the Namespace of
Extension Attributes
<anyAttribute namespace="##other"/> allows the instance document to contain new attributes,
provided the attributes come from a namespace other than
the one the schema is defining (i.e., targetNamespace).
<anyAttribute namespace="http://www.somewhere.com"/> allows new attributes , provided they're
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)
<anyAttribute namespace="##targetNamespace"/> allows new attributes, provided they're from
the namespace that the schema is defining.
<anyAttribute namespace="##any"/> allows any attributes. This is the default.
<anyAttribute namespace="##local"/> allows any unqualified attributes (i.e., the attributes comes
from no namespace)
169
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Open Content
• Definition: an open content schema is one that allows instance
documents to contain additional elements beyond what is declared in
the schema. This is achieved by using the <any> and <anyAttribute>
elements in the schema.
• Sprinkling <any> and <anyAttribute> elements liberally throughout
your schema will yield benefits in terms of how evolvable your schema
is.
– See later slides for how open content enables the rapid evolution of
schemas that is required in today's marketplace.
170
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
171
Global Openness
•
There is a range of openness that a schema may support - anywhere from having
instance documents where new elements can be inserted anywhere (global openness), to
instance documents where new elements can be inserted only at specific locations
(localized openness)
<xsd:element name="Book">
<xsd:complexType>
<xsd:sequence>
<xsd:any minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="Title" type="xsd:string">
<xsd:complexType>
<xsd:anyAttribute/>
</xsd:complexType>
<xsd:/element>
<xsd:any minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="Author" type="xsd:string">
<xsd:complexType>
<xsd:anyAttribute/>
</xsd:complexType>
</xsd:element>
...
</xsd:sequence>
<xsd:anyAttribute/>
</xsd:complexType>
<xsd:/element>
This schema is allowing
expansion before and
after every element.
Further, is is allowing
for attribute expansion
on every element.
Truly, this is the ultimate
in openness!
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
172
Localized Openness
• With localized openness we design our
schema to allow instance documents to
extend at specific points in the document
<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="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
<xsd:any minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
With this schema we are
allowing instance documents
to extend only at the end of
Book's content model.
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
173
Dynamic Schema Evolution
using Open Content
In today's rapidly changing market static schemas will be less commonplace, as the market pushes schemas to quickly
support new capabilities. For example, consider the cellphone industry. Clearly, this is a rapidly evolving market. Any schema
that the cellphone community creates will soon become obsolete as hardware/software changes extend the cellphone capabilities.
For the cellphone community rapid evolution of a cellphone schema is not just a nicety, the market demands it!
Suppose that the cellphone community gets together and creates a schema, cellphone.xsd. Imagine that every week NOKIA
sends out to the various vendors an instance document (conforming to cellphone.xsd), detailing its current product set.
Now suppose that a few months after cellphone.xsd is agreed upon NOKIA makes some breakthroughs in their cellphones - they
create new memory, call, and display features, none of which are supported by cellphone.xsd. To gain a market advantage NOKIA
will want to get information about these new capabilities to its vendors ASAP. Further, they will have little motivation to wait for
the next meeting of the cellphone community to consider upgrades to cellphone.xsd. They need results NOW. How does open content help?
That is described next.
Suppose that the cellphone schema is declared "open". Immediately NOKIA can extend its instance documents to incorporate data
about the new features. How does this change impact the vendor applications that receive the instance documents? The answer is - not
at all. In the worst case, the vendor's application will simply skip over the new elements. More likely, however, the vendors are showing
the cellphone features in a list box and these new features will be automatically captured with the other features. Let's stop and think about
what has been just described … Without modifying the cellphone schema and without touching the vendor's applications, information about
the new NOKIA features has been instantly disseminated to the marketplace! Open content in the cellphone schema is the enabler for this
rapid dissemination.
Continued -->
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
174
Dynamic Schema Evolution
using Open Content (cont.)
Clearly some types of instance document extensions may require modification to the vendor's applications. Recognize, however, that the
vendors are free to upgrade their applications in their own time. The applications do not need to be upgraded before changes can be
introduced into instance documents. At the very worst, the vendor's applications will simply skip over the extensions. And, of course,
those vendors do not need to upgrade in lock-step
To wrap up this example … suppose that several months later the cellphone community reconvenes to discuss enhancements to the schema.
The new features that NOKIA first introduced into the marketplace are then officially added into the schema. Thus completes the cycle.
Changes to the instance documents have driven the evolution of the schema.
Do Lab 12
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
175
Strategy for Defining Semantics
of your XML Elements
(by Mary Pulvermacher)
•
•
Capture the semantics in the XML Schema
– Describe the semantics within the <annotation> element
– Adopt the convention that every element and attribute have an annotation
which provides information on the meaning
Advantages:
– The XML Schema will capture the data structure, meta-data, and
relationships between the elements
– Use of strong typing will capture much of the data content
– The annotations can capture definitions and other explanatory information
– The structure of the "definitions" will always be consistent with the
structure used in the schema since they are linked
– Since the schema itself is an XML document, we can use XSLT to extract
the annotations and transform the "semantic" information into a format
suitable for human consumption
Copyright © [2001]. Roger L. Costello. All Rights Reserved.
Strategy for Defining Semantics of
your XML Elements
(by Mary Pulvermacher)
Requirement
XML Schema approach
Capture data structure and content
Inherent part of XML schema (DTDs
define structure, schema data types add
content information)
Capture relationships between data
elements
Inherent part of XML schema
Capture data semantics
Use XML annotation element to
capture data definitions
Use data attributes to capture metadata
Make data, meta-data and semantics
accessible to a wide variety of clients
Use XSL to transform this XML
tagged data to make it readable to
humans or other computers
Ensure parallelism between data
structure definition and data
semantics
Consistency ensured by having both
part of same XML schema
176