4 XML Schema English

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 67

XML SCHEMA

Oscar Díaz
Dpto. de Lenguajes y Sistemas Informáticos

The big picture!


Sender Receiver
Composition and Validation Parsing, Validation, & Translation
(Message Preparation) (Message Processing)
XML
XML document Schema
Guidance Data entry

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

Ø  Acknowledgments. Part of these slides have been prepared


using R.L. Costello’s material
Oscar Díaz 3

Motivation: Is this document valid?

<location>
<latitude>32.904237</latitude>
<longitude>73.620290</longitude>
<uncertainty unidads="meters">2</uncertainty>
</location>

To be valid it must satisfy the following conditions:


1.  Location is composed of latitude followed by longitude and then an
indication of the precision of these measures
2.  Latitude must be a decimal between -90 and +90
3.  Longitude must be a decimal between -180 and +180
4.  Both latitude and longitude must have exactly six numbers after the
decimal point
5.  The precision value must be positive
6.  Precision is measured in meters or feet
All these restrictions can be expressed with XML Schema
Oscar Díaz 4

2
Who validates?
BEFORE: The receiving application

Code to carry
out the function

Code to check
structure and
content of data

On average, up to 60% of the code is used


to verify the data
Oscar Díaz 5

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

Restrictions (schema) described using XML Schema


-check that latitude is between -90 and +90
-check that longitude is between -180 and +180
...

Oscar Díaz 6

3
Externalization of validation
Ø Validator: special purpose engine
Ø Application: focus on the business logic
BEFORE AFTER

Code to carry out Code to carry out


the function the function

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 elements MUST appear

Ø  which elements can appear as part of another


element

Ø  what attributes can appear or must appear

Ø  what kind of values can/must be in an attribute.


Oscar Díaz 9

What is XMLSchema?

Ø  W3C standard to define schemas


(recommendation since May 2001)

Ø  This namespace allows specifying a schema, i.e.:


•  structure of data
•  type of each element/attribute

Ø  The schema is also an XML document

Oscar Díaz 10

5
SCHEMA =
IDENTIFIER + VOCABULARY

Oscar Díaz 11

Schema = identifier + vocabulary


Ø  Schema or vocabulary or namespace
Ø  This vocabulary is used to describe “instance” documents

BookStore
Author
Book
Namespace Title ISBN
Publisher
Date

Namespace identifier http://www.books.org

Oscar Díaz 12

6
Schema = identifier + vocabulary
Ø  A namespace is only a string… unique in the
whole wide world

Ø  Syntax: Uniform Resource Identifier (URI) Norm

Ø  Two options


•  Uniform Resource Locator (URL)
–  http://www.onekin.org/miVocabulario
•  Uniform Resource names (URN)
–  urn:www-onekin-org:miVocabulario

Oscar Díaz 13

Identifier: uniqueness

Ø  A namespace must guarantee that each term defined


in the space is unique

Ø  But, to single out each “term” in the “whole wide


world”, the namespace identifier must be unique too.

Ø  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

Uniqueness guaranteed by Uniqueness guaranteed by


the “Internet Naming yourself
Authority”

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

Option 2: using a qualifier


<?xml version="1.0"?>
<BookSeller xmlns="http://www.BookRetailers.org"
xmlns:bns="http://www.books.org“ />
<Book bns:id="P.M."> The qualifier indicates that we
<Title>My Life and Times</Title>
<Author>Paul McCartney</Author> are using the “www.books.org”
<Date>1998</Date> vocabulary.
<ISBN>1-56592-235-2</ISBN>
<Publisher>McMillin Publishing</Publisher>
<bns: Reviewer
<name>
<First>Roger</First>
<Last>Costello</Last>
</name>
</bns:Reviewer> The subelements that are NOT
</Book>
qualified are automatically
<Book bns:id="R.B.">
qualified in the same way as the
<Title>Illusions: The Adventures of a Reluctant Messiah</Title>
<Author>Richard Bach</Author> element that contains them.
<Date>1977</Date>
<ISBN>0-440-34319-4</ISBN>
<Publisher>Dell Publishing Co.</Publisher>
Exception: attributes, they have to
</Book> always
be qualifed explicitly.
</BookSeller>
Oscar Díaz 19

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

Ø  Refer to http://www.w3.org/TR/


xmlschema-1/#schema-loc

Oscar Díaz 22

10
THE SCHEMA AS
AN XML DOCUMENT

Oscar Díaz 23

Schema is a XML document


Ø  Extension: “.xsd”

Ø  Being an XML document,


•  the XML rules must be followd
•  an XML editor can be used to write them
•  DOM can be used to manipulate them
•  XSLT can be used to transform them

•  Its structure/vocabulary is dictated by a (meta)


schema defined by W3C

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

What can be found in a schema?


Ø Elements <?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetnamespace="http://www.books.org“ …>
<element name="BookStore">
<complexType>

Ø Atributes <sequence>
<element name="Book" maxOccurs="unbounded">
<complexType>
<sequence>
<element name="Title" type="xsd:string"/>

Ø Simple types <element name="Author" type="xsd:string"/>


<element name="Date" type="xsd:string"/>
<element name="ISBN" type="xsd:string"/>
<element name="Publisher" type="xsd:string"/>
</sequence>

Ø Complex types <xsd:attribute name="href" type="xsd:anyURI" use="required"/>


</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>

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"/>

required The "use" attribute must be


xsd:string
optional “optional” if “default” or
xsd:integer
prohibited* “fixed” are used.
xsd:boolean
...
(*) Only when restricting complex types.

<xsd:attribute name="name" use="how-its-used" default/fixed="value">


2 <xsd:simpleType>
<xsd:restriction base="simple-type">
<xsd:facet value="value"/>

</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
Oscar Díaz 31

13
Examples

<xs:attribute name="language" type="xs:string" default="EN"/>

<xs:attribute name="language" type="xs:string" fixed="EN"/>

<xs:attribute name="language" type="xs:string" use="required"/>

Oscar Díaz 32

ELEMENTS

Oscar Díaz 33

14
Element declaration: content model

Ø  Simple content model


•  Only text
name

‘juan tarco’

Ø  Complex content model name

•  With attributes ‘juan tarco’ @language

‘castellano’

name
•  With subelements
firstName lastName

Oscar Díaz 34
‘juan’ ‘tarco’

Simple content model

<nombre> juan tarco </nombre>


nombre

‘juan tarco’

<xsd:element name=“nombre" type=“xsd:string" minOccurs="int" maxOccurs="int"/>

{0,1…} {0,1… “unbounded”}

Oscar Díaz 35

15
Complex content elements
<product pid="1345"/>
<product pid="1345"/>

<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>

<food type="dessert">Ice cream</food>

<description>
It happened on <date lang="norwegian">03.03.99</date> ....
</description>
Oscar Díaz

Complex content model with


attributes
<nombre language = “castellano”>
juan tarco
nombre
</nombre>
‘juan tarco’ @language

Ø  <xs:element name=“nombre"> ‘castellano’


<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name=“language" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>

Oscar Díaz 37

16
Complex content model with
subelements
<nombre>
<firstName> juan </firstName>
<lastName> tarco</lastName> nombre

</nombre> firstName lastName

‘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

Complex content model with mixed


content
<nombre>
El alumno con nombre <firstName> juan </firstName> y apellido
<lastName> tarco</lastName> se ha matriculado
nombre
</nombre>
“El alumno con nombre” firstName lastName

‘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”/>

Ø  Permits reuse <xsd:element name="Title" type="xsd:string"/>


<xsd:element name="Author" type="xsd:string"/>
<xsd:complexType name=“BookT”>
<xsd:sequence>
<xsd:element ref="Title" minOccurs="1" maxOccurs="1"/>
Ø  Cardinalities are <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/>
set where the </xsd:sequence>
</xsd:complexType>
element is reused </xsd:schema>

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”/>

Ø  Anonymous types <xsd:element name="Book">


•  Those that have no name, <xsd:complexType>
and they are used in line <xsd:sequence>

•  In the example, the type of </xsd:sequence>


Book is anonymous </xsd:complexType>
</xsd:element>

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

Ø  Complex types


•  Complex types with attributes or subelements
•  Complex derived types with simple content
•  Complex derived types with complex content
•  Complex types with mixed content
Oscar Díaz 45

Simple types
Ø Simple type elements
•  Only have content
•  No structure
•  No attributes

Ø Example: only ISBN, First and Last are


simple type elements
<ISBN>1-56592-235-2</ISBN>
<Publisher country = “Spain” >McMillin Publishing</Publisher>
<name>
<First>Roger</First>
<Last>Costello</Last>
</name>
Oscar Díaz 47

20
(some) Basic simple types

•  string •  "Hello World"


•  boolean •  {true, false, 1, 0}
•  decimal •  7.08
•  float •  12.56E3, 12, 12560, 0, -0, INF, -INF, NAN
•  double •  12.56E3, 12, 12560, 0, -0, INF, -INF, NAN
•  duration •  P1Y2M3DT10H30M12.3S
•  dateTime •  format: CCYY-MM-DDThh:mm:ss
•  time •  format: hh:mm:ss.sss
•  date •  format: CCYY-MM-DD
•  gYearMonth •  format: CCYY-MM
•  gYear •  format: CCYY
•  gMonthDy •  format: --MM-DD

Note: 'T' is the date/time separator


INF = infinity
NAN = not-a-number

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

Ø  IDREF untyped: one has no control over what it points to --


you point to something, but you don’t know what it is!
•  <character-refs> ch-snoopy isbn-23740192374 </character-refs>

Ø  IDs are based on a single element


•  While keys can be based on more that one attribute (e.g. enroll
(sid: string, cid: string, grade:string))
Ø  An element can have at most one ID (primary)
•  While a relation may have multiple keys

Oscar Díaz 53

1.1 Simple types derived from basic


types
Ø  Each type has a set of valid values
Ø  A type can be derived from another type (the “base”
type) by restricting its value set.
Ø  This restriction is expressed in terms of “facets” of
the basic type

Ø  Example “facets” of the “string” type


•  length
•  minLength
•  maxLength
•  pattern
•  enumeration
•  whitespace ({preserve, replace, collapse})

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>

An element of type “shapeT” can only hold a


string out of {cicle, triangle, square}

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>

• The new ‘TelephoneNumberT’ type is created.


• Elements of this type contain “strings”
• But the length of the “string” is restricted to 8 characters and
• The “string” must follow the ddd-dddd pattern, where 'd' corresponds to a 'dígit'.
(In this example the regular expression makes the length restriction redundant.)

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>

The regular expression restricts passwords


to be eight-length alpha-numeric characters

Oscar Díaz 57

Some pattern examples


Regular expression Example
- Chapter \d - Chapter 1
- a*b - b, ab, aab, aaab, …
- [xyz]b - xb, yb, zb
- a?b - b, ab
- a+b - ab, aab, aaab, …
- [a-c]x - ax, bx, cx

•  [-ac]x •  -x, ax, cx


•  [ac-]x •  ax, cx, -x
•  [^0-9]x •  any non-digit char followed by x
•  \Dx •  any non-digit char followed by x
•  Chapter\s\d •  Chapter followed by a blank followed by a digit
•  (ho){2} there •  hoho there
•  (ho\s){2} there •  ho ho there
•  .abc •  any (one) char followed by abc
•  (a|b)+x •  ax, bx, aax, bbx, abx, bax,...

Oscar Díaz 58

25
“Integer” type facets
•  totalDigits
•  pattern
•  whitespace
•  enumeration
•  maxInclusive
•  maxExclusive
•  minInclusive
•  minExclusive

Oscar Díaz 59

Example

<xsd:simpleType name= “coordinadasTerrestresT">


<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="-1290"/>
<xsd:maxInclusive value="29035"/>
</xsd:restriction>
</xsd:simpleType>

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

Ø  Three possible values:


•  "preserve" causes the processor to keep all whitespace as-is
•  "replace" causes the processor to replace all whitespace characters
(tabs, carriage returns, line feeds, spaces) with space characters
•  "collapse" causes the processor to replace all strings of whitespace
characters (tabs, carriage returns, line feeds, spaces) with a single
space character

<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

<xsd:simpleType name= "name">


<xsd:restriction base= "xsd:source">
<xsd:facet value= "value"/>
<xsd:facet value= "value"/>

</xsd:restriction>
</xsd:simpleType>
Facets:
- length Sources:
- minlength - string
- maxlength - boolean
- pattern - number
- enumeration - float
- minInclusive - double
- maxInclusive - duration
- minExclusive - dateTime
- maxExclusive - time
Oscar Díaz ... 63 ...

1.2 Simple types derived from other


derived types
Ø  A derived type can be used as the “base” type.
Ø  The new type must be more restrictive than the “base” type.

<xsd:simpleType name= “latitudT">


<xsd:restriction base="xsd:integer">
<xsd:minInclusive value=”-90"/>
<xsd:maxInclusive value=“90"/>
</xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name= “latitudAragonT">


<xsd:restriction base=“latitudT">
<xsd:minInclusive value=”-10"/>
<xsd:maxInclusive value=”40"/>
</xsd:restriction>
</xsd:simpleType>
Oscar Díaz 64

28
Setting the value of a “facet”

<xsd:simpleType name= “edadAlumnoT">


<xsd:restriction base="xsd:nonNegativeInteger">
<xsd:minInclusive value="18" fixed="true"/>
<xsd:maxInclusive value=“120"/>
</xsd:restriction>
</xsd:simpleType>

Those types derived from edadAlumnoT cannot change


the lower limit

Oscar Díaz 65

1.3 Simple type lists


Ø “Simple type list” types are defined using
the “list” (meta) element
<xsd:simpleType name=”phoneListT">
<xsd:list itemType="myInteger"/>
</xsd:simpleType>

Ø Example:
<phoneList>
20003 15037 95977 95945
</phoneList>
Oscar Díaz
66

29
2.2 Structured complex types

Ø The way sub-elements are structured is set


through restrictions:

•  all: all subelements must be present

•  sequence: all subelements must be present in


a given orden

•  choice: choices of subelements possible

Oscar Díaz 67

<sequence>

Sequence: the order is


meaningful

Sequence: cardinality
restrictions can be set.
<xsd:complexType name=“alumnoT">

<xsd:sequence minOccurs="0" maxOccurs="unbounded">


<xsd:element name=“nombre" type="xsd:string"/>
<xsd:element name=“apellido" type="xsd:string"/>
<xsd:element name=“edad" type="edadT"/>
<xsd:element name=“curso" type="cursoT"/>
</xsd:sequence>
</xsd:complexType>
Oscar Díaz 68

30
<choice>

Choice: alternatives

<xsd:complexType name=“medioComunicacionT"> >


<xsd:choice>
<xsd:element name=“telefono" type=“telefonoT"/>
<xsd:element name=“correoElectronico" type=“correoT"/>
<xsd:element name=“direccionPostal" type=“direccionT"/>
</xsd:choice>
</xsd:complexType>

Oscar Díaz 69

<sequence> & <choice>


Sequence, choice: they can
<xsd:complexType name="lifeT"> > be nested
<xsd:sequence minOccurs="0" maxOccurs="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>
<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:attribute ref="Category" use="required"/>
</xsd:complexType>

Attributes are defined after sub-elements

Oscar Díaz 70

31
<all>

Elements inside <all>:


maxOccurs = "1“,
minOccurs = "0" or "1“

<all> cannot be nested to


<sequence>, <choice>, or
<xsd:complexType name="BookT”> another <all>
<xsd:all>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>Content of <all> must be
<xsd:element name="Date" type="xsd:string"/> elements. <sequence> and
<xsd:element name="ISBN" type="xsd:string"/><choice> are NOT allowed.
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:all>
</xsd:complexType>

Oscar Díaz 71

2.4 Complex derived types


Ø  Can be derived from:
•  a simple type: <simpleContent>
•  a complex type: <complexContent>
Ø  In both cases the result is a complex type

<xsd:complexType name="…"> <xsd:complexType name="…">


<xsd:complexContent> <xsd:simpleContent>
<extension base="X"> <extension base="Y">
… …
</extension> </extension>
</xsd:complexContent> </xsd:simpleContent>
</xsd:complexType> </xsd:complexType>
X must be a complexType Y must be a simpleType

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

Types derived from complex types:


use of extension
<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>
</xsd:complexType > BookPublicationT has PublicationT
elements + its own.
<xsd:complexType name="BookPublicationT">
<xsd:complexContent> The latter are always added at the end.
<xsd:extension base="PublicationT">
<xsd:sequence>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType >

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

Controlling type derivation (and


substitution)
Ø Three properties for controling derivation:

•  final: limits the definition of derived types in


schemas

•  block: limits the definition of derived types in


instances

•  abstract: forces the definition of derived types

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>

• The designer prevents other people from creating


types derived from “PersonT” through extension.

Oscar Díaz
81

Values for “Final”


Ø  Example:

<xsd:complexType name="PersonT" final="extension">


...
</xsd:complexType>

Ø  Values: {extension, restriction, substitution, #all}

Ø  A default value can be assigned at schema level

<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

<xsd:element name="Publication” maxOccurs="unbounded"


type="PublicationT"
block=“extension"/>
</xsd:sequence>

•  “Publication” can hold “PublicationT” instances except


those whose type is a extension-constructed subtype
of “PublicationT”

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”

Ø  A default value can be assigned at schema level:


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tns=http://example.org/person
targetnamespace="http://example.org/person"
blockDefault="#all" >

Oscar Díaz 85

Abstract (applies to types)


Ø  Purpose: Abstract types cannot be instantiated.
•  defines a specification that others will implement. At run time, the
abstract type is replaced by one of its derived types.

Ø  Example:
•  <xsd:complexType name=“TeachingStaffT“ abstract="true">

•  “ReaderT”, “LecturerT”, “ProfessorT” derivates from “TeachingStaffT”

•  there cannot be instances of TeachingStaffT, but there could be of


“ReaderT”, “LecturerT”, “ProfessorT”

•  A taughtBy element of type TeachingStaffT can hold instances of one of


its derived types.

–  Thus, the designer of taughtBy abstracts away from the different


“TeachingStaffT ” situations that could exist

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

Option 2: Type ubstitution


Ø Type substitution xsi:type
•  similar to OO polimorphism
•  one base type can be replaced with any of
its derived types

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"/>

•  Wherever the <item> element appears, any of its


substitutes can appear.
•  Substitution relation is transitive, but not commutative.
•  Substitute elements have to be global.
•  The type of substitute elements has to be derived from the
type of the element to be substitued.
Oscar Díaz 97

<?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>

<xsd:element name=”BookStore” >


<xsd:complexType>
<xsd:sequence>
<xsd:element ref=“bk:Publication maxOccurs="unbounded” />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

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

Attributes for substitution groups


Ø  abstract, to define abstract elements inside the
substituion group

<xsd:element name=“house" type="xsd:string“ abstract =“true”/>


•  There cannot exist documents with “house”. We can only use
one of the substitutes.

Ø  final, to avoid the definition of substitutes

<xsd:element name=“maison" substitutionGroup=“house"


type="xsd:string“ final =“substitution” />
•  There cannot be substitutes for the “maison” element

Oscar Díaz 103

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

Only <Publication> elements but with variable content


<Catalogue>
<Publication xsi:type="…">
- variable content section
</Publication>
</Catalogue>
Oscar Díaz

Recap: Abstract element and


element substitution
Ø  Diverse elements
Ø  Semantic cohesion (subtypes)
•  Limited structural variability
Ø  Extensibility <Book>
•  Other schemas can extend the element set
"substitutable for"

<Catalogue>
- variable content section Publication (abstract)
</Catalogue>
"substitutable for"

<Magazine>

Oscar Díaz substitutionGroup

46
Recap: choice element

Ø  Diverse elements


Ø  No semantic coherence
•  Items are not type related Group
•  Catalogue can be of books, bikes, refrigerators
Ø  Non extensible (for read-only schemas)
<Refrigerator>
•  You can not add new item types to the catalogue
”no semantically related"

<Catalogue>
- variable content section <Book>
</Catalogue>
”no semantically related"

<Bicycle>

Oscar Díaz

DATABASE-LIKE
RESTRICTIONS

Oscar Díaz 107

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>

“middle” can contain the


“null” value
<PersonName>
<forename>John</forename>
<middle xsi:nil="true"/>
<surname>Doe</surname>
</PersonName>

John does not have <middle>. “null” assignment


It is not that I forgot to put it
Oscar Díaz 108

Key definition: <key>


<?xml version="1.0"?> We want to guarantee that a library
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
doesn’t have two books with the same
targetnamespace="http://www.books.org"
xmlns="http://www.books.org" ISBN inside a BookStore element
xmlns:bk="http://www.books.org"
elementFormDefault="qualified"> Inside the BookStore element, the
<xsd:element name=“Library"> following key is defined
<xsd:complexType> • name: PK
<xsd:sequence> • inside: Book
<xsd:element name="Book" maxOccurs="unbounded">• on field: ISBN
<xsd:element name=”isbn"
type="xsd:string" minOccurs =“1"/>
</xsd:element> The key element MUST be compulsory
</xsd:sequence> and not null i.e.
</xsd:complexType> (minOccurs > 0, nillable="false")
<xsd:key name="PK">
<xsd:selector xpath=“bk:Book"/> Keys always go at the end of the
<xsd:field xpath="bk:ISBN"/> element declaration on which they are
</xsd:key> defined.
</xsd:element>
</xsd:schema>
Oscar Díaz 109

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

In a meeting, there cannot be two people


with the same firstname+lastname
<xsd:element name=“Meeting">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Participants" >
<xsd:complexType>
<xsd:sequence>
<xsd:element name=”Participant" minOccurs="0" maxOccurs="unbounded">
<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> A key can be
</xsd:complexType> defined on more
</xsd:element>
</xsd:sequence> than one elements
</xsd:complexType>
<xsd:key name="PK">
<xsd:selector xpath=“ns1:Participants/reunion:Participant"/> Inside XPath always
<xsd:field xpath=“ns1:First"/> qualify the elements
<xsd:field xpath=“ns1:Last"/>
</xsd:key>
</xsd:element>
</xsd:schema>
Oscar Díaz 111

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>

Oscar Díaz 112

Foreing keys: <keyref>


<xsd:element name=“Department">
<xsd:complexType>
<xsd:sequence>
<xsd:element name=”Lecturers">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref=”Lecturer" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element ref=“HeadOfDepartment"/>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="PK"> HeadOfDepartment is a
<xsd:selector xpath="bk:Lecturers/bk:Lecturer"/>
foreign key on Lecturer in
<xsd:field xpath="bk:dni"/>
</xsd:key> the Department context
<xsd:keyref name="isbnRef" refer=“PK">
<xsd:selector xpath="bk:HeadOfDepartment"/>
<xsd:field xpath="bk:dni"/>
</xsd:keyref>
</xsd:element>
Oscar Díaz 113

50
UML & XML SCHEMA

Oscar Díaz 114

UML and XML Schema

Ø  XML Schema is code à implementation tool

Ø  Use UML to design document schemas


•  UML is visual à design tool

Ø  There is controversy about the correspondence between


the primitives of UML and XML Schema

Ø  Here we show some examples taken from David Carlson,


Ontogenics Corp.

Oscar Díaz 115

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

ProductBundle Product Service


0..* photoURL : uriReference units : UnitOfTime
units : UnitOfMeasure Generalization
child, parent

Oscar Díaz 116

enumeration à simple type with


restriction

<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>

Oscar Díaz 117

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>

Oscar Díaz 118

Class à <all> type


<xs:complexType name="CatalogItemT">
<xs:all>
<xs:element name="name" type="xs:string"/>
<xs:element name=“description" type="xs:string"/>
CatalogItem <xs:element name="listPrice">
name : string <xs:complexType>
description : string <xs:sequence>
listPrice : set of Money <xs:element ref="cml:Money"/>
sku : string </xs:sequence>
globalIdentifier : string </xs:complexType>
</xs:element>
<xs:element name="sku" type="xs:string"/>
<xs:element name="globalIdentifier" type="xs:string"/>
</xs:all>
</xs:complexType>

Oscar Díaz 119

53
Class à type with attribute

<xsd:complexType name = “ProductT>


Product
<xsd:attribute ref=“photoURL" use="required"/>
photoURL : uriReference
units : UnitOfMeasure
<xsd:attribute ref=“units" use="required"/>
</xsd:complexType>

Oscar Díaz 120

Some data, different representations


<movie>
<title>The Devil's Advocate</title>
<still>http://www.movies.org/devilsAdvocate.jpg</still>
<year>1997</year>
<producer>
<sex>M</sex>
<name>Warner Bros</name>
<movie title=“…” still= “…” year=“1997”>
</producer>
<producer> <actor> …. </actor>
<actor> …. </actor>
<sex>M</sex>
</movie>
<name>Warner Bros</name>
</producer>
<actor> …. </actor>
<actor> …. </actor>
</movie>
<movie still= “…”>
<title>The Devil's Advocate</title>
<still>http://www.movies.org/devilsAdvocate.jpg</still>
<producer sex =“M”>
<name>Warner Bros</name>
</producer>
<actor> …. </actor>
<actor> …. </actor>
</movie>
Oscar Díaz 121

54
Attribute vs. Element. Guidelines
Ø Guide: syntactic restrictions
Ø Guide: data vs. meta-data
Ø Guide: target audience

Oscar Díaz 122

Guide: syntactic restrictions


Ø  element
•  à has structure, can be repeated, it is not
“denormalized”
Ø  attribute
•  à is atomic, cannot be repeated, it is usually
“denormalized”
<movie title=“…” still= “…” year=“1997”>
<producer>
<sex>M</sex>
<name>Warner Bros</name>
</producer>
<actor> …. </actor>
<actor> …. </actor>
</movie>
Oscar Díaz 123

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>

Guide: target audience


Ø element
•  à if thought for person consumption
Ø attribute
•  à if thought for computer consumption (e.g.
URL, images, IDs, …)
<movie year= “1997” still=“…”>
<title>The Devil's Advocate</title>
<producer sex =“M”>
<name>Warner Bros</name>
</producer>
<actor> …. </actor>
<actor> …. </actor>
</movie>
Oscar Díaz 125

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

<xs:complexType name="CatalogItemT" >


<xs:all>
...
<xs:element name="supplier">
<xs:complexType> Guarantees the
<xs:sequence> cardinality of the
association
<xs:element ref="cml:Organization"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
Oscar Díaz 126
</xs:complexType>

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>

Oscar Díaz 127

57
HANDLING SCHEMA
COMPLEXITY

Oscar Díaz 128

Schema complexity
A schema can be very complex

•  Are there modularization mechanisms?


–  use of different schemas

•  Are there reuse mechanisms?

Oscar Díaz 129

58
Different schemas.
Perspective from the schema document

Ø  A schema can be created using other


schemas.

Ø  Two options:


•  <include>: one namespace spread among several
varios “.xsd”
•  <import>: one namespace that uses other
namespaces

Oscar Díaz 131

<include>

targetnameSpace: http://www.library.org

LibraryBook.xsd LibraryEmployee.xsd

All the schemas must have the same


targetnameSpace

library.xsd
<xsd:include schemaLocation="LibraryBook.xsd"/>
<xsd:include schemaLocation="LibraryEmployee.xsd"/>

Oscar Díaz 132

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 &amp; 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>

Nikon.xsd Olympus.xsd Pentax.xsd

Schemas can have different


targetnameSpaces
http://www.camera.org

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"/>

Oscar Díaz 135

<?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>

Oscar Díaz 137

<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

Redefine vs. include


Ø Like include:
•  The target namespace of redefine must be the
same as that of the redefining schema
Ø Unlike include:
•  the redefined type propagates to the
redefining schema
•  if the redefining schema contains subtypes,
these definitions might be affected by the
redefinition

Oscar Díaz 140

63
EXTENSIBILITY POINTS

Oscar Díaz 141

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.

Ø Role “schema author”


•  indicates where the schema can be extended.
Ø Role “document author”
•  can provide elements not declared in the
schema
Oscar Díaz 144

Role “schema author”


where do I allow extending the schema?
The author of schema
Book, allows adding
new elements
<xsd:complexType name="BookT"> >
<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 namespace = “##any” minOccurs="0"/>
</xsd:sequence>
<xsd:anyAttribute namespace = “##any” />
</xsd:complexType>
The author of schema
Book, allows adding
new attributes

Oscar Díaz 145

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

At the time the document is defined…


<?xml version="1.0"?>
<BookSeller xmlns="http://www.BookRetailers.org"
xmlns:own="http://www.myOwnSchema.org” /> Combine both vocabularies
<Book own:recommendable=”false”>
<Title>My Life and Times</Title>
<Author>Paul McCartney</Author>
<Date>1998</Date>
<ISBN>1-56592-235-2</ISBN>
<Publisher>McMillin Publishing</Publisher>
<Reviewer xmlns="http://www.myOwnSchema.org">
<name>
<First>Roger</First>
<Last>Costello</Last>
</name> creates the instance document
</Reviewer> using both vocabularies,
</Book> knowing that elements
<Book own:recommendable=”true">
from repository can be
<Title>Illusions: The Adventures of a Reluctant Messiah</Title>
<Author>Richard Bach</Author> used wherever Book allows
<Date>1977</Date> it
<ISBN>0-440-34319-4</ISBN>
Oscar Díaz
<Publisher>Dell Publishing Co.</Publisher> 147
</Book>
</BookSeller>

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

You might also like