Professional Documents
Culture Documents
4 XML Schema English
4 XML Schema English
4 XML Schema English
Oscar Díaz
Dpto. de Lenguajes y Sistemas Informáticos
XML Parser
XML
XML WSYSWG
Schema Validation
Editor
XML
Translation
Rules
XML
document
Transformer
User/Application
Oscar Díaz 2
1
Contents
Ø Motivation
Ø Schema = identifier + vocabulary
Ø Schema definition: inline vs. definition by reference
Ø Namespace = types + elements
Ø Null value, key, foreign key
Ø UML and XML Schema
Ø Schema adaptability
Ø Schema extensibility
Ø Schema complexity
<location>
<latitude>32.904237</latitude>
<longitude>73.620290</longitude>
<uncertainty unidads="meters">2</uncertainty>
</location>
2
Who validates?
BEFORE: The receiving application
Code to carry
out the function
Code to check
structure and
content of data
Who validates?
AFTER: The validator, a general-purpose application
Data document
<location>
<latitude>32.904237</latitude>
<longitude>73.620290</longitude>
<uncertainty unidads="meters">2</uncertainty>
</location>
SCHEMA
Data O.K.!!
VALIDATOR
Oscar Díaz 6
3
Externalization of validation
Ø Validator: special purpose engine
Ø Application: focus on the business logic
BEFORE AFTER
Code to check
structure and
content of data SCHEMA
VALIDATOR
Oscar Díaz 7
Some terminology
Ø Validation:
• process that checks if an XML document follows the
rules stated by a given schema
Ø Validator:
• a general purpose programs for conducting
validations from a declarative schema
Ø “Well formed” document
• one that follows XML rules
Ø “Valid” document
• one that follows rules of a schema (if there is one)
Oscar Díaz 8
4
The schema determines …
Ø what sort of elements can appear in the
document
What is XMLSchema?
Oscar Díaz 10
5
SCHEMA =
IDENTIFIER + VOCABULARY
Oscar Díaz 11
BookStore
Author
Book
Namespace Title ISBN
Publisher
Date
Oscar Díaz 12
6
Schema = identifier + vocabulary
Ø A namespace is only a string… unique in the
whole wide world
Oscar Díaz 13
Identifier: uniqueness
Ø G u a r a n t e e : “ I n t e r n e t N a m i n g A u t h o r i t y ”
http://www.onekin.org/misVocabularios/alumno
Oscar Díaz 14
7
Schema = identifier + vocabulary
A schema defines the vocabulary and restrictions that control
the creation of new “document instances”
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
http://www.books.org targetnamespace="http://www.books.org“ …>
<element name="BookStore">
<complexType>
<sequence>
<element name="Book" maxOccurs="unbounded">
BookStore <complexType>
<sequence>
Author <element name="Title" type="xsd:string"/>
<element name="Author" type="xsd:string"/>
Book </sequence>
Title </complexType>
</element>
</sequence>
</complexType>
</element>
Oscar Díaz </schema> 15
ASSOCIATING A SCHEMA
TO A DOCUMENT
Oscar Díaz 17
8
Option 1: the “xmlns” attribute
<?xml version="1.0"?>
<BookSeller xmlns="http://www.BookRetailers.org"/>
<Book >
<Title>My Life and Times</Title> It indicates the vocabulary in
<Author>Paul McCartney</Author>
<Date>1998</Date> which the element is defined.
<ISBN>1-56592-235-2</ISBN>
<Publisher>McMillin Publishing</Publisher>
<Reviewer xmlns="http://www.books.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> WARNING: The subelements with
</Book> no xmlns attribute, “inherit” it from
</BookSeller> the the element that contains them.
Oscar Díaz 18
9
Oscar Díaz 20
Schema location
Ø Aim: providing hints
for the validator to
locate the schema
Ø I f n o l o c a t i o n i s
provided it is up to the
validator to find the
schema on its own
Oscar Díaz 22
10
THE SCHEMA AS
AN XML DOCUMENT
Oscar Díaz 23
Oscar Díaz 24
11
Example
<?xml version="1.0"?>
The root is always a
<schema xmlns="http://www.w3.org/2001/XMLSchema" schema element
http://www.books.org targetnamespace="http://www.books.org“ …>
<element name="BookStore">
<complexType>
<sequence>
<element name="Book" maxOccurs="unbounded">
<complexType>
BookStore <sequence>
<element name="Title" type="xsd:string"/>
URL where the vocabulary
Author
<element name="Author" type="xsd:string"/> being define will be left
Book <element name="Date" type="xsd:string"/>
Title <element name="ISBN" type="xsd:string"/>
Publisher ISBN <element name="Publisher" type="xsd:string"/>
Date </sequence> Root element of “book”
</complexType>
</element> documents
</sequence>
</complexType>
</element>
</schema>
Oscar Díaz 25
Ø Atributes <sequence>
<element name="Book" maxOccurs="unbounded">
<complexType>
<sequence>
<element name="Title" type="xsd:string"/>
Oscar Díaz 29
12
ATTRIBUTES
Oscar Díaz 30
Attribute definitions
1 <xsd:attribute name=“name" type=“tipo-simple" use=“cómo-se-utiliza" default/fixed="value"/>
13
Examples
Oscar Díaz 32
ELEMENTS
Oscar Díaz 33
14
Element declaration: content model
‘juan tarco’
‘castellano’
name
• With subelements
firstName lastName
Oscar Díaz 34
‘juan’ ‘tarco’
‘juan tarco’
Oscar Díaz 35
15
Complex content elements
<product pid="1345"/>
<product pid="1345"/>
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
<description>
It happened on <date lang="norwegian">03.03.99</date> ....
</description>
Oscar Díaz
Oscar Díaz 37
16
Complex content model with
subelements
<nombre>
<firstName> juan </firstName>
<lastName> tarco</lastName> nombre
‘juan’ ‘tarco’
Ø <xs:element name=“nombre">
<xs:complexType>
<xs:sequence>
<xs:element name=“firstName" type="xs:string"/>
<xs:element name=“lastName" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Oscar Díaz 39
‘juan’ ‘tarco’
<xs:element name=“nombre">
<xs:complexType mixed="true" >
<xs:sequence>
<xs:element name=“firstName" type="xs:string"/>
<xs:element name=“lastName" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Oscar Díaz 40
17
Inline definition
(a.k.a russian-doll approach)
Ø Element and type <?xml version="1.0"?>
definition is done <schema xmlns="http://www.w3.org/2001/XMLSchema"
targetnamespace="http://www.books.org“ …>
inside an element <element name="BookStore">
<complexType>
• Title <sequence>
<element name="Book" maxOccurs="unbounded">
• Book’s type <complexType>
<sequence>
<element name="Title" type="xsd:string"/>
<element name="Author" type="xsd:string"/>
<element name="Date" type="xsd:string"/>
<element name="ISBN" type="xsd:string"/>
Ø Drawback: the <element name="Publisher" type="xsd:string"/>
</sequence>
definition of elements </complexType>
</element>
cannot be reused. </sequence>
</complexType>
</element>
</schema>
Oscar Díaz 41
Definition by reference
Ø Decouples <?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
definition and targetnamespace="http://www.books.org"
xmlns="http://www.books.org"
usage elementFormDefault="qualified">
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
Ø Usage by <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
reference. </xsd:complexType>
</xsd:element>
<xsd:element name="Book” type=“BookT”/>
Oscar Díaz 42
18
TYPES
Oscar Díaz 43
Types
Ø Named types <xsd:complexType name=“BookT”>
• Those that have a name, and <xsd:sequence>
they are used by reference
</xsd:sequence>
</xsd:complexType>
<xsd:element name="Book” type=“BookT”/>
Oscar Díaz 44
19
Types
Ø Simple types
• Basic types (pre-defined by XML Schema)
• Simple types, derived from basic types
• Simple types, derived from derived simple types
• Simple types, obtained as lists/unions
Simple types
Ø Simple type elements
• Only have content
• No structure
• No attributes
20
(some) Basic simple types
Oscar Díaz 48
Oscar Díaz 49
21
Types xs:ID and xs:IDREFS
Ø ID type restricts the value to be unique
within the whole document
Ø IDREFS restricts the value to coincide with
another value that is ID typed
Oscar Díaz 51
Oscar Díaz 52
22
ID/IDREFS vs. Database keys
Ø ID unique within the entire document (like oids),
• while a key needs only to uniquely identify a tuple within a relation
Oscar Díaz 53
Oscar Díaz 54
23
Example I
<xsd:simpleType name="shapeT">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="circle"/>
<xsd:enumeration value="triangle"/>
<xsd:enumeration value="square"/>
</xsd:restriction>
</xsd:simpleType>
Oscar Díaz 55
Example II
<xsd:simpleType name="TelephoneNumberT">1
<xsd:restriction base="xsd:string"> 2
<xsd:length value=”10"/>3
<xsd:pattern value="\d{3}-\d{6}"/> 4
</xsd:restriction>
</xsd:simpleType>
Oscar Díaz 56
24
Example III
<xs:simpleType name = "passwordT">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9]{8}"/>
</xs:restriction>
</xs:simpleType>
Oscar Díaz 57
Oscar Díaz 58
25
“Integer” type facets
• totalDigits
• pattern
• whitespace
• enumeration
• maxInclusive
• maxExclusive
• minInclusive
• minExclusive
Oscar Díaz 59
Example
Elements of ‘coordenadasTerrestresT’
can have integer values
… between -1290 and 29035, both inclusive.
Oscar Díaz 60
26
Facet Description
enumeration Defines a list of acceptable values
fractionDigits The maximum number of decimal places allowed. >=0
length The exact number of characters or list items allowed. >=0
maxExclusive The upper bounds for numeric values (the value must be less than
the value specified)
maxInclusive The upper bounds for numeric values (the value must be less than
or equal to the value specified)
maxLength The maximum number of characters or list items allowed. >=0
minExclusive The lower bounds for numeric values (the value must be greater
than the value specified)
minInclusive The lower bounds for numeric values (the value must be greater
than or equal to the value specified)
minLength The minimum number of characters or list items allowed >=0
pattern The sequence of acceptable characters based on a regular
expression
totalDigits The exact number of digits allowed. >0
whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage
returns) is handled
Oscar Díaz 61
Whitespace facet
Ø The "whitespace" facet controls how white space in the element will
be processed
<xs:simpleType name=“addressT>
<xs:restriction base="xs:string">
<xs:whitespace value="replace"/>
</xs:restriction>
</xs:simpleType>
Oscar Díaz 62
27
Derived simple types. Syntax
28
Setting the value of a “facet”
Oscar Díaz 65
Ø Example:
<phoneList>
20003 15037 95977 95945
</phoneList>
Oscar Díaz
66
29
2.2 Structured complex types
Oscar Díaz 67
<sequence>
Sequence: cardinality
restrictions can be set.
<xsd:complexType name=“alumnoT">
30
<choice>
Choice: alternatives
Oscar Díaz 69
Oscar Díaz 70
31
<all>
Oscar Díaz 71
Oscar Díaz 72
32
Types derived from simple types
We want to extend
“integer” with a
<xsd:complexType name ="precioT"> “currency” attribute
<xsd:simpleContent>
<xsd:extension base="xsd:integer">
<xsd:attribute name=“currency" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
Example of element of
type precioT
<precio currency=“dolar">5440</precio>
Oscar Díaz 73
Oscar Díaz 74
33
Types derived from complex types:
use of restrictions
<xsd:complexType name="PublicationT">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:gYear"/>
</xsd:sequence>
SingleAuthorPublicationT has
</xsd:complexType>
PublicationT’s three elements but one
<xsd:complexType name= "SingleAuthorPublicationT"> author only.
<xsd:complexContent> Note that you have to repeat all the
<xsd:restriction base="PublicationT"> elements.
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Author" type="xsd:string“ maxOccurs =“1" />
<xsd:element name="Date" type="xsd:gYear"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
Oscar Díaz 75
cont.
<xsd:complexType name="PublicationT">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Author" type="xsd:string" minOccurs="0“ maxOccurs =“1”/>
<xsd:element name="Date" type="xsd:gYear"/>
</xsd:sequence> If the inherited element is
</xsd:complexType> optional (minOccurs=0), the
<xsd:complexType name= "ZeroAuthorPublicationT"> derived element can remove it.
<xsd:complexContent> To attain this, it is enough not to
<xsd:restriction base="PublicationT"> repeat it.
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:gYear"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
Oscar Díaz 76
34
Substitution mechanisms
Ø Type substitution xsi:type
• similar to OO polimorphism
• one base type can be replaced with any of
its derived types
Oscar Díaz 77
Example
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetnamespace="http://www.books.org" PublicationType is the base type
xmlns="http://www.books.org"
elementFormDefault="unqualified">
<xsd:complexType name="PublicationType">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:year"/>
BookType extends
</xsd:sequence> </xsd:complexType> PublicationType
<xsd:complexType name="BookType">
<xsd:complexContent>
<xsd:extension base="PublicationType">
<xsd:sequence>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:extension> </xsd:complexContent </xsd:complexType>
<xsd:element name="BookStore">
The Publication element is of
<xsd:complexType> type PublicationType
<xsd:sequence>
<xsd:element name="Publication" maxOccurs="unbounded" type="PublicationType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Oscar Díaz 78
35
Type substitution
<?xml version="1.0"?> <?xml version="1.0"?>
<xsd:schema …> <bk:BookStore xmlns:bk="http://www.books.org"
<xsd:complexType name="PublicationType"> xmlns:xsi=“…"
<xsd:sequence> xsi:schemaLocation= "http://www.books.org
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> BookStore.xsd">
<xsd:element name="Date" type="xsd:year"/> <Publication>
</xsd:sequence>
<Title>Staying Young Forever</Title>
</xsd:complexType>
<xsd:complexType name="BookType"> <Author>Karin Jordan, M.D.</Author>
<xsd:complexContent> <Date>1999</Date>
<xsd:extension base="PublicationType"> </Publication>
<xsd:sequence>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/> <Publication xsi:type="bk:BookType">
</xsd:sequence> <Title>The First and Last Freedom</Title>
</xsd:extension>
</xsd:complexContent> <Author>J. Krishnamurti</Author>
</xsd:complexType> <Date>1954</Date>
<xsd:element name="BookStore"> <ISBN>0-06-064831-7</ISBN>
<xsd:complexType> <Publisher>Harper Row</Publisher>
<xsd:sequence>
</Publication>
<xsd:element name="Publication“
maxOccurs="unbounded" type="PublicationType"/>
</xsd:sequence> </bk:BookStore>
</xsd:complexType> The default type is the base type PublicationType
</xsd:element>
</xsd:schema> but any of its derived types can be used
Oscar Díaz 79
Oscar Díaz 80
36
“Final” (applies to complex types)
Ø Aim: Regulates the means by which a type
can be derived
Ø Example:
<xsd:complexType name="PersonT" final=”extension">
...
</xsd:complexType>
Oscar Díaz
81
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://example.org/person" targetnamespace="http://
example.org/person" finalDefault="restriction" >
Oscar Díaz 82
37
Block (applies to elements)
Ø Aim: To avoid substitutes/derivates of an
element
Oscar Díaz 83
Example
<xsd:complexType name="PublicationType"> <?xml version="1.0"?>
<xsd:sequence> <bk:catalogue xmlns:bk="http://www.books.org"
<xsd:element name="Title" type="xsd:string"/> xmlns:xsi="http://www.w3.org/2001/
XMLSchema-instance“
<xsd:element name="Author" type="xsd:string"
maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:year"/> <Publication>
</xsd:sequence> <Title>Staying Young Forever</Title>
</xsd:complexType> <Author>Karin Jordan, M.D.</Author>
<xsd:complexType name="BookType"> <Date>1999</Date>
<xsd:complexContent> </Publication>
<xsd:extension base="PublicationType">
……….</xsd:extension> <Publication xsi:type="bk:BookType">
</xsd:complexContent> <Title>Illusions </Title>
</xsd:complexType> <Author>Richard Bach</Author>
<Date>1977</Date>
<xsd:element name=“catalogue"> <ISBN>0-440-34319-4</ISBN>
<xsd:complexType> <Publisher>Dell Publishing Co.</Publisher>
<xsd:sequence> </Publication>
Avoids Publication instances to be of a a derived type of
<xsd:element name="Publication PublicationType
</bk:BookStore>
maxOccurs="unbounded"
type="PublicationType"
The error is detected when the instance is created.
block="extension"/>
</xsd:sequence>
</xsd:complexType>
Oscar Díaz 84
</xsd:element>
38
Values for “Block”
<xsd:element name=“elementName" type=“typeName” block=“value"/>
Ø Values:
• block="substitution” Forbids element substitution
• block="extension” Forbids type substitution using extension
• block="restriction” Forbids type substitution using derivation
• block="#all” Forbids element and type substitution.
Equivalent to: block=“restriction extension substitution”
Oscar Díaz 85
Ø Example:
• <xsd:complexType name=“TeachingStaffT“ abstract="true">
Oscar Díaz 86
39
VARIABLE CONTENT
CONTAINERS
Oscar Díaz 87
The issue
Ø The X from XML stands for “extensible”
• How to engineer content models for
variability?
Ø Examples:
• A name can be described by either a string or
a compound of firstName and surName
• A catalogue containing books, magazines, …
• A list of items of heterogeneous nature (shirt,
hat, umbrella)
Oscar Díaz 88
40
The approaches
Ø Choice structure
Ø Subtype mechanism
Ø Substitution groups
Oscar Díaz 89
Sample problem 2
Ø Catalogue items can
be
• Book
• Magazine
<Magazine>
<Title>Staying Young Forever</Title>
<Author>Karin Jordan, M.D.</Author>
<Date>1999</Date>
</Magazine>
<Book>
<Title>Illusions </Title>
<Author>Richard Bach</Author>
<Date>1977</Date>
<ISBN>0-440-34319-4</ISBN>
<Publisher>Dell Publishing Co.</Publisher>
</Book>
Oscar Díaz 91
41
Option 1: choice structure
<Catalogue>
<xs:element name=“Catalogue"> <Magazine>
<xs:complexType> <Title>Staying Young Forever</Title>
<Author>Karin Jordan, M.D.</Author>
<xs:sequence> <Date>1999</Date>
<xs:choice> </Magazine>
<xs:element ref=“Magazine"/>
<xs:element ref=“Book"/> <Book>
<Title>Illusions </Title>
</xs:choice> <Author>Richard Bach</Author>
</xs:sequence> <Date>1977</Date>
<ISBN>0-440-34319-4</ISBN>
</xs:complexType> <Publisher>DellPublishing Co.</Publisher>
</xs:element> </Book>
</Catalogue>
Oscar Díaz 93
Oscar Díaz 94
42
Example
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetnamespace="http://www.books.org" PublicationType is the base type
xmlns="http://www.books.org"
elementFormDefault="unqualified">
<xsd:complexType name="PublicationType">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:year"/>
BookType extends
</xsd:sequence> </xsd:complexType> PublicationType
<xsd:complexType name="BookType">
<xsd:complexContent>
<xsd:extension base="PublicationType">
<xsd:sequence>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:extension> </xsd:complexContent </xsd:complexType>
<xsd:element name="BookStore">
The Publication element is of
<xsd:complexType> type PublicationType
<xsd:sequence>
<xsd:element name="Publication" maxOccurs="unbounded" type="PublicationType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Oscar Díaz 95
Type substitution
<?xml version="1.0"?> <?xml version="1.0"?>
<xsd:schema …> <bk:BookStore xmlns:bk="http://www.books.org"
<xsd:complexType name="PublicationT"> xmlns:xsi=“…"
<xsd:sequence> xsi:schemaLocation= "http://www.books.org
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> BookStore.xsd">
<xsd:element name="Date" type="xsd:year"/> <Publication>
</xsd:sequence>
<Title>Staying Young Forever</Title>
</xsd:complexType>
<xsd:complexType name="BookT"> <Author>Karin Jordan, M.D.</Author>
<xsd:complexContent> <Date>1999</Date>
<xsd:extension base="PublicationType"> </Publication>
<xsd:sequence>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/> <Publication xsi:type="bk:BookT">
</xsd:sequence> <Title>The First and Last Freedom</Title>
</xsd:extension>
</xsd:complexContent> <Author>J. Krishnamurti</Author>
</xsd:complexType> <Date>1954</Date>
<xsd:element name="BookStore"> <ISBN>0-06-064831-7</ISBN>
<xsd:complexType> <Publisher>Harper Row</Publisher>
<xsd:sequence>
</Publication>
<xsd:element name="Publication“
maxOccurs="unbounded" type="PublicationType"/>
</xsd:sequence> </bk:BookStore>
</xsd:complexType> The default type is the base type PublicationType
</xsd:element>
</xsd:schema> but any of its derived types can be used
Oscar Díaz 96
43
Option 3: substitutionGroup
• Purpose: Group of elements that can be used wherever the
base element shows.
• Example:
<xsd:element name=“item" type=”publicationT"/>
<xsd:element name=“magazine" substitutionGroup=“publicationT"
type=”magazineT"/>
<xsd:element name=“book" substitutionGroup=“publicationT" type=”bookT"/>
<?xml version="1.0"?>
<xsd:schema …xmlns:bk="http://www.books.org>
<xsd:complexType name=”publicationT">
…
</xsd:complexType>
<xsd:complexType name=”magazineT">
…
</xsd:complexType>
<xsd:complexType name=“bookT">
…
</xsd:complexType>
<xsd:element name=”Item” type:”bk:publicationT>
<xsd:element name=”Magazine” type:”bk:magazineT>
<xsd:element name=”Book” type:”bk:BookT>
Oscar Díaz 98
44
<?xml version="1.0"?>
<bk:BookStore xmlns:bk=“http://www.books.org “>
<Magazine>
<Title>Staying Young Forever</Title>
<Author>Karin Jordan, M.D.</Author>
<Date>1999</Date>
</Magazine>
<Book>
<Title>The First and Last Freedom</Title>
<Author>J. Krishnamurti</Author>
<Date>1954</Date>
<ISBN>0-06-064831-7</ISBN>
<Publisher>Harper Row</Publisher>
</Book>
</bk:BookStore>
Oscar Díaz 99
45
Recap: Abstract type and type
substitution
Ø Only one element
Ø Semantic cohesion (subtypes)
• Limited structural variability
Ø Extensibility PublicationType (abstract)
• Other schemas can extend the element set
BookType MagazineType
<Catalogue>
- variable content section Publication (abstract)
</Catalogue>
"substitutable for"
<Magazine>
46
Recap: choice element
<Catalogue>
- variable content section <Book>
</Catalogue>
”no semantically related"
<Bicycle>
Oscar Díaz
DATABASE-LIKE
RESTRICTIONS
47
“Null” value
<xsd:element name="PersonName">
<xsd:complexType>
<xsd:element name="forename" type="xsd:string"/>
<xsd:element name="middle" type="xsd:string" nillable="true"/>
<xsd:element name="surname" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
48
In the Spanish libraries, there cannot
be two books with the same ISBN
<xsd:element name=“SpanishLibraries">
<xsd:complexType>
<xsd:sequence>
<xsd:element name=“Library">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
…
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="PK">
<xsd:selector xpath=“bk:Library/bk:Book"/>
<xsd:field xpath="bk:ISBN"/>
</xsd:key>
</xsd:element>
Oscar Díaz 110
49
Element uniqueness: <unique>
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetnamespace="http://www.books.org"
xmlns="http://www.books.org"
xmlns:bk="http://www.books.org"
elementFormDefault="qualified">
<xsd:element name=“Library">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
………… Unlike the key
</xsd:element>
</xsd:sequence>
restriction, unique
</xsd:complexType> allows for nulls
<xsd:unique name="UNIQ">
<xsd:selector xpath="bk:Book"/>
<xsd:field xpath="bk:ISBN"/>
</xsd:unique>
</xsd:element>
</xsd:schema>
50
UML & XML SCHEMA
51
Example
Class Association
name, optional name, 2..* ends
abstract
AssociationEnd
role name, min..max,
navigability, type
CatalogItem
Catalog
name : string Organization
name : string
+item description : string +supplier
description : string name : string
listPrice : Money
startDate : date * 1 addressLine [0..3] : string
0..* sku : string
endDate : date
globalIdentifier : string
1..*
+contains Attribute
name [min..max] : type
<xs:simpleType name="unitOfMeasure">
<<enumeration>>
<xs:restriction base="xs:string">
UnitOfMeasure
<xs:enumeration value="each"/>
each
<xs:enumeration value="dozen"/>
dozen
meter <xs:enumeration value="meter"/>
kilogram <xs:enumeration value="kilogram"/>
</xs:restriction>
</xs:simpleType>
52
Class à <sequence> type
<xs:element name="Organization”
type=”OrganizationT"/>
Organization
name : string <xs:complexType name="OrganizationT">
addressLine [0..3] : string <xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="addressLine"
type="xs:string"
minOccurs="0" maxOccurs="3” />
</xs:sequence>
</xs:complexType>
53
Class à type with attribute
54
Attribute vs. Element. Guidelines
Ø Guide: syntactic restrictions
Ø Guide: data vs. meta-data
Ø Guide: target audience
55
Guide: data vs. meta-data
Ø element
• à data, essential part of the document
• à if there are other elements that qualify or describe it
Ø attribute
• à meta-data, helps to understand, process, classify
the document (e.g. author, creationDate, identifier..)
<movie year= “1997”>
<title>The Devil's Advocate</title>
<still>http://www.movies.org/devilsAdvocate.jpg</still>
<producer >
<sex>M</sex>
<name>Warner Bros</name>
</producer>
<actor> …. </actor>
Oscar Díaz <actor> …. 124 </actor>
</movie>
56
Association à element
CatalogIte
m
name : string Organization
description : string +supplier
listPrice : Money name : string
* 1
n addressLine [0..3] : string
sku : string
globalIdentifier : string
Specialization à substitutionGroup
<xs:element name="Product" type=”ProductT"
CatalogItem substitutionGroup=”CatalogItem” />
name : string
description : string
listPrice : Money
<xs:complexType name="ProductT">
sku : string
globalIdentifier : string <xs:complexContent>
<xs:extension base=”CatalogItemT">
<xs:all>
<xs:element name="photoURL"
type="xs:uriReference"/>
Product Service <xs:element name="units"
photoURL : uriReference units : UnitOfTime
units : UnitOfMeasure
type="cml:unitsOfMeasure"/>
</xs:all>
</xs:extension>
</xs:complexContent>
</xs:complexType>
57
HANDLING SCHEMA
COMPLEXITY
Schema complexity
A schema can be very complex
58
Different schemas.
Perspective from the schema document
<include>
targetnameSpace: http://www.library.org
LibraryBook.xsd LibraryEmployee.xsd
library.xsd
<xsd:include schemaLocation="LibraryBook.xsd"/>
<xsd:include schemaLocation="LibraryEmployee.xsd"/>
59
<include>.
Schema perspective
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetnamespace="http://www.library.org"
xmlns="http://www.library.org"
elementFormDefault="qualified">
<xsd:include schemaLocation="LibraryBook.xsd"/>
<xsd:include schemaLocation="LibraryEmployee.xsd"/>
<xsd:element name="Library">
targetnamespace
<xsd:complexType>
<xsd:sequence> All the included schemas must have the
<xsd:element name="Books"> same targetnamespace
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Book" maxOccurs="unbounded"/>
</xsd:sequence> <include>
</xsd:complexType> Has the same effect as if the schema was
</xsd:element>
<xsd:element name="Employees"> specified in this file. It can be
<xsd:complexType> overwritten.
<xsd:sequence>
<xsd:element ref="Employee" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType> Use of included elements
</xsd:element> An included element/attribute is indicated
Oscar Díaz </xsd:sequence> 133 by reference.
</xsd:complexType>
</xsd:element>
</xsd:schema>
<include>.
Instance perspective
<?xml version="1.0"?>
<Library xmlns="http://www.library.org”>
<Books>
<Book> The instance document indicates
<Title>My Life and Times</Title>
<Author>Paul McCartney</Author> the www.library.org vocabulary.
<Date>1998</Date>
<ISBN>1-56592-235-2</ISBN>
<Publisher>Macmillan Publishing</Publisher>
</Book>
<Book>
<Title>The First and Last Freedom</Title>
<Author>J. Krishnamurti</Author>
<Date>1954</Date>
<ISBN>0-06-064831-7</ISBN>
<Publisher>Harper & Row</Publisher>
</Book>
</Books>
<Employees> To sum up. <include> allows a
<Employee> modular approach when defining the
<name>John Doe</name>
<SSN>123-45-6789</SSN>
same namespace.
</Employee>
</Employees>
</Library>
Oscar Díaz 134
60
<import>
Camera.xsd
<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"/>
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetnamespace="http://www.nikon.com"
xmlns="http://www.nikon.com"
elementFormDefault="qualified">
<xsd:complexType name="body_type">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
Nikon.xsd </xsd:sequence>
</xsd:complexType>
</xsd:schema>
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetnamespace="http://www.olympus.com"
xmlns="http://www.olympus.com"
elementFormDefault="qualified">
<xsd:complexType name="lens_type">
Schemas have different
Olympus.xsd
<xsd:sequence> targetnameSpaces
<xsd:element name="zoom" type="xsd:string"/>
<xsd:element name="f-stop" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetnamespace="http://www.pentax.com"
xmlns="http://www.pentax.com"
Pentax.xsd elementFormDefault="qualified">
<xsd:complexType name="manual_adapter_type">
<xsd:sequence>
<xsd:element name="speed" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Oscar Díaz 136
61
<import>
Schema perspective
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"targetnamespace
targetnamespace="http://www.camera.org" The imported schemas DON’T have the
xmlns:nikon="http://www.nikon.com" same targetnamespace
xmlns:olympus="http://www.olympus.com"
xmlns:pentax="http://www.pentax.com"
elementFormDefault="qualified">
<xsd:import namespace="http://www.nikon.com"
schemaLocation="Nikon.xsd"/> <import>
<xsd:import namespace="http://www.olympus.com" Imports the definitions As they have
schemaLocation="Olympus.xsd"/> different namespaces, we have to indicate
<xsd:import namespace="http://www.pentax.com"
schemaLocation="Pentax.xsd"/>
where is each of them located.
<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> Including imported elements
</xsd:element>
Using qualifiers
<xsd:schema>
<import>
Instance perspecitve
<?xml version="1.0"?>
The instance document must specify
<c:camera xmlns:c="http://www.camera.org"
xmlns:nikon="http://www.nikon.com" ALL the namespaces
xmlns:olympus="http://www.olympus.com"
xmlns:pentax="http://www.pentax.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation= However, it is enough to locate the
"http://www.camera.org
“importing” schema, as it already has
Camera.xsd">
<c:body> the other locations.
<nikon:name>Ergonomically designed casing for easy handling</nikon:name>
</c:body>
<c:lens>
<olympus:zoom>300mm</olympus:zoom>
<olympus:f-stop>1.2</olympus:f-stop>
</c:lens>
<c:manual_adapter> To sum up. <import> allows using
<pentax:speed>1/10,000 sec to 100 sec</pentax:speed>
other vocabularies when definig a
</c:manual_adapter>
</c:camera> vocabulary.
Oscar Díaz 138
62
<redefine>
<xsd:complexType name="BookType">
<xsd:sequence> <redefine>
<xsd:element name="Title" type="xsd:string"/> The same as <include>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/> but it also allows
<xsd:element name="ISBN" type="xsd:string"/> redefining any
<xsd:element name="Publisher" type="xsd:string"/> “component”
</xsd:sequence> • simpleType,
</xsd:complexType> LibraryBook.xsd
• complexType,
<xsd:redefine schemaLocation="LibraryBook.xsd"> • attributeGroup
<xsd:complexType name="BookType"> • group
<xsd:complexContent>
<xsd:extension base="BookType">
<xsd:sequence> Any reference to
<xsd:element name="Summary" type="xsd:string"/> BookType both in
</xsd:sequence> Library.xsd and
</xsd:extension> LibraryBook.xsd will be to
</xsd:complexContent>
</xsd:complexType> the new redefined type.
Library.xsd
</xsd:redefine>
Oscar Díaz 139
63
EXTENSIBILITY POINTS
Roles
Ø Global schema author
• (e.g. a standarisation committee)
Ø Local schema author
• (e.g. a local authority, the company’s DB
administrator)
Ø Document instance author
• (e.g. a programmer)
Ø Notice that
1. the author of the schema (.xsd) and of the
instance (.xml) are usually different
2. Global schemas tend to be adapted for local use
Oscar Díaz 143
64
Extensibility points: the <any>
element
Ø They allow documents to contain additional
elements that are not declared in the main
XML schema.
65
Role “document author”
what do I want to extend the schema with?
Ø He defines a NEW schema with his own
elements/attributes
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetnamespace="http://www.myOwnSchema.org">
<element name =“Reviewer”>
<complexType> Thus:
<sequence> 1. creates his own vocabulary:
<element name="name"> www.myOwnSchema.org
<complexType>
<sequence>
<element name="First" type="xsd:string"/>
<element name="Last" type="xsd:string"/>
</sequence>
</complexType>
</element>
</sequence> </complexType> </element>
<attribute name=”recommndable" type="xsd:boolean"/>
</xsd:schema>
Oscar Díaz 146
66
Restrincting the vocabularies that
can extend the schema
<anyAttribute namespace="##any"/> allows any new attribute.
Default value.
<anyAttribute namespace="http://www.somewhere.com"/>
allows new attributes, only if they come from the
specified vocabulary.
Note: more than one can be specified, separated by
spaces.
<anyAttribute namespace="##targetnamespace"/>
allows attributes from the vocabulary that is being
defined (targetnamespace).
<anyAttribute namespace="##other"/>
allows the instances to have new attributes, providing
that those attributed are NOT defined in the
targetnamespace.
<anyAttribute namespace="##local"/>
allows any attribute that is NOT defined in any
vocabulary.
* exacty the same for <any>
Oscar Díaz 148
67