Tecnologias de Sistemas de Informação baseados na WEB
4
XML Schema
TSIW 2006/2007
Warning
! Authors
" João Moura Pires (jmp@di.fct.unl.pt)
" With contributions of
! Carlos Damásio (cd@di.fct.unl.pt)
! Joaquim Aparício (jna@di.fct.unl.pt)
! Pedro Barahona (pb@di.fct.unl.pt)
! These slides can be freely used for personal or academic matters
without permission from the authors, as long as this author list is
included.
! The use of these slides for commercial matters is not allowed,
unless authorized from the authors.
4-2
4 - XML Schema
Summary
! Introduction and Motivation
! Examples: from a DTD to an XML Schema
! XML Schemas and namespaces
! XML Schema components
! Simple data types
! Deriving simple data types
! Content models
! Primary and foreign keys
! Schemas in several documents
4-3
4 - XML Schema
DTD weaknesses
! Non-XML syntax
! DTDs are quite limited regarding data types
! Limited content model
! Don’t support the definition of new data types
! DTD’s are like closed structures
" The rules of an XML vocabulary are all defined in a single DTD
" Impossible to reuse existing DTDs and extend them without name
conflicts (no namespace support)
4-4
Introduction and Motivation 4 - XML Schema
XML Schema goals
! The main XML Schema goals are to provide a solution for most
of the DTDs weaknesses, namely
" XML syntax (and not BNF)
" Better control of the document data types
" Better control of the document structure
" Compatibility with namespaces
! Several consortiums have been proposing schema languages,
but so far they lack the official support from W3C.
4-5
Introduction and Motivation 4 - XML Schema
What do Schemas provide to XML?
! Validation
" More common use
" Due to the possibility of using extensible complex data types the
validation of documents can be enhanced.
! Documentation
" They provide a formal specification of new vocabularies
" As schemas are XML documents they can be processed (ex: XSL) for
producing documentation of vocabularies in several ways.
! Querying support
" New Xpath, XSLT and XQuery specifications will use XML Schemas
for enhancing the performance of some functions (sort, equality
checks, etc.) by analysing the document data types. 4-6
Introduction and Motivation 4 - XML Schema
What do Schemas provide to XML?
! Data Binding
" Automatic mapping for internal data structures of programs which
process XML documents
! Object mapping
! Database mapping
! Automatic APIs creation
" Development of Data Binding tools which use declarative mappings
! http://www.rpbourret.com/xml/XMLDataBinding.htm
! Editors / GUIs
" W3C is creating a standard API aimed at editors
" Related with the XForms recommendation
4-7
Introduction and Motivation 4 - XML Schema
Competitors
! Other schema languages
" RDF (Resource Description Framework)
" XML – Data
" XML - DR (XML – Data Reduced)
" DCD (Document Content Description)
" SOX (Schema for Object-Oriented XML) Definition
" DDML (Document Definition Markup Language)
" BizTalk Schema
" Schematron
! XML Schema (or W3C Schema) is a W3C proposed
recommendation.
4-8
Introduction and Motivation 4 - XML Schema
W3C Recommendation
! In 2 May 2001 XML Schema became a W3C
Recommendation
! Reference Documents (http://www.w3.org/XML/Schema)
" XML Schema Part 0: Primer
! http://www.w3.org/TR/xmlschema-0/
" XML Schema Part 1: Structures
! Structure specification (elements and attributes)
! http://www.w3.org/TR/xmlschema-1/
" XML Schema Part 2: Datatypes
! Data types and restrictions specifications
! http://www.w3.org/TR/xmlschema-2/
4-9
Introduction and Motivation 4 - XML Schema
References: Web sites
! Zvon XML Schema reference:
" http://zvon.org/xxl/xmlSchema2001Reference/Output/Schema/
index.html
! XML Schemas - Needed Rules for XML Documents -
Simon St.Laurent
" http://www.simonstl.com/articles/schemas/index.htm
! Using W3C XML Schemas
" http://www.xml.com/pub/a/2000/11/29/schemas/part1.html
! Great XML Schema Tutorial by Roger L. Costello
" http://www.xfront.com/xml-schema.html
! XML Schemas: Best Practice de Roger L. Costello
" http://www.xfront.com/BestPracticesHomepage.html
4 - 10
Introduction and Motivation 4 - XML Schema
References: Books
! XML Schema
" The W3C’s Object Oriented Descriptions for XML
" Eric van der Vlist
" O’Rilley
" 2002
! Professional XML Schemas
" Wrox
" 2001
! Beginning XML 2nd Edition
" Wrox
" 2001
4 - 11
Introduction and Motivation 4 - XML Schema
XML Schema tools
! Command line tools
" XSV by Henry Thompson
! ftp://ftp.cogsci.ed.ac.uk/pub/XSV/XSV29.EXE
! Graphical User Interface tools
" Altova XML Spy
! http://www.altova.com/
" oXygen XML editor
! http://www.oxygenxml.com/
" Exchanger XML
! http://www.freexmleditor.com/
4 - 12
Introduction and Motivation 4 - XML Schema
XML Schema tools
! Tools that provide programming interfaces
" Apache Xerces (Xerces-J, Xerces-C, Xerces-P)
! http://xerces.apache.org/
" IBM Schema Quality Checker
! http://www.alphaworks.ibm.com/tech/xmlsqc
! This tool is only used to check XML Schemas by themselves. It can’t
be used to validate an XML document according to a schema.
" XML Parser for Java v2
! http://www.oracle.com/technology/tech/xml/index.html
Integrated with Oracle 8i DBMS
" MSXML
! http://www.microsoft.com
4 - 13
Introduction and Motivation 4 - XML Schema
References: Web sites
! http://www.w3c.org/XML/Schema
! http://zvon.org/xxl/xmlSchema2001Reference/Output/Schema/ind
ex.html
! http://search.msdn.microsoft.com/search/results.aspx?qu=xml+s
chema&View=msdn&st=b&c=4&s=1&swc=4
! http://www.xml.com/pub/a/2000/11/29/schemas/structuresref.html
! http://www.w3schools.com/schema/
4 - 14
Introduction and Motivation 4 - XML Schema
XML Schema
Examples: from a DTD to an XML Schema
TSIW 2006/2007
An example from a DTD
! A bookstore catalogue is composed by a set of books (one or
more)
! Each book is described as:
" Title
" Author
" Date
" ISBN
" Publisher
! These elements shall be represented by the most appropriate
data types
4 - 16
From a DTD to an XML Schema 4 - XML Schema
XML Document example
<?xml version="1.0" ?>
<BookStore>
<Book>
<Title>The Adventures of a Reluctant Messiah</Title>
<Author>Richard Bach</Author>
<Date>1977</Date>
<ISBN>0-440-34319-4</ISBN>
<Publisher>Dell Publishing Co.</Publisher>
</Book>
<Book>
<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>
….
</BookStore> 4 - 17
From a DTD to an XML Schema 4 - XML Schema
Starting with the DTD
<!ELEMENT BookStore (Book)+>
<!ELEMENT Book (Title, Author, Date, ISBN, Publisher)>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Author (#PCDATA)>
<!ELEMENT Date (#PCDATA)>
<!ELEMENT ISBN (#PCDATA)>
<!ELEMENT Publisher (#PCDATA)>
4 - 18
From a DTD to an XML Schema 4 - XML Schema
Direct conversion of DTD to XML Schema (1)
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
….
<xs:element name="Title" type="xs:string"/> <!ELEMENT Title (#PCDATA)>
<xs:element name="Author" type="xs:string"/> <!ELEMENT Author (#PCDATA)>
<xs:element name="Date" type="xs:string"/>
<xs:element name="ISBN" type="xs:string"/>
<!ELEMENT Date (#PCDATA)>
<xs:element name="Publisher" type="xs:string"/> <!ELEMENT ISBN (#PCDATA)>
…
<!ELEMENT Publisher (#PCDATA)>
</xs:schema>
4 - 19
From a DTD to an XML Schema 4 - XML Schema
Direct conversion of DTD to XML Schema (2)
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
...
<xs:element name="Book">
<xs:complexType>
<xs:sequence>
<xs:element ref="Title" minOccurs="1" maxOccurs="1"/>
<xs:element ref="Author" minOccurs="1" maxOccurs="1"/>
<xs:element ref="Date" minOccurs="1" maxOccurs="1"/>
<xs:element ref="ISBN" minOccurs="1" maxOccurs="1"/>
<xs:element ref="Publisher" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
...
<xs:schema> <!ELEMENT Book (Title, Author, Date, ISBN, Publisher)>
4 - 20
From a DTD to an XML Schema 4 - XML Schema
Direct conversion of DTD to XML Schema (3)
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
...
<xs:element name="BookStore">
<xs:complexType>
<xs:sequence>
<xs:element ref="Book" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Book"> ... </xs:element>
...
</xs:schema>
<!ELEMENT BookStore (Book+)>
4 - 21
From a DTD to an XML Schema 4 - XML Schema
XMLSpy automatic conversion
<?xml version="1.0" encoding="UTF-8"?>
<!--W3C Schema generated by XMLSPY v5 rel. 2 U (http://www.xmlspy.com)-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="BookCatalogue">
<xs:complexType>
<xs:sequence>
<xs:element name="Book" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Author" type="xs:string"/>
<xs:element name="Date" type="xs:string"/>
<xs:element name="ISBN" type="xs:string"/>
<xs:element name="Publisher" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
4 - 22
From a DTD to an XML Schema 4 - XML Schema
Corresponding XML Schema diagram
4 - 23
From a DTD to an XML Schema 4 - XML Schema
Other example: Purchase Order
! A purchase order is described as:
" A name and address where to ship the product
" A name and address of who pays the product
" A comment
" List of items of the purchase order. Each item is composed
by:
! Product name, quantity, price and an optional comment
! Attributes:
" Date attribute for the purchase order.
" Part-number attribute of each item.
4 - 24
From a DTD to an XML Schema 4 - XML Schema
Purchase order XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<purchaseOrder orderDate="1999-10-20">
<shipTo country="US">
<name>Alice Smith</name>
<street>123 Maple Street</street>
<city>Mill Valley</city>
<state>CA</state>
<zip>90952</zip>
</shipTo>
<billTo country="US">
<name>Robert Smith</name>
<street>8 Oak Avenue</street>
<city>Old Town</city>
<state>PA</state>
<zip>95819</zip>
</billTo>
<comment>Hurry, my lawn is going wild!</comment>
<items>
<item partNum="872-AA">
<productName>Lawnmower</productName>
<quantity>1</quantity>
<USPrice>148.95</USPrice>
<comment>Confirm this is electric</comment>
</item>
<item partNum="926-AA">
<productName>Baby Monitor</productName>
<quantity>1</quantity>
<USPrice>39.98</USPrice>
<shipDate>1999-05-21</shipDate>
</item>
</items>
</purchaseOrder>
4 - 25
From a DTD to an XML Schema 4 - XML Schema
Purchase order XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<purchaseOrder orderDate="1999-10-20">
<shipTo country="US">
<name>Alice Smith</name>
<street>123 Maple Street</street>
<city>Mill Valley</city>
<state>CA</state>
<zip>90952</zip>
</shipTo>
<billTo country="US">
….
</billTo>
<comment>Hurry, my lawn is going wild!</comment>
<items>
<item partNum="872-AA">
<productName>Lawnmower</productName>
<quantity>1</quantity>
<USPrice>148.95</USPrice>
<comment>Confirm this is electric</comment>
</item>
<item partNum="926-AA">
….
</item>
</items>
</purchaseOrder>
4 - 26
From a DTD to an XML Schema 4 - XML Schema
Conversion to XML Schema – Diagram (1)
4 - 27
From a DTD to an XML Schema 4 - XML Schema
“Simple” DTD for the purchase order
<?xml version="1.0" encoding="UTF-8"?>
<! Basic version-->
<!ELEMENT purchaseOrder (shipTo, billTo, comment, items)>
<!ELEMENT shipTo (name, street, city, state, zip)>
<!ELEMENT billTo (name, street, city, state, zip)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT zip (#PCDATA)>
<!ELEMENT comment (#PCDATA)>
<!ELEMENT items (item+)>
4 - 28
From a DTD to an XML Schema 4 - XML Schema
“Simple” DTD for the purchase order
<?xml version="1.0" encoding="UTF-8"?>
<! Basic version-->
<!ELEMENT purchaseOrder (shipTo, billTo, comment, items)>
<!ATTLIST purchaseOrder
orderDate CDATA #REQUIRED>
<!ELEMENT shipTo (name, street, city, state, zip)>
<!ATTLIST shipTo
country CDATA #REQUIRED>
<!ELEMENT billTo (name, street, city, state, zip)>
<!ATTLIST billTo
country CDATA #REQUIRED>
<!ELEMENT name (#PCDATA)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT zip (#PCDATA)>
<!ELEMENT comment (#PCDATA)>
<!ELEMENT items (item+)>
<!ELEMENT item (productName, quantity, USPrice, comment?, shipDate?)>
<!ATTLIST item
partNum ID #REQUIRED>
<!ELEMENT productName (#PCDATA)>
<!ELEMENT quantity (#PCDATA)>
<!ELEMENT USPrice (#PCDATA)>
<!ELEMENT shipDate (#PCDATA)>
4 - 29
From a DTD to an XML Schema 4 - XML Schema
Purchase order XML Schema (1)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
….
<xs:element name="comment" type="xs:string"/>
<xs:element name="purchaseOrder">
<xs:complexType>
<xs:sequence>
<xs:element name="shipTo"> …. </xs:element>
<xs:element name="billTo“> … </xs:element>
<xs:element ref="comment"/>
<xs:element name="items">
<xs:complexType>
<xs:sequence>
<xs:element name="item" maxOccurs="unbounded">
…
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderDate" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
4 - 30
From a DTD to an XML Schema 4 - XML Schema
Purchase order XML Schema (2)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="comment" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="street" type="xs:string"/>
<xs:element name="zip" type="xs:string"/>
<xs:element name="purchaseOrder">
<xs:complexType>
<xs:sequence>
<xs:element name="shipTo"> …. </xs:element>
<xs:element name="billTo“> … </xs:element>
<xs:element ref="comment"/>
<xs:element name="items"> … </xs:element>
</xs:sequence>
<xs:attribute name="orderDate" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
4 - 31
From a DTD to an XML Schema 4 - XML Schema
Purchase order XML Schema (3)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="comment" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="street" type="xs:string"/>
<xs:element name="zip" type="xs:string"/>
<xs:element name="purchaseOrder">
<xs:complexType>
<xs:sequence>
<xs:element name="shipTo">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="street"/>
<xs:element ref="city"/>
<xs:element ref="state"/>
<xs:element ref="zip"/>
</xs:sequence>
<xs:attribute name="country" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
….
</xs:sequence>
<xs:attribute name="orderDate" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema> 4 - 32
From a DTD to an XML Schema 4 - XML Schema
Conversion to XML Schema – Diagram (2)
4 - 33
From a DTD to an XML Schema 4 - XML Schema
Conversion to XML Schema – Diagram (3)
4 - 34
From a DTD to an XML Schema 4 - XML Schema
Conversion to XML Schema – Diagram (4)
4 - 35
From a DTD to an XML Schema 4 - XML Schema
Purchase order XML Schema (4)
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="addressType">
…
</xs:complexType>
<xs:complexType name="itemType">
…
</xs:complexType>
<xs:element name="comment" type="xs:string"/>
<xs:element name="purchaseOrder">
<xs:complexType>
<xs:sequence>
<xs:element name="shipTo" type="addressType"/>
<xs:element name="billTo" type="addressType"/>
<xs:element ref="comment"/>
<xs:element name="items">
<xs:complexType>
<xs:sequence>
<xs:element name="item" type="itemType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderDate" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
4 - 36
From a DTD to an XML Schema 4 - XML Schema
Important Notes
! There are several different ways of building an XML Schema for
an XML vocabulary.
! DATA TYPE definitions
" Anonymous or named data types
" Global or local data types
" Simple or complex data types
! ELEMENT declarations
" Local or global element declarations
" Reuse of global elements by using references
4 - 37
From a DTD to an XML Schema 4 - XML Schema
XML Schemas and namespaces
! Extend namespaces
! Declare namespaces in an XML Schema
! How to assign an instance document to an XML
Schema
4 - 38
XML Schemas and namespaces 4 - XML Schema
Namespaces
! In general, different vocabularies can use markups with the same
name. However the use of several vocabularies in the same
document can raise ambiguity in the semantic of the markups.
! Namespaces where defined by the W3C to provide a mechanism
for uniquely identify the markups (elements and attributes) of a
vocabulary.
! W3C Recommendation (14th, January 1999)
" “Namespaces in XML”
" http://www.w3.org/TR/REC-xml-names/
" “We envision applications of Extensible Markup Language (XML) where a single XML
document may contain elements and attributes (here referred to as a "markup
vocabulary") that are defined for and used by multiple software modules. One motivation
for this is modularity; if such a markup vocabulary exists which is well-understood and for which
there is useful software available, it is better to re-use this markup rather than re-invent it.”.
4 - 39
XML Schemas and namespaces 4 - XML Schema
Namespaces
! W3C Recommendation (14th, January 1999)
" “Namespaces in XML”
" http://www.w3.org/TR/REC-xml-names/
! Proposed recommendation (4th, February 2004)
" “Namespaces in XML 1.1”
" http://www.w3.org/TR/xml-names11/
! Proposed recommendation (5th, November 2003)
" “Extensible Markup Language (XML) 1.1”
" http://www.w3.org/TR/xml11/
4 - 40
XML Schemas and namespaces 4 - XML Schema
What is a namespace?
! “An XML namespace is a collection of names, identified by a URI
reference, which are used in XML documents as element types and
attribute names.” (W3C)
! When using namespaces there are qualified names for the markups:
" Namespace Prefix : local name
" As URIs are unique and each prefix is associated with an URI, a qualified
name is unique, avoiding naming conflicts.
xmlns:xs = ”http://www.w3.org/2001/XMLSchema”
prefix mapping URI
4 - 41
XML Schemas and namespaces 4 - XML Schema
How to use namespaces?
! In any of the XML document elements it is possible to declare the
use of a namespace using the xmlns or xmlns:prefix attributes.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element …>
….
</xs:element>
</xs:schema>
Inside this element names of the namespace identified with the
URI http://www.w3.org/2001/XMLSchema will be used. These names
will be used with the xs prefix.
4 - 42
XML Schemas and namespaces 4 - XML Schema
How to use namespaces?
<?xml version="1.0"?>
<BookCatalogue xmlns="http://bookcatalog.org">
<Book>
<Title>Adventures of a Reluctant Messiah</Title>
<Author>Richard Bach</Author>
<Date>1977</Date>
<ISBN>0-440-34319-4</ISBN>
<Publisher>Dell Publishing Co.</Publisher>
</Book>
<Book>
<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>
</BookCatalogue>
Default namespace
4 - 43
XML Schemas and namespaces 4 - XML Schema
How to use namespaces?
<?xml version="1.0"?>
<bk:BookCatalogue xmlns:bk="http://bookcatalog.org">
<bk:Book>
<bk:Title>Adventures of a Reluctant Messiah</bk:Title>
<bk:Author>Richard Bach</bk:Author>
<bk:Date>1977</bk:Date>
<bk:ISBN>0-440-34319-4</bk:ISBN>
<bk:Publisher>Dell Publishing Co.</bk:Publisher>
</bk:Book>
<bk:Book>
<bk:Title>The First and Last Freedom</bk:Title>
<bk:Author>J. Krishnamurti</bk:Author>
<bk:Date>1954</bk:Date>
<bk:ISBN>0-06-064831-7</bk:ISBN>
<bk:Publisher>Harper & Row</bk:Publisher>
</bk:Book>
</bk:BookCatalogue>
4 - 44
XML Schemas and namespaces 4 - XML Schema
Scope of the namespace declaration
URI of this namespace URI of the other namespace
root element2 element1
element1 att1
extra
<?xml version="1.0" encoding="UTF-8"?>
<pr1:root xmlns:pr1="URI of this namespace">
<pr1:element1 pr1:att1="value">
<pr1:element2>
example
</pr1:element2>
<pr2:extra xmlns:pr2="URI of the other namespace">
<pr2:element1>
This element is defined in the second namespace
</pr2:element1>
</pr2:extra>
</pr1:element1>
</pr1:root>
4 - 45
XML Schemas and namespaces 4 - XML Schema
Use of various namespaces
! Various namespaces can be declared in the same element by
using multiple attributes.
<?xml version="1.0"?>
<!-- both namespace prefixes are available throughout -->
<bk:book xmlns:bk='urn:loc.gov:books'
xmlns:isbn='urn:ISBN:0-395-36341-6'>
<bk:title>Cheaper by the Dozen</bk:title>
<isbn:number>1568491379</isbn:number>
</bk:book>
4 - 46
XML Schemas and namespaces 4 - XML Schema
Default namespace
! It is possible to change the default namespace inside an XML
document.
<?xml version="1.0"?>
<!-- initially, the default namespace is "books" -->
<book xmlns='urn:loc.gov:books'
xmlns:isbn='urn:ISBN:0-395-36341-6'>
<title>Cheaper by the Dozen</title>
<isbn:number>1568491379</isbn:number>
<notes>
<!-- make HTML the default namespace for some commentary -->
<p xmlns='urn:w3-org-ns:HTML'>
This is a <i>funny</i> book!
</p>
</notes>
</book>
4 - 47
XML Schemas and namespaces 4 - XML Schema
Elements that don’t belong to a namespace
<?xml version='1.0'?>
<Beers>
<!-- the default namespace is now that of HTML -->
<table xmlns='http://www.w3.org/TR/REC-html40'>
<th><td>Name</td><td>Origin</td><td>Description</td></th>
<tr>
<!-- no default namespace inside table cells -->
<td><brandName xmlns="">Huntsman</brandName></td>
<td><origin xmlns="">Bath, UK</origin></td>
<td>
<details xmlns=""><class>Bitter</class><hop>Fuggles</hop>
<pro>Wonderful hop, light alcohol, good summer beer</pro>
<con>Fragile; excessive variance pub to pub</con>
</details>
</td>
</tr>
</table>
</Beers>
4 - 48
XML Schemas and namespaces 4 - XML Schema
Two attributes can’t have the same name
! The following document is not well-formed, according to the XML
1.0 syntax rules:
<dimensions>
<vertical height="24 inches" height="60 cms"/>
</dimensions>
! By using namespaces this situation is solved:
<dimensions xmlns="http://www.example.org/measurements“
xmlns:inch="http://www.example.org/inches"
xmlns:cm="http://www.example.org/cms">
<vertical inch:height="24" cm:height="60"/>
</dimensions>
4 - 49
XML Schemas and namespaces 4 - XML Schema
Two attributes can’t have the same name
! Two distinct prefixes may point to the same namespace, what
might lead to errors.
<dimensions xmlns:inch="http://www.example.org/measurements"
xmlns:cm="http://www.example.org/measurements">
<vertical inch:height="24" cm:height="60"/>
</dimensions>
! However the default namespace isn’t applied to attributes.
Unless specified, the attributes belong to the same namespace of
their element.
<dimensions xmlns="http://www.example.org/measurements"
xmlns:cm="http://www.example.org/measurements">
<vertical height="24" cm:height="60"/>
</dimensions>
4 - 50
XML Schemas and namespaces 4 - XML Schema
Namespaces of the XML Schema vocabulary
! XML Schema namespace
" http://www.w3c.org/2001/XMLSchema
" Declaration of elements like: schema, element, attribute,
complexType, group, sequence, etc...
" Declaration of attributes like: name, ref, type, minOccurs, etc..
" Includes the predefined data types names.
! XML Schema data types namespace
" http://www.w3c.org/2001/XMLSchema-datatypes
" Declaration of data types: string, integer, etc...
" This namespace can be used separately.
4 - 51
XML Schemas and namespaces 4 - XML Schema
Namespaces of the XML Schema vocabulary
! XML Schema instance namespace
" http://www.w3c.org/2001/XMLSchema-instance
" Declaration of four commonly used attributes
! xsi:schemaLocation Informs the parser about the location of the schema
when it defines a namespace
! xsi:noNamespaceSchemaLocation Informs the parser about
the location of the schema when
it defines no namespace
! xsi:type
! xsi:nil
4 - 52
XML Schemas and namespaces 4 - XML Schema
Create vocabularies without namespaces
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Customer"> Schema
<xs:complexType>
<xs:sequence> Customer.xsd
<xs:element name="FirstName" type="xs:string"/>
<xs:element name="MiddleInitial" type="xs:string"/>
<xs:element ref="LastName"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="LastName" type="xs:string"/>
</xs:schema>
<?xml version="1.0"?>
<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Customer.xsd">
Instance
<FirstName>Ray</FirstName>
<MiddleInitial>G</MiddleInitial>
<LastName>Bayliss</LastName>
</Customer>
4 - 53
XML Schemas and namespaces 4 - XML Schema
Create vocabularies with namespaces
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns="http://www.books.org">
<xs:element name="BookStore">
<xs:complexType name="BookType">
<xs:sequence> ... </xs:sequence>
Namespace of the XML
</xs:complexType> Schema language:
</xs:element> element, complexType, etc
<xs:element name="Book"> … </xs:element>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Author" type="xs:string"/> XML Schema elements are
<xs:element name="Date" type="xs:string"/> refered with the xs prefix
<xs:element name="ISBN" type="xs:string"/>
<xs:element name="Publisher" type="xs:string"/>
</xs:schema>
4 - 54
XML Schemas and namespaces 4 - XML Schema
Creating a target namespace
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns="http://www.books.org">
<xsd:element name="BookStore"> … </xsd:element>
<xsd:element name="Book"> … </xsd:element>
<xsd:element name="Title" type="xsd:string"/> Namespace of the vocabulary
<xsd:element name="Author" type="xsd:string"/> defined in this schema.
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/> The defined elements are:
<xsd:element name="Publisher" type="xsd:string"/>
BookStore, Book, Title, Author,
<xsd:schema>
Date, ISBN, Publisher
These are part of the namespace
4 - 55
XML Schemas and namespaces 4 - XML Schema
Setting the default namespace
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd ="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns="http://www.books.org">
<xsd:element name="BookStore"> … </xsd:element>
<xsd:element name="Book"> … </xsd:element>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/> In this case the default namespace is
<xsd:element name="ISBN" type="xsd:string"/> the same as the target namespace. So
<xsd:element name="Publisher" type="xsd:string"/> there is no need to include any prefix
<xsd:schema>
to refer the elements defined in this
schema:
- BookStore, Book, Title, Author,
Date, ISBN, Publisher
4 - 56
XML Schemas and namespaces 4 - XML Schema
Setting a different default namespace
In this case, the default namespace
<?xml version="1.0" encoding="UTF-8" ?> is the XML Schema namespace. So
<schema xmlns="http://www.w3.org/2001/XMLSchema” no need to use any prefix to use
targetNamespace="http://www.books.org"
XML Schema elements:
xmlns:bk="http://www.books.org">
–element, complexType, etc...
<element name="BookStore"> ... </element> The refereces to elements with
<element name="Book"> names defined in www.books.org,
<complexType>
must use the specified bk prefix.
<sequence>
<element ref=”bk:Title" minOccurs="1" maxOccurs="1"/>
<element ref=”bk:Author" minOccurs="1" maxOccurs="1"/>
<element ref=”bk:Date" minOccurs="1" maxOccurs="1"/>
<element ref=”bk:ISBN" minOccurs="1" maxOccurs="1"/>
<element ref=”bk:Publisher" minOccurs="1" maxOccurs="1"/>
</sequence>
</complexType>
</element>
…..
4 - 57
XML Schemas and namespaces 4 - XML Schema
Specify an XML Schema in an XML instance doc.
<?xml version="1.0"?>
<BookStore xmlns ="http://www.books.org" 1
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3
xsi:schemaLocation="http://www.books.org BookStore.xsd">
2
<Book>
<Title>My Life and Times</Title>
<Author>Paul McCartney</Author>
<Date>July, 1998</Date>
<ISBN>94303-12021-43892</ISBN>
<Publisher>McMillin Publishing</Publisher>
</Book>
...
</BookStore>
1. Declaration of the default namespace means that all the tags are defined
in http://www.books.org
2. Declaration that the namespace http://www.books.org is defined in
Bookstore.xsd. This information will be used by validating parsers.
3. Required declaration of the XMLSchema-instance namespace for the
use of the xsi:schemaLocation attribute in point 2.
4 - 58
XML Schemas and namespaces 4 - XML Schema
Specify an XML Schema in an XML instance doc.
schemaLocation="http://www.books.org targetNamespace="http://www.books.org"
BookStore.xsd"
BookStore.xml BookStore.xsd
- uses elements of the namespace - defines elements in the namespace
http://www.books.org http://www.books.org
A schema defines a new vocabulary.
Instance documents use that new vocabulary.
4 - 59
XML Schemas and namespaces 4 - XML Schema
XML Schema components
! Primary components ! Complementar components
" Simple Type definitions " Comments
" Complex Type definitions " Models
" Attribute declarations " Particles
" Element declarations " Wildcards
" Attribute usage
! Secundary components
" Attribute group definition
" Restriction definitions
" Model definitions
" Annotation declarations
4 - 60
XML Schema components 4 - XML Schema
Basics: Complex vs. Simple Types
! Complex types
" Can include other elements.
" Can include attributes.
! Simple types
" Can’t include other elements, just data!
" Can’t include attributes.
Simple Types
Model Data types
Complex Types
Attributes
4 - 61
XML Schema components 4 - XML Schema
Basics: Complex vs. Simple Types
<complexType>
Type shall contain
Contains elements? Yes
elements or attributes ?
Yes No
Derived by: Derived by: No
<restriction> <restriction>
contentModel ? <simpleType> ? <simpleType>
attribute or attributeGroup? facets?
anyAtttibute? atttibute or attributeGroup?
<restriction> anyAttribute?
Derived by:
or <restriction>
<restriction>
<extension> ou
contentModel ? <extension> or
attribute or attributeGroup? atttibute or attributeGroup? <list>
anyAtttibute? anyAtttibute? or
<extension> <extension> <union>
4 - 62
XML Schema components 4 - XML Schema
Basics: Named and anonymous types
! Named types
<xs:simpleType name=“NameforType">
....
</xs:simpleType>
<xs:complexType name=“NameforType">
....
</xs:complexType>
! Anonymous types
<xs:simpleType>
....
</xs:simpleType>
<xs:complexType>
....
</xs:simpleType>
4 - 63
XML Schema components 4 - XML Schema
Basics: Declarations vs. definitions
! Element and attribute declarations
<xs:element name=“elementName">
…
</xs:element> Can be part of the
vocabulary
<xs:attribute name=“attributeName">
…
</xs:attribute>
! Type definitions
For defining the data
types of attributes and
" Can be named or anonymous
leaf complex elements.
4 - 64
XML Schema components 4 - XML Schema
Basics: Global vs. local elements
! Global elements and attributes
" Declared under <schema>
" Once declared globally can be refered by the use of ref (thus being
reused in other contexts)
" A global element can be root element of an instance document.
" Notes:
! Global declarations can’t use ref. Simple or complex types should be
identified directly by type or should be created in-line.
! Cardinality restrictions (minOccurs, maxOccurs, use) can’t be placed
in global declarations (although these can be placed in local
declarations which refer the global declaration by using ref).
4 - 65
XML Schema components 4 - XML Schema
Basics: Global vs. local elements
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="addressType">
… Global declaration
</xs:complexType>
<xs:complexType name="itemType">
… Refered in a
</xs:complexType>
<xs:element name="comment" type="xs:string"/>
local declaration
<xs:element name="purchaseOrder">
<xs:complexType>
<xs:sequence>
<xs:element name="shipTo" type="addressType"/>
<xs:element name="billTo" type="addressType"/>
<xs:element ref="comment“ minOccurs=“0“ maxOccurs=“1“/>
<xs:element name="items">
<xs:complexType>
<xs:sequence>
<xs:element name="item" type="itemType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderDate" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
4 - 66
XML Schema components 4 - XML Schema
Schema design styles
! Russian Doll Design
" Schema follows the structure of the instance documents
" Just one global element (root) where all the others are declared
locally.
" No reuse of elements / types.
! Salami Slice Design
" The opposite of Russion Doll Design
" All elements are declared globally
" Elements are reused
! Venetian Blind Model
" Definition complex types that will be reused
" Only the root element is declared globally
4 - 67
XML Schema components 4 - XML Schema
Annotating an XML Schema
! The <annotation> element is use to document the Schema, for
human or program use.
" Use <documentation> child node for providing comments for
humans.
" Use <appinfo> child node for providing additional structured
information for programs
! Its content is any well-formed XML
! The content of the <annotation> element doesn’t affect the
Schema nor the defined vocabulary.
4 - 68
XML Schema components 4 - XML Schema
The <annotation> element
<xs:annotation>
<xs:documentation>
The following constraint is not expressible with XML Schema: The value of element A should be greater
than the value of element B. So, we need to use a separate tool (e.g., Schematron) to check this constraint.
We will express this constraint in the appinfo section (below).
</xs:documentation>
<xs:appinfo>
<assert test="A > B">A should be greater than B</assert>
</xs:appinfo>
<xs:/annotation>
4 - 69
XML Schema components 4 - XML Schema
Where to use the <annotation> element
! It is not possible to place the <annotation> element in any part of
the schema
! Rule
" <annotation> can be before or after any global component.
" <annotation> can only be in the beginning of a local element.
4 - 70
XML Schema components 4 - XML Schema
Where to use the <annotation> element
<?xml version="1.0"?>
<xs:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns="http://www.books.org"
elementFormDefault="qualified">
<xs:element name="BookStore"> Points where
<xs:complexType>
<xs:sequence>
is possible
<xs:element name="Book" maxOccurs="unbounded"> to add
<xs:complexType> annotations.
<xs:sequence>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Author" type="xs:string"/>
<xs:element name="Date" type="xs:string"/>
<xs:element name="ISBN" type="xs:string"/>
<xs:element name="Publisher" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
4 - 71
XML Schema components 4 - XML Schema
Where to use the <annotation> element
<?xml version="1.0"?>
<xs:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"…>
<xs:element name="BookStore">
<xs:complexType>
<xs:sequence>
<xs:element name="Book" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Author" type="xs:string"/>
<xs:element name="Date" type="xs:string">
<xs:annotation>
<xs:documentation>This is how to annotate the Date element!</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="ISBN" type="xs:string"/>
<xs:element name="Publisher" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
4 - 72
XML Schema components 4 - XML Schema
<documentation> and <appinfo> attributes
! <xs:documentation>
" source: URL to an additional information document
" xml:lang: language in with the documentation is written
<xsd:documentation source="http://www.xfront.com/BookReview.txt" xml:lang="FR"/>
! <xsd:appinfo>
" source: URL to an additional information document
<xsd:appinfo source="http://www.xfront.com/Assertions.xml"/>
4 - 73
XML Schema components 4 - XML Schema
Simple and complex types
! Simple types
" A simple character sequence
" Restricted character sequences that restrict numeric values or dates,
for instance
" No sub elements
! Complex types
" Contain sub elements or
" Attributes
4 - 74
Simple data types 4 - XML Schema
What is an XML data type?
! A simple data type is defined by:
" Value space
! The set of distinct values for a data type. Each value corresponds to
one or more textual representations.
! E.g.: the “31” value
" Lexical space
! The set of textual representations (lexical) that can be used to
represent the values in the value space.
! E.g.: any of the textual representations “31”, “0.31E2”, “thirty one”,
“0.31 102” can be used to represent the 31 value.
" Facets
! Properties of the value space, or each one of its values or lexical
representations.
! E.g.: numeric or not numeric.
! E.g.: the maximum size of the textual representation.
4 - 75
Simple data types 4 - XML Schema
Value space
! Each data type has a set of valid distinct values.
! These sets of values are defined implicitly. For instance integers,
floats, dates, strings, ...
! Example
" European Union country codes
! {PT, …., UK}
4 - 76
Simple data types 4 - XML Schema
Lexical space
! The set of textual representations (lexical) which can be used to
represent the values of the value space.
! Sets of character sequences, composed by a subset of the XML
legal UNICODE characters.
! Each value of the string type can only have one lexical
representation, but in other datatypes each value can have more
tha one representation.
! Each value has a lexical canonical representation according to
the XML recommendation
" http://www.w3.org/TR/2002/REC-xml-exc-c14n-20020718/
4 - 77
Simple data types 4 - XML Schema
Facets
! Properties of the value space, or each one of its values or lexical
representations.
! Basic
" equality, order, bounds, cardinality, numeric/non-numeric
! Restriction
" length, minLength, maxLength
" whiteSpace
Used to build new
" pattern derived data types
" enumeration
" minExclusive, maxExclusive
minInclusive, maxInclusive
" totalDigits, fractionDigits
4 - 78
Simple data types 4 - XML Schema
XML Schema data types
! Built-in
" Basic – the base of all other data types
" Derived – obtained from the firsts by domain restrictions
! User defined
" Simple
! With addditional user-defined restrictions
" Complex
! Allow the definition of structures from simple types
4 - 79
Simple data types 4 - XML Schema
Deriving new data types
! A derived data type is defined in function of another data type
(refered as the base type)
" The base type can be basic or derived
" Derived types inherit the value space of the base type
" Derived types can constrain / restrict their set of valid values
<xs:simpleType Name=”dayOfMonth” >
<xs:restriction base=”xs:integer”>
<xs:minInclusive value=”1”/>
<xs:maxInclusive value=”31”/>
</xs:restriction>
</xs:simpleType>
4 - 80
Simple data types 4 - XML Schema
Deriving new data types
! Deriving of new data types by:
" Restriction
! By restricting the subset of valid values for the base type
" List
! The new derived type is a list of values seperated by spaces
! E.g.: NMTOKENS derived from NMTOKEN
<simpleType name='sizes'>
<list itemType='decimal'/>
</simpleType>
<cerealSizes xsi:type='sizes'> 8 10.5 12 </cerealSizes>
" Union
! By union of two or more base types
4 - 81
Simple data types 4 - XML Schema
Types of simple data types
! Atomic
! List
! Union
4 - 82
Simple data types 4 - XML Schema
List
! A simple data type is a list if composed by a sequence of atomic
types seperated by spaces
" Example 1: NMTOKENS “xpto ypto zpto”
" Example 2: IDREFS “a01 p01 p02”
! Lists can be defined as simple types
<xs:simpleType name=“digit">
<xs:restriction base="xs:int">
<xs:simpleType name=“digitList">
<xs:maxExclusive=“10"/>
<xs:list itemType = “digito”>
<xs:minInclusive=“0"/>
</xs:simpleType>
</xs:restriction>
</xs:simpleType>
4 - 83
Simple data types 4 - XML Schema
Union
<xsd:simpleType name=“digitH">
<xsd:restriction base="xsd:string">
<xsd:enumeration value=“A"/>
.....
<xsd:enumeration value=“F"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name=“hexDigit">
<xsd:union memberTypes = “digit digitH”>
</xsd:simpleType>
4 - 84
Simple data types 4 - XML Schema
Basic data types
4 - 85
Simple data types 4 - XML Schema
String based data types
Whitespace replacement
Whitespace collapse
E.g.: en-US, fr, pt No whitespaces
Starts with letter,
: or -. No whitespaces
4 - 86
Simple data types 4 - XML Schema
Simple data types examples
" string - "Hello World
" boolean- {true, false}
" number - 123456789012345678 (18 digits)
" decimal - 7.08
" float - 12.56E3, 12, 12560, 0, -0, INF, -INF,
" double - 12.56E3, 12, 12560, 0, -0, INF, -INF,
" duration- P1Y2M3DT10H30M12.3S
" dateTime- CCYY-MM-DDThh-mm-ss
" time - hh:mm:ss.sss
" date - CCYY-MM-DD
4 - 87
Simple data types 4 - XML Schema
Simple data types examples
" gYearMonth - CCYY-MM
" gYear - CCYY
" gMonthDay - --MM-DD (with minus)
" gDay - ---DD
" gMonth - --MM--
" hexBinary - a hex string
" base64Binary - a base64 string
" anyURI - http://www.xfront.com
" Qname - a namespace qualified name
" NOTATION - a NOTATION from the XML spec
4 - 88
Simple data types 4 - XML Schema
Simple data types examples
" negativeInteger negative infinity to -1
" long -9223372036854775808 to
9223372036854775808
" int -2147483648 to 2147483647
" short -32768 to 32767
" byte -127 to 128
" nonNegativeInteger 0 to infinity
" positiveInteger 1 to infinity
" unsignedLong 0 to 18446744073709551615
" unsignedInt 0 to 4294967295
" unsignedShort 0 to 65535
" unsignedByte 0 to 255
4 - 89
Simple data types 4 - XML Schema
Deriving diagram for data types
<complexType>
Type shall contain
Contains elements? Yes
elements or attributes ?
Yes No
Derived by: Derived by: No
<restriction> <restriction>
contentModel ? <simpleType> ? <simpleType>
attribute or attributeGroup? facets?
anyAtttibute? atttibute or attributeGroup?
<restriction> anyAttribute?
Derived by:
or <restriction>
<restriction>
<extension> ou
contentModel ? <extension> or
attribute or attributeGroup? atttibute or attributeGroup? <list>
anyAtttibute? anyAtttibute? or
<extension> <extension> <union>
4 - 90
Deriving simple data types 4 - XML Schema
First example
<xsd:simpleType name=“PortugueseTelephone"> 1
<xsd:restriction base="string"> 2
<xsd:length value=“15"/> 3
<xsd:pattern value=“+351\d{9}"/>4
</xsd:restriction>
</xsd:simpleType>
1. Definition of new simple type “PortugueseTelephone”.
2. The elements of this data data are based on the string type
3. The length of the string shall be restricted to 15 characters and…
4. it shall have the +351ddddddddd pattern, where ‘d’ represents a single
digit. (In this case the regular expression facet makes the length facet
redundant – as the regular expression is also limiting the length)
4 - 91
Deriving simple data types 4 - XML Schema
Facets for the string type
! String
" length
" minLength
" maxLength
" pattern (using regular expressions)
" enumeration (list of possible values)
" whiteSpace
Preserve
No normalization is done, the value is not changed
replace
All occurrences of #x9 (tab), #xA (line feed) and #xD (carriage return) are replaced with #x20 (space)
collapse
After the processing implied by replace, contiguous sequences of #x20's are collapsed to
a single #x20, and leading and trailing #x20's are removed
4 - 92
Deriving simple data types 4 - XML Schema
Facets for the decimal type
! Decimal
" totalDigits
" fractionDigits
" pattern
" whiteSpace
" enumeration
" maxInclusive
" maxExclusive
" minInclusive
" minExclusive
4 - 93
Deriving simple data types 4 - XML Schema
enumeration restriction
<simpleType name = ”SizeType”>
<restriction base=”string”>
<enumeration value = ”S”/>
<enumeration value = ”M”/>
<enumeration value = ”L”/>
<enumeration value = ”XL”/>
</restriction>
</simpleType>
...
<attribute name=”size” type=”SizeType”/>
...
4 - 94
Deriving simple data types 4 - XML Schema
totalDigits and fractionDigits restrictions
<simpleType name = ”value”>
<restriction base = ”decimal”>
<totalDigits value = ”9”/>
<fractionDigits value = ”3”/>
<minInclusive value = ”0”/>
<maxInclusive value = ”999999.999”/>
</restriction>
</simpleType>
4 - 95
Deriving simple data types 4 - XML Schema
Regular Expressions
! A regular expression is a string with special character sequences
which form a pattern to describe a set of string values (the
values in this set match the pattern)
! W3C
" http://www.w3c.org/TR/xmlschema-2/#regexs
" http://www.w3.org/TR/xmlschema-0/#regexAppendix
! UNICODE also defines regular expressions
! Perl uses regular expressions
4 - 96
Deriving simple data types 4 - XML Schema
Basic Grammar
! A regular expression is a set of one or more data assertions.
Each assertion is also known as branch. A regular expression is
true if any of its brach expressions is true.
branch
branch1 | branch2 | … | branchN
Or
<simpleType name = ”SizeType”>
<restriction base=”string”>
<pattern value = ”S|M|L|XL”/>
</simpleType>
4 - 97
Deriving simple data types 4 - XML Schema
Basic Grammar
! Each regular expression branch is formed by zero or more
concated components.
! Each component is composed by an atom and an optional
quantifier.
<simpleType name = ”TimeType”>
<restriction base = ”string”>
<pattern value = ”[0-9]{2}:[0-9]{2}:[0-9]{2}”/>
</restriction>
</simpleType>
Atom
C1 Component C2 C3 C4 C5
Quantifier
4 - 98
Deriving simple data types 4 - XML Schema
Basic Grammar
! A regular expression atom can be:
" A single character (Unicode)
! Some characters, called metacharacters have special meanings (e.g.
“.” means any character except end of line)
! “\” marks the beggining of an escape sequence.
" A character class
! Ranges (e.g.: [a-z])
! Character categories (e.g.: greek characters, financial symbols, etc.)
! Others
" Another regular expression inside brackets
<pattern value = ”XPTO-[0-9]\| .[A-C]”/>
XPTO-1|xA XPTO-8|ZB XPTO-8|AAB
4 - 99
Deriving simple data types 4 - XML Schema
Regular expressions – Metacharacters
Metacharacters Description Example
. Any character except end-of-line (#x0D, #x0A) .
\ Escape sequence start \n
? Zero or one occurrences a?
* Zero or one occurrences b*
+ One or more occurrences c+
{ } Contains a numeric quantifier or a character group {2}
( ) Contains a regular expression (atom or another regular expression) (abc)
[ ] Contains a class or characters [0-9]
4 - 100
Deriving simple data types 4 - XML Schema
Regular expressions – special characters
Special characters Description Special characters Description
\n New-line \* “*” character
\r Carriage return \+ “+” character
\t Tab \[ “[” character
\\ “\” character \] “]” character
\| “|” character \{ “{” character
\. “.” character \} “}” character
\^ “^” character \( “(” character
\? “?” character \) “)” character
4 - 101
Deriving simple data types 4 - XML Schema
Regular expressions – Quantifiers
Quantifiers Description Example
? Zero or one occurrences a?
* Zero or more occurrences b*
+ One or more occurrences c+
{min,max} Between min and max occurrences {2,5}
{equ} Exactly equ occurrences {3}
{min,} At least min occurrences {2,}
{0,max} From zero but no more than max occurrences {0,6}
{0,0} Exactly zero occurrences {0,0}
4 - 102
Deriving simple data types 4 - XML Schema
Regular expressions – Character classes
• PERL classic character classes
Class Description Class Description
\s Spaces. (whitespace, tabs, line-feeds \i Valid characters of the first letter of an XML
and CR) Name (letters and the “-” character).
\S Non-white characters (complementary \i Complementary set of \i
set of \s)
\d Digits (from “0” to “9” and digits in \c Valid characters in XML Names: letters, “.”,
other alphabets) ”:”, ”-” and other UNICODE special
characters.
\D Non-digit characters (complementary \C Complementary set of \c
set of \d)
\w Word characters. Excluding
punctuation, separaters and other
characters.
\W Non-word characters. (complementary
set of \w)
•There are also the UNICODE character classes
4 - 103
Deriving simple data types 4 - XML Schema
Regular expressions – user classes
! Character lists limited by “[“ and “]”
! Can use “-” for defining ranges or characters
! Can use “^” as the not operator (of what appears next)
! Can use subtraction between sets
[abdxyz] – one of the specified characters
[a-z] – a caracter in the range of “a” to “z”.
[^a-z] – a character not in the range of “a” to “z”.
[^\d] – non digit character.
[-+] – numeric sign character
4 - 104
Deriving simple data types 4 - XML Schema
Regular expressions - Examples
Expression Correspondence(s)
chapter \d chapter 0, chapter 1, chapter 2, ...
a*x x, ax, aax, aaax, ...
a?x ax, x
a+x ax, aax, aaax, ...
(a|b)+x ax, bx, aax, abx, bax, bbx, aaax, aabx, abax, abbx, baax,
babx, bbax, bbbx, aaaax, ...
[abcde]x ax, bx, cx, dx, ex
[a-e]x ax, bx, cx, dx, ex
[-ae]x -x, ax, ex
[ae-]x ax, ex, -x
[a-e-[bd]]x ax, cx, ex
[^0-9]x Non-digit characters followed by the x character
\Dx Non-digit characters followed by the x character
4 - 105
Deriving simple data types 4 - XML Schema
Content Models
<complexType>
Type shall contain
Contains elements? Yes
elements or attributes ?
Yes No
Derived by: Derived by: No
<restriction> <restriction>
contentModel ? <simpleType> ? <simpleType>
attribute or attributeGroup? facets?
anyAtttibute? atttibute or attributeGroup?
<restriction> anyAttribute?
Derived by:
or <restriction>
<restriction>
<extension> ou
contentModel ? <extension> or
attribute or attributeGroup? atttibute or attributeGroup? <list>
anyAtttibute? anyAtttibute? or
<extension> <extension> <union>
4 - 106
Content Models 4 - XML Schema
Complex types
! Complex elements or complex types can be
" Created (in opposition to simple types)
" Derived by restriction
" Derived by extension
! Complex types can be
" Simple model
! Simple types plus attributes
" Complex model
! Containing child elements
4 - 107
Content Models 4 - XML Schema
Content Models: Compositors
! Sequence
" Child elements should appear in the specified order
! Choice
" Choice of one of the child elements
! All
" Child elements can appear in any order
4 - 108
Content Models 4 - XML Schema
Compositors: sequence
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name=“root">
<xs:complexType>
<xs:sequence>
<xs:element name="A" type="xs:string"/>
<xs:element name="B" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0"> <!ELEMENT root (A, B))>
<root >
<A>text</A>
<B>text</B>
</root>
4 - 109
Content Models 4 - XML Schema
Compositors: sequence
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="A" type="xs:string" minOccurs="1" maxOccurs="2"/>
<xs:element name="B" type="xs:string" minOccurs="1" maxOccurs=“3"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0">
<root> <!ELEMENT root (A1-2, B1-3))>
<A/>
<A/>
<B/>
<B/>
<B/>
</root>
4 - 110
Content Models 4 - XML Schema
Compositors: sequence
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="2">
<xs:element name="A" type="xs:string" minOccurs="1" maxOccurs="2"/>
<xs:element name="B" type="xs:string" minOccurs="1" maxOccurs=“3"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0">
<root> <!ELEMENT root (A1-2, B1-3)2)>
<A/> <A/> <B/> <B/> <B/>
<A/> <B/>
</root>
4 - 111
Content Models 4 - XML Schema
Compositors: choice
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:choice>
<xs:element name="A" type="xs:string"/>
<xs:element name="B" type="xs:string"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0"> <?xml version="1.0"> <!ELEMENT root (A | B))>
<root> <root>
<A>text</A> <B>text</B>
</root> </root>
4 - 112
Content Models 4 - XML Schema
Compositors: choice
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:choice>
<xs:element name="A" type="xs:string“
minOccurs=“1" maxOccurs="unbounded"/>
<xs:element name="B" type="xs:string" maxOccurs=“2"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0"> <?xml version="1.0"> <!ELEMENT root (A1-" | B1-2))>
<root> <root>
<A/> <A/> <A/> <B/>
</root> </root>
4 - 113
Content Models 4 - XML Schema
Compositors: choice
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:choice minOccurs=“1" maxOccurs=“2“ >
<xs:element name="A" type="xs:string“
minOccurs=“1" maxOccurs="unbounded"/>
<xs:element name="B" type="xs:string" maxOccurs=“2"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0"> <?xml version="1.0"> <!ELEMENT root ((A1-" | B1-2) 1-2))>
<root> <root>
<A/> <A/> <A/> <B/>
<B/> <B/> <B/>
</root> </root>
4 - 114
Content Models 4 - XML Schema
Compositors: all
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:all>
<xs:element name="A" type="xs:string“ minOccurs=“0"/>
<xs:element name="B" type="xs:string“ minOccurs=“0"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<root> <root> <root> <root> <root>
<A/> <B/> <A/> <B/>
<B/> <A/> </root> </root> </root>
</root> </root>
4 - 115
Content Models 4 - XML Schema
Combining and repeating compositors
! Sequence and choice
" Can be repeated by using minOccurs and maxOccurs
" Can be combined
! Sequence
! Choice and Sequence
! Choice
! Choice and Sequence
! All
" Complex types can only have an all compositor.
" Should appear as the first child of the complexType element.
" It’s not possible to combine “all” either with sequence or choice.
" Inside “all” minOccurs and maxOccurs can not be bigger than 1.
4 - 116
Content Models 4 - XML Schema
Reviewing xs:element
<element
abstract = Boolean : false
block = (#all | List of (extension | restriction | substitution))
default = string
final = (#all | List of (extension | restriction))
fixed = string
form = (qualified | unqualified)
id = ID
maxOccurs = ( nonNegativeInteger | unbounded) : 1
minOccurs = nonNegativeInteger : 1
name = NCName
nillable = Boolean : false
ref = QName substitutionGroup = QName
type = QName {any attributes with non-schema Namespace}…>
Content: (annotation?, ((simpleType | complexType)?, (unique |
key |
keyref)*))
</element>
4 - 117
Content Models 4 - XML Schema
Mixed content (text and elements)
! Element that can have in their content text and elements mixed
together
<xs:element name="root"> <root>
<xs:complexType mixed="true"> Free text
<xs:sequence> <A/>
<xs:element name="A"/> Free text
<xs:element name="B"/> <B/>
</xs:sequence> Free text
</xs:complexType> </root>
</xs:element>
<root>
Free text
<B/>
Free text
<A/>
Free text
</root>
4 - 118
Content Models 4 - XML Schema
Any content (text and elements)
! Element can have in their content text and any elements mixed
together
<xs:element name="root">
<xs:complexType mixed="true"> <root>
<xs:sequence> Free text
<xs:any minOccurs="1“ <Z/>
maxOccurs="unbounded" Free text
processContents="skip"/> <X/>
</xs:sequence> Free text
</xs:complexType> </root>
</xs:element>
! processContent can have the following values:
" skip: indication not to validate this document content
4 - 119
Content Models 4 - XML Schema
Any content (text and elements)
! processContent can have the following values:
" skip: indication not to validate this document content.
" strict: must validate the element content according to the indicated
namespace
" lax: should try to validate the element content according to the
indicated namespace
<xs:element name="root”>
<xs:complexType mixed="true">
<xs:sequence>
<xs:any namespace=“http://www.w3c.org/1999/xhtml“
minOccurs=“0“
maxOccurs="unbounded"
processContents="skip"/>
</xs:sequence>
</xs:complexType>
</xs:element>
4 - 120
Content Models 4 - XML Schema
Any content (text and elements)
! Besides using elements from a namespace can also be used
attributes
<xs:element name="root">
<xs:complexType mixed="true">
<xs:sequence>
<xs:any namespace=“http://www.w3c.org/1999/xhtml“
minOccurs=“0“
maxOccurs="unbounded"
processContents=“strict"/>
</xs:sequence>
<xs:anyAttribute namespace=“http://www.w3c.org/1999/xhtml”>
</xs:complexType>
</xs:element>
4 - 121
Content Models 4 - XML Schema
Empty content (no text or elements)
! When we intend an empty element
<xs:element name="root">
<xs:complexType >
<xs:sequence maxOccurs="1">
<xs:element name="A">
<xs:complexType/>
</xs:element>
<xs:element name="B" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<?xml version="1.0"?> <?xml version="1.0"?> <?xml version="1.0"?>
<root> <root> <root>
<A/> <A></A> <A> </A>
<B/> <B/> <B/>
</root> </root> </root>
4 - 122
Content Models 4 - XML Schema
Content: nil value
! When an empty element is of string type it is interpreted as “”.
When the element is of another type the it should explicitly
indicated the nil value.
<xs:element name="root">
<xs:complexType >
<xs:sequence maxOccurs="1">
<xs:element name="A" type="xs:integer" nillable="true"/>
<xs:element name="B" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<A xsi:nil="true"/>
<B/>
</root>
4 - 123
Content Models 4 - XML Schema
Grouped models
<xsd:element name="Book" >
<xsd:complexType>
<xsd:sequence>
<xsd:group ref="PublicationElements"/>
<xsd:element name="ISBN" type="string"/>
<xsd:element name="Reviewer" type="string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="CD" >
<xsd:complexType>
<xsd:sequence>
<xsd:group ref="PublicationElements"/>
<xsd:element name="RecordingStudio" type="string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:group name="PublicationElements">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:string"/>
</xsd:sequence>
</xsd:group>
4 - 124
Content Models 4 - XML Schema
Grouped models
! Have to be named.
! Have to be global
! They only exist to group sets of elements
! Can not have attributes
! By using a group, cardinality restrictions (minOccurs,
maxOccurs) can be used.
4 - 125
Content Models 4 - XML Schema
Attributes
! An element with attributes is a complex type
" Simple content - no child elements
" Complex content - with child elements
! Attribute declaration
<attribute
default = string
fixed = string
form = (qualified | unqualified)
id = ID
name = NCName
ref = QName
type = QName
use = (optional | prohibited | required): optional
{any attributes with non-schema Namespace}>
Content: (annotation?, (simpleType?))
</attribute>
4 - 126
Content Models 4 - XML Schema
Attributes: example
<?xml version = ”1.0”?>
<schema xmlns =”http://www.w3.org/2001/XMLSchema”>
<element name = ”incomingEmail”>
<complexType>
<sequence>
<element name = ”from” type = ”string”/>
<element name = ”subject” type = ”string”/>
<element name = ”message” type = ”string”/>
</sequence>
<attribute name = ”read” type=”boolean”/>
</complexType>
</element>
</schema>
4 - 127
Content Models 4 - XML Schema
Attributes group
<attributeGroup name = ”emailDetails”>
<attribute name = ”priority” type = ”string”/>
<attribute name = ”attachments” type = ”boolean”/>
</attributeGroup>
<element name = ”incomingEmail”>
<complexType>
<sequence>
<element name = ”from” type = ”string”/>
<group ref = ”Subjectmessage”/>
</sequence>
<attribute name = ”read” type=”boolean”/>
<attributeGroup ref=”emailDetails”/>
</complexType>
</element>
<element name = ”outgoingEmail”>
<complexType>
<sequence>
<element name = ”to” type = ”string”/>
<group ref = ”Subjectmessage”/>
</sequence>
<attribute name = ”sent” type=”boolean”/>
<attributeGroup ref=”emailDetails”/>
</complexType>
</element>
4 - 128
Content Models 4 - XML Schema
Complex types
! Complex elements or complex type can
" Be created (by opposition to simple types)
" Be derivate by restriction
" Be derivate by extension
! Complex types can be
" Of simple model
! Are simple types plus attributes
" Of complex model
! Contain child elements
4 - 129
Content Models 4 - XML Schema
Complex type derivation
<complexType>
The type should contain
Should contain elements? Yes
elements or attributes?
Yes No
Derivated by: Derivated by: No
<restriction> <restriction>
contentModel ? <simpleType> ? <simpleType>
attribute ou attributeGroup? facets?
anyAtttibute? atttibute ou attributeGroup?
<restriction> anyAttribute?
Derivated by:
or <restriction>
<restriction>
<extension> or
contentModel ? <extension> or
attribute ou attributeGroup? atttibute ou attributeGroup? <list>
anyAtttibute? anyAtttibute? or
<extension> <extension> <union>
4 - 130
Content Models 4 - XML Schema
Derivation by extension
When we extend a complex base type
! Use extension with complexContent
When we extend a simple base type
! Use extension with simpleContent
4 - 131
Content Models 4 - XML Schema
Derivation from a complex base type
! Extending a complex type
<complexType name=”NameType”>
<sequence>
<element name=”Name” type=”string”/>
<element name=”FirstSurname” type=”string”/>
<element name=”SecondSurname” type=”string”/>
</sequence>
</complexType>
<complexType name=”PersonType”>
<complexContent>
<extension base=”NameType”>
<sequence>
<element name=”age” type=”positiveInteger”/>
</sequence>
<attribute name=”weight” type=”positiveInteger”/>
</extension>
</complexContent>
</complexType>
4 - 132
Content Models 4 - XML Schema
Derivation from a simple base type
! Extending a simple type:
<complexType name=”MessageType”>
<simpleContent>
<extension base=”string”>
<attribute name=”sent” type=”boolean”/>
</extension>
</simpleContent>
</complexType>
<element name=”message” type=”MessageType”/>
4 - 133
Content Models 4 - XML Schema
Derivation by restriction
<complexType name=”NameType”>
<sequence>
<element name=”Name” type=”string”/>
<element name=”FirstSurname” type=”string”/>
<element name=”SecondSurname” type=”string”/>
</sequence>
</complexType>
<complexType name=”SimplifiedNameType”>
<complexContent>
<restriction base=”NameType”>
<sequence>
<element name=”Name” type=”string”/>
<element name=”FirstSurname” type=”string”/>
</sequence>
</restriction>
</complexContent>
</complexType>
4 - 134
Content Models 4 - XML Schema
Primary keys and Foreign keys
! Primary keys
" ID and IDs
" Only applicable to attributes
" Allows uniqueness to all the document
" This data types are still available in the XML Schema
" New feature - unique
! References to primary keys
" IDREF and IDREFS
" Allow the reference to a key in the document
" This date types are still available in the XML Schema
" New feature – key and keyref
4 - 135
Primary keys and Foreign keys 4 - XML Schema
Unique and Key advantages
! Allow the definition that an element content should be unique.
! Allow the definition that an element content and its attributes are
unique.
! Allow a distinction between unique and key
! Allow the declaration of the document region on which the
uniqueness restriction is valid.
! Key: an element or attribute (or a combination) defined as key
should:
" Be always present (minOccurs bigger than zero)
" Not have the attribute nillable = “false”
" Always be unique.
! Key implies unique but unique does not implies key
4 - 136
Primary keys and Foreign keys 4 - XML Schema
The key element
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns="http://www.books.org"
xmlns:bk="http://www.books.org"
elementFormDefault="qualified">
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
....
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="PK">
<xsd:selector xpath="bk:Book"/>
<xsd:field xpath="bk:ISBN"/>
</xsd:key>
</xsd:element>
</xsd:schema>
4 - 137
Primary keys and Foreign keys 4 - XML Schema
The key element
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns="http://www.books.org"
xmlns:bk="http://www.books.org"
elementFormDefault="qualified">
<xsd:element name="BookStore">
<xsd:complexType>
</xsd:complexType>
<xsd:key name="PK">
<xsd:selector xpath="bk:Book"/>
<xsd:field xpath="bk:ISBN"/>
</xsd:key>
</xsd:element>
</xsd:schema>
Inside the <BookStore> element we define a key named PK. It selects each
book and inside it the ISBN element will be a key.
4 - 138
Primary keys and Foreign keys 4 - XML Schema
The key element
<?xml version="1.0"?>
<BookStore xmlns="http://www.books.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.books.org
BookStore.xsd">
<Book>
<Title>My Life and Times</Title>
<Author>Paul McCartney</Author>
<Date>1998</Date>
<ISBN>1-56592-235-2</ISBN>
<Publisher>McMillin Publishing</Publisher>
</Book>
<Book>
<Title>Illusions The Adventures of a Reluctant Messiah</Title>
<Author>Richard Bach</Author>
<Date>1977</Date>
<ISBN>0-440-34319-4</ISBN>
<Publisher>Dell Publishing Co.</Publisher>
</Book>
<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>
</BookStore>
4 - 139
Primary keys and Foreign keys 4 - XML Schema
Notes on how to use key
! Should be inside an <element>
! Should be in the end of the <element> (after the content model
and the attribute declarations)
! Use the <selector> as child of <key> to select the element set
(under <element>) for which the key should be aplied.
! Use the <field> element as child of <key> to identify the element
or attribute that should be the key
! In case a key is composed by various elements or attributes
various <key> elements should be used
4 - 140
Primary keys and Foreign keys 4 - XML Schema
Key: composed keys
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.CostelloReunion.org"
xmlns="http://www.CostelloReunion.org"
xmlns:reunion="http://www.CostelloReunion.org"
elementFormDefault="qualified">
<xsd:element name="Y2KFamilyReunion">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Participants" >
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" 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>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="PK">
<xsd:selector xpath="reunion:Participants/reunion:Name"/>
<xsd:field xpath="reunion:First"/>
<xsd:field xpath="reunion:Last"/>
</xsd:key> 4 - 141
</xsd:element>
</xsd:schema>
4 - XML Schema
Unique
! The <unique> element is used exactly in the same way as <key>
<unique name= ”...”>
<selector xpath = ”...” />
<field xpath = ”...” />
...
</unique>
! The only difference is that is validates always if the elements are
present
! Applicable in opcional elements
4 - 142
Primary keys and Foreign keys 4 - XML Schema
Foreign keys: keyref
! When we declare and IDREF type element then the element
should reference an ID attribute and the XML parser verifies that
the IDREF values matches or not an existing ID.
! In a similar way keyref defines a restriction:
" The value of this element should match the value of an element
referenced by it.
<keyref name= ”...” refer = ”...”>
<selector xpath = ”...” />
<field xpath = ”...” />
...
</keyref>
4 - 143
Primary keys and Foreign keys 4 - XML Schema
Keyref: an example
<?xml version="1.0"?>
<Library xmlns="http://www.library.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.library.org
Key
AuthorSigningAtLibrary.xsd">
<Books>
<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> We want to assure that ISBN
</Book>
... indicated in the author is in
</Books>
<GuestAuthors> fact an book ISBN.
<Author>
<Name>Richard Bach</Name>
<BookForSigning>
<Title>Illusions The Adventures of a Reluctant Messiah</Title>
<ISBN>0-440-34319-4</ISBN>
</BookForSigning>
</Author> Refence element
</GuestAuthors>
</Library>
4 - 144
Primary keys and Foreign keys 4 - XML Schema
Keyref: Example
<xsd:element name="Library">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Books">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Book" maxOccurs="unbounded"/> <GuestAuthors>
</xsd:sequence> <Author>
</xsd:complexType> <Name>Richard Bach</Name>
<BookForSigning>
</xsd:element>
<Title>Illusions ….</Title>
<xsd:element ref="GuestAuthors"/>
<ISBN>0-440-34319-4</ISBN>
</xsd:sequence> </BookForSigning>
</xsd:complexType> </Author>
<xsd:key name="PK"> </GuestAuthors>
<xsd:selector xpath="bk:Books/bk:Book"/>
<xsd:field xpath="bk:ISBN"/>
</xsd:key>
<xsd:keyref name="isbnRef" refer="PK">
<xsd:selector xpath="bk:GuestAuthors/bk:Author/bk:BookForSigning"/>
<xsd:field xpath="bk:ISBN"/>
</xsd:keyref>
</xsd:element>
4 - 145
Primary keys and Foreign keys 4 - XML Schema
Key and Keyref
Defining the ISBN key on book
<xsd:key name="PK">
<xsd:selector xpath="bk:BookStore/bk:Book"/>
<xsd:field xpath="bk:ISBN"/>
</xsd:key>
Indicating the foreign key
<xsd:keyref name="isbnRef" refer="PK">
<xsd:selector xpath="bk:GuestAuthors/bk:Author/bk:BookForSigning"/>
<xsd:field xpath="bk:ISBN"/>
</xsd:keyref>
The number of fields (<field>) of the <key> element must be equal to the number off
Fields (<field>) in keyref.
Besides that they should be of the same type an be in the same order.
4 - 146
Primary keys and Foreign keys 4 - XML Schema
Control the scope of key, keyref and unique
! The key, keyref and unique elements can be declared in any part
of the schema (as long as they are in the end of an element
declaration).
! The place where they are defined determines the restriction
scope.
4 - 147
Primary keys and Foreign keys 4 - XML Schema
Restrictions scope
XML
Document
Splitting a
All the keys and
XML document into respective
Document several references could
be lost
XML
Document
All keys are maintained
All key references are maintained
4 - 148
Primary keys and Foreign keys 4 - XML Schema
Schemas in several documents
! To promote the reuse of the modules of a schema
! To modularize the schema functionalities
! So that the schema size is the lowest possible and
consequentially be easier to maintain.
! To share several schema between several users and
organizations.
4 - 149
Schemas in several documents 4 - XML Schema
Modular organization
core
opcional
core
opcional
opcional
opcional
Principals schemas
Modular schemas
4 - 150
Schemas in several documents 4 - XML Schema
What are we getting from modules
Principal schema Modular schema
<schema>
<element name=”...”>
<schema> <attribute name=”...”>
<complexType name=”...”>
<simpleType name=”...”>
</schema>
<group name=”...”>
<attributeGroup name=”...”>
</schema>
Global named type definitions and global declarations
4 - 151
Schemas in several documents 4 - XML Schema
Reusing mechanisms
! The XML Schema features the following mechanisms to access
the modules of a schema.
" The include element
" The redefine element
" The import element
4 - 152
Schemas in several documents 4 - XML Schema
Possible reusing sceneries
! Homogeneous namespace
! Heterogeneous namespace
! No namespace
4 - 153
Schemas in several documents 4 - XML Schema
Homogeneous namespace
!For schemas that use the same namespace one can use:
! The include mechanism
! The redefine mechanism
4 - 154
Schemas in several documents 4 - XML Schema
Homogeneous namespace: Example
Types:
typeLib.xsd nameType
addressType
catalog.xsd
contact.xsd Types:
contactType
All schemas have the same namespace: contacts
4 - 155
Schemas in several documents 4 - XML Schema
Homogeneous: Example – typeLib.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="contacts"
xmlns:ct="contacts"
elementFormDefault ="qualified">
<xs:complexType name=”nameType">
<xs:sequence>
<xs:element name=”Name" type="xs:string"/>
<xs:element name=”Surname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name=”addressType">
<xs:sequence>
<xs:element name=”street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name=”PostalCode" type="xs:string"/>
<xs:element name=”Country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
4 - 156
Schemas in several documents 4 - XML Schema
Homogeneous: Example – contact.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="contacts"
xmlns:ct="contacts"
elementFormDefault ="qualified">
<xs:include schemaLocation="typelib.xsd"/>
<xs:complexType name=”contactType">
<xs:sequence>
<xs:element name=”completeName" type="ct:nameType"/>
<xs:element name=”address" type="ct:addressType" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name=”observationsType">
<xs:restriction base="xs:string">
<xs:maxLength value="256"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
4 - 157
Schemas in several documents 4 - XML Schema
Homogeneous: Example – include use
! Principal schema: catalog.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="contacts"
xmlns:ct="contacts"
elementFormDefault ="qualified">
<!--We can not included because its already included in contactos.xsd
<xs:include schemaLocation = "typeLib.xsd"/> -->
<xs:include schemaLocation = "contacts.xsd"/>
<xs:element name = "catalog">
<xs:complexType>
<xs:sequence>
<xs:element name="contact" type="ct:contactType"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
4 - 158
Schemas in several documents 4 - XML Schema
Homogeneous: Example – Final diagram
4 - 159
Schemas in several documents 4 - XML Schema
Homogeneous: Example – instance
<?xml version="1.0" encoding="UTF-8"?>
<catalog
xmlns="contacts"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="contacts catalog.xsd">
<contact>
<completeName>
<Name></Name>
<Surname></Surname>
</completeName>
<address>
<street></street>
<city></city>
<PostalCode></PostalCode>
<Country></Country>
</address>
</contact>
</catalog>
4 - 160
Schemas in several documents 4 - XML Schema
Homogeneous: Using redefine
Types:
typeLib.xsd nameType
addressType
include
catalog.xsd
contact.xsd Types:
contactType
redefine
Types:
contactType
+ extension
telephone
4 - 161
4 - XML Schema
Homogeneous: Using redefine
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="contacts"
xmlns:ct="contacts"
elementFormDefault ="qualified">
<xs:redefine schemaLocation="contacts.xsd">
<!-- includes with redefinition -->
<xs:complexType name=”contactType">
<xs:complexContent>
<xs:extension base="ct:contactType">
<xs:sequence>
<xs:element name="telephone" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:redefine>
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<!-- observationsType was not redefined but was included -->
<xs:element name="observations" type="ct:observationsType"/>
<xs:element name="contact" type="ct:contactType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
4 - 162
Schemas in several documents 4 - XML Schema
Homogeneous: Using redefine - the instance
<?xml version="1.0" encoding="UTF-8"?>
<catalog
xmlns="contacts"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="contacts catalog.xsd">
<observations></observations>
<contact>
<completeName>
<Name></Name>
</Surname></Surname>
</completeName>
<address>
<street></street>
<city></city>
<PostalCode></PostalCode>
<Country></Country>
</address>
</contact>
</catalog>
4 - 163
Schemas in several documents 4 - XML Schema
Heterogeneous namespace
!For schemas that use different namespaces one uses:
!The import mechanism
4 - 164
Schemas in several documents 4 - XML Schema
Heterogeneous namespace: Example
EN=typeLib
Types:
EN=contactcatalog typeLib.xsd nameType
addressType
catalog.xsd
contact.xsd Types:
contactType
EN=contact
All schemas have different namespaces
4 - 165
4 - XML Schema
Heterogeneous: Example - typeLib.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace=”typeLib"
xmlns:typ=”typeLib"
elementFormDefault ="qualified">
<xs:complexType name=”nameType">
<xs:sequence>
<xs:element name=”Name" type="xs:string"/>
<xs:element name=”Surname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name=”addressType">
<xs:sequence>
<xs:element name=”street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name=”PostalCode" type="xs:string"/>
<xs:element name=”Country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
4 - 166
Schemas in several documents 4 - XML Schema
Heterogeneous: Example - contact.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="contacts"
xmlns:ct="contacts”
xmlns:typ="typeLib"
elementFormDefault ="qualified">
<xs:import namespace="typeLib" schemaLocation="typelib.xsd"/>
<xs:complexType name=”contactType">
<xs:sequence>
<xs:element name=”completeName" type=”typ:nameType"/>
<xs:element name=”address" type=”typ:addressType" minOccurs="0”/
</xs:sequence>
</xs:complexType>
<xs:simpleType name=”observationsType">
<xs:restriction base="xs:string">
<xs:maxLength value="256"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
4 - 167
Schemas em vários documentos 4 - XML Schema
Heterogeneous: Example - catalog.xsd
! Principal schema: catalog.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="catalogContacts"
xmlns:cct="catalogContacts”
xmlns:ct=”contacts"
elementFormDefault ="qualified">
<xs:import namespace="contacts" schemaLocation = "contacts.xsd"/>
<xs:element name = "catalog">
<xs:complexType>
<xs:sequence>
<xs:element name="contact" type="ct:contactType"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
4 - 168
Schemas in several documents 4 - XML Schema
Heterogeneous: Example - catalog.xsd
<?xml version="1.0" encoding="UTF-8"?>
<cct:catalog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cct="catalogContacts"
xmlns:ct="contacts"
xmlns:typ="typeLib"
xsi:schemaLocation="catalogContacts catalog.xsd">
<cct:contact>
<ct:completeName>
<typ:Name></typ:Name>
<typ:Surname></typ:Surname>
</ct:completeName>
<ct:morada>
<typ:street></typ:street>
<typ:city></typ:city>
<typ:PostalCode></typ:PostalCode>
<typ:Country></typ:Country>
</ct:morada>
</cct:contact>
</cct:catalog>
4 - 169
Schemas in several documents 4 - XML Schema
Heterogeneous: Example – catalog.xml (v1)
<?xml version="1.0" encoding="UTF-8"?>
<cct:catalog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cct="catalogContacts"
xmlns:ct="contacts"
xmlns:typ="typeLib"
xsi:schemaLocation="catalogContacts catalog.xsd">
<cct:contact>
<ct:completeName>
<typ:Name></typ:Name>
<typ:Surname></typ:Surname>
</ct:completeName>
<ct:address>
<typ:street></typ:street>
<typ:city></typ:city>
<typ:PostalCode></typ:PostalCode>
<typ:Country></typ:Country>
</ct:address>
</cct:contact>
</cct:catalog>
4 - 170
Schemas in several documents 4 - XML Schema
Heterogeneous: Example – catalog.xml (v2)
<?xml version="1.0" encoding="UTF-8"?>
<catalog
xmlns="catalogContacts"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="catalogContacts catalog.xsd">
<contact>
<nomeCompleto xmlns="contacts">
<Name xmlns="typeLib"></Name>
<Surname xmlns="typeLib"></Surname>
</nomeCompleto>
<address xmlns="contacts">
<street xmlns="typeLib"></street>
<city xmlns="typeLib"></city>
<PostalCode xmlns="typeLib"></PostalCde>
<Country xmlns="typeLib"></Country>
</address>
</contact>
</catalog>
4 - 171
Schemas in several documents 4 - XML Schema
Without namespace
! Schemas without namespaces
! The include mechanism
! The redefine mechanism
4 - 172
Schemas in several documents 4 - XML Schema
Without namespaces
Types:
typeLib.xsd nameType
addressType
catalog.xsd
contact.xsd Types:
contactType
The schemas don’t have namespaces
4 - 173
Schemas in several documents 4 - XML Schema
Without namespaces
! The TypeLib.xsd schema
<schema xmlns = ”http://www.w3.org/2001/XMLschema”
elementFormDefault = ”qualified”>
...
</schema>
! The contacto.xsd schema
<schema xmlns = ”http://www.w3.org/2001/XMLschema”
elementFormDefault = ”qualified”>
...
</schema>
4 - 174
Schemas in several documents 4 - XML Schema
Without NS: include and redefine-catalog.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="catalogContacts"
xmlns="catalogContacts"
elementFormDefault="qualified">
<xs:redefine schemaLocation="contactss.xsd">
<!-- include with definition -->
<xs:complexType name=”contactType">
<xs:complexContent>
<xs:extension base=”contactType">
<xs:sequence>
<xs:element name="telephone" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:redefine>
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<!-- the observationsType was not redefined but was included -->
<xs:element name="observations" type=”observationsType"/>
<xs:element name="contact" type=”contactType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
4 - 175
Schemas in several documents 4 - XML Schema
Without NS: include and redefine– catalog.xml
<?xml version="1.0" encoding="UTF-8"?>
<catalog
xmlns="catalogContacts"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="catalogContacts redefine-catalog.xsd">
<observations></observations>
<contact>
<completeName xmlns="">
<Name></Name>
<Surname></Surname>
</completeName>
<address xmlns="">
<street></street>
<city></city>
<PostalCode></PostalCode>
<Country></Country>
</address>
<telephone></telephone>
</contact>
</catalog>
4 - 176
Schemas in several documents 4 - XML Schema
Notes: include, redefine and import
! The include element allows access to the components in other
schemas
" All schemas should have the same namespace was the schema which is
being created
" The practical effect is the “copy” of the other schemas declarations
for insider the schema which is being created.
" When the schemas which are being included don’t declare any
namespace then, in the inclusion process, they adopt the
namespace of the schema being created.
! The redefine element allows, besides what include allows,
redefinition of some of the included components.
! These element must appear before any element declaration or
type definition.
4 - 177
Schemas in several documents 4 - XML Schema
Another import example
Nikon.xsd Olympus.xsd Pentax.xsd
Camera.xsd
4 - 178
Schemas in several documents 4 - XML Schema
Another import example: sources
<?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"> <?xml version="1.0"?>
<xsd:sequence> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
<xsd:element name="description" type="xsd:string"/> targetNamespace="http://www.olympus.com"
</xsd:sequence> xmlns="http://www.olympus.com"
</xsd:complexType> elementFormDefault="qualified">
</xsd:schema> Nikon.xsd <xsd:complexType name="lens_type">
<xsd:sequence>
<xsd:element name="zoom" type="xsd:string"/>
<xsd:element name="f-stop" type="xsd:string"/>
<?xml version="1.0"?> </xsd:sequence>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" </xsd:complexType>
targetNamespace="http://www.pentax.com" </xsd:schema> Olympus.xsd
xmlns="http://www.pentax.com"
elementFormDefault="qualified">
<xsd:complexType name="manual_adapter_type">
<xsd:sequence>
<xsd:element name="speed" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema> Pentax.xsd
4 - 179
Schemas in several documents 4 - XML Schema
Another import example : integration
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.camera.org"
xmlns:nikon="http://www.nikon.com"
xmlns:olympus="http://www.olympus.com"
xmlns:pentax="http://www.pentax.com"
elementFormDefault="qualified">
<xsd:import namespace="http://www.nikon.com"
schemaLocation="Nikon.xsd"/>
<xsd:import namespace="http://www.olympus.com"
schemaLocation="Olympus.xsd"/>
<xsd:import namespace="http://www.pentax.com"
schemaLocation="Pentax.xsd"/>
<xsd:element name="camera">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="body" type="nikon:body_type"/>
<xsd:element name="lens" type="olympus:lens_type"/>
<xsd:element name="manual_adapter" type="pentax:manual_adapter_type"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:schema>
4 - 180
Schemas in several documents 4 - XML Schema
Another import example : integration
4 - 181
Schemas in several documents 4 - XML Schema
Another import example: usage
<?xml version="1.0"?>
<c:camera xmlns:c="http://www.camera.org"
xmlns:nikon="http://www.nikon.com"
xmlns:olympus="http://www.olympus.com"
xmlns:pentax="http://www.pentax.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.camera.org
Camera.xsd">
<c:body>
<nikon:description>Ergonomically designed casing for easy handling</nikon:description>
</c:body>
<c:lens>
<olympus:zoom>300mm</olympus:zoom>
<olympus:f-stop>1.2</olympus:f-stop>
</c:lens>
<c:manual_adapter>
<pentax:speed>1/10,000 sec to 100 sec</pentax:speed>
</c:manual_adapter>
</c:camera>
4 - 182
Schemas in several documents 4 - XML Schema
Pratical rule on qualification
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
….
</xs:schema>
4 - 183
Schemas in several documents 4 - XML Schema