Understanding XML Basics and Schema
Understanding XML Basics and Schema
XML
Basic Notions
A. BOUKOTTAYA
Evolution of Mark-Up languages
2
XML basic principles
Use of tagged documents
– To capture the logical document structure
– To associate various styles without modifying the content
<report>
<introduction>This report aims at …</introduction>
<chapter>
<section> Introducing basic notions … </section>
<section> Advanced concepts are ….</section>
</chapter>
<chapter>
<section> ... </section> <section> ... </section>
<section> ... </section>
</chapter>
<chapter> ... </chapter>
</report>
…
3
Logical structure Vs Physical structure
Recette
Contenu
Présentation
multimedia
...
4
XML basic principles
Fundamental construct in XML doc: element
element = start and end tags, and text between them (content)
XML doc. must have a single root element encloses all other elements in the
doc.
Elements must nest properly ex:
<account>…<balance>…</balance>…</account>
An element may have attributes
– name = value pairs before ">" in start-tag
– attributes are strings, do not contain markup
– attributes appear only once in a given tag, unlike subelements which may be repeated
Example:
<account account-type="savings">
<account-nb>A-102</account-nb>
<branch-name>Perryridge</branch-name>
<balance>400</balance>
</account>
5
XML basic principles (Structure Definition)
An XML document is well-formed if:
– it starts with an XML declaration
– it has a root element
– all tags have start and end tags, or end with "/>"
– tags nest properly
An XML document is valid if:
– it is well-formed
– it conforms to the associated schema (if any)
6
XML basic principles (Structure Definition)
Describing document classes
– To enforce a consistant document content
– Another document type: message
{
Subject: XML Training
Optional signature { CV
7
XML basic principles (Structure Definition)
Describing document classes
– Through DTD (Document Type Definition)
8
XML basic principles (Structure Definition)
Describing document classes
– An example of document instance that conforms to the model
<Message>
<Subject> XML Training </Subject>
<Sender> C. Vanoirbeek </Sender>
<Receiver> A. Ballim </Receiver>
<Body>
<Parag> Is it a good idea to introduce basic concepts about SGML? </Parag>
<Parag> I think it is worth while </Parag>
</Body>
No signature
</Message>
9
XML Today…
De-facto standard for representing structured and semi-
structured information in a textual form.
CSS XML-Schema
XSL-Fo XSLT XPath XLink
Xquery XPointer
The XML family
MathML
SMIL SVG
XFrames
Soap WSDL
CC/PP XHTML XForms
10
Part I:
XML Schema
11
Why XML Schemas ?
DTDs present several limitations:
– It's a different syntax
• You write your XML (instance) document using one syntax and the DTD
using another syntax bad, inconsistent
– Limited data type capability
• DTDs support a very limited capability for specifying data types (only
PCDATA). You can't, for example, impose that an element is an integer
with a range of 0 to 100.
– Limited reuse capability (offer only ENTITY mechanism)
– Constructors (“,” , “¦”) and occurrences indicators (“*”, “+”, “?”)
don’t offer a lot of possibilities to constraint element’s
occurrences.
12
XML Schemas Vs DTDs
13
Example: [Link]
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="[Link]
targetNamespace="[Link]
xmlns="[Link]
elementFormDefault="qualified">
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Book" minOccurs="1" <!ELEMENT BookStore (Book)+>
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Book">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Title" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> <!ELEMENT Book (Title, Author, Date,
<xsd:element ref="Date" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/>
ISBN, Publisher)>
<xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Title" type="xsd:string"/> <!ELEMENT Title (#PCDATA)>
<xsd:element name="Author" type="xsd:string"/> <!ELEMENT Author (#PCDATA)>
<xsd:element name="Date" type="xsd:date"/>
<xsd:element name="ISBN" type="xsd:string"/> <!ELEMENT Date (#PCDATA)>
<xsd:element name="Publisher" type="xsd:string"/> <!ELEMENT ISBN (#PCDATA)>
</xsd:schema> <!ELEMENT Publisher (#PCDATA)>
14
Name Spaces
15
Name spaces
complexType
element BookStore
sequence Author
schema Book
boolean Title
string Publisher ISBN
integer Date
One difference between XML Schemas and DTDs is that the XML Schema vocabulary
is associated with a name (namespace). Likewise, the new vocabulary that you
define must be associated with a name (namespace).
16
Example: [Link] xsd = Xml-Schema Definition
<?xml version="1.0"?>
<xsd:schema An XML schema has « schema » as
xmlns:xsd="[Link] root element
targetNamespace="[Link]
xmlns="[Link]
elementFormDefault="qualified">
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType> Elements and data types:
</xsd:element>
<xsd:element name="Book">
<xsd:complexType> • schema
<xsd:sequence> • element
<xsd:element ref="Title" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="Author" minOccurs="1" maxOccurs="1"/>
• complex Type
<xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> • sequence
<xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> • string
<xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/>
</xsd:sequence> • boolean
</xsd:complexType> • integer
</xsd:element>
<xsd:element name="Title" type="xsd:string"/>
• …etc
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/> Come from the namespace:
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/> [Link]
</xsd:schema>
17
Target Name Space
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="[Link]
targetNamespace="[Link]
xmlns="[Link]
elementFormDefault="qualified">
<xsd:element name="BookStore">
<xsd:complexType> Elements defined by
<xsd:sequence> this schema
<xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType> - BookStore
</xsd:element>
<xsd:element name="Book"> - Book
<xsd:complexType> - Title
<xsd:sequence> - Author
<xsd:element ref="Title" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> - Date
<xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> - ISBN
<xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/>
- Publisher
</xsd:sequence>
</xsd:complexType> are to go in this space
</xsd:element>
<xsd:element name="Title" type="xsd:string"/> name
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:schema>
18
Default Name space
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="[Link]
targetNamespace="[Link]
xmlns="[Link] The default namespace is
elementFormDefault="qualified"> [Link]
<xsd:element name="BookStore"> which is the targetNamespace!
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Book" minOccurs="1"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element> This is referencing a
<xsd:element name="Book">
<xsd:complexType>
Book element declaration.
<xsd:sequence> Since there is no namespace
<xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> qualifier it is referencing the
<xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> Book element in the default
<xsd:element ref="Date" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> Namespace.
<xsd:element ref="Publisher" minOccurs="1"
maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:schema>
19
Default XMLSchema, Qualify targetNamespace
<?xml version="1.0"?>
<schema xmlns="[Link]
targetNamespace="[Link]
Note that
xmlns:bk="[Link] [Link]
elementFormDefault="qualified">
<element name="BookStore"> is the default
<complexType> namespace.
<sequence>
<element ref="bk:Book" maxOccurs="unbounded"/> Consequently, there
</sequence>
</complexType>
are no namespace
</element> qualifiers on
<element name="Book">
<complexType> - schema
<sequence> - element
<element ref="bk:Title"/>
<element ref="bk:Author"/> - complexType
<element ref="bk:Date"/>
<element ref="bk:ISBN"/>
- sequence
<element ref="bk:Publisher"/> - string
</sequence>
</complexType>
</element>
<element name="Title" type="string"/>
<element name="Author" type="string"/>
<element name="Date" type="string"/>
<element name="ISBN" type="string"/>
<element name="Publisher" type="string"/>
</schema>
20
Name Space: Conclusion
schemaLocation="[Link] targetNamespace="[Link]
[Link]"
[Link] [Link]
- uses elements from - defines elements in
namespace [Link] namespace [Link]
21
Multiple levels of checking
22
Data Types
23
Elements, Attributes declaration
Element Declaration:
<xsd:schema xmlns:xsd="[Link]
</xsd:schema>
Attribute Declaration:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsd:schema xmlns:xsd="[Link]
<xsd:element name="contacts" type="typeContacts"/>
<xsd:element name="remarque" type="xsd:string"/>
<xsd:complexType name="typeContacts">
<!—content declaration-->
<xsd:attribute name="maj" type="xsd:date"/>
</xsd:complexType>
</xsd:schema>
24
Data Types
25
Simple Types: Predefined Types
Data Types
String Boolean Entity ID IDREF Decimal Binary QName Float Time duration reccuring
duration
unsignedInt short
unsignedShort byte
unsignedByte
26
Simple type: examples
27
Complex types: Constructors and occurrence indicators
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="[Link]
targetNamespace="[Link]
xmlns="[Link]
elementFormDefault="qualified">
<xsd:element name="life">
<xsd:complexType>
<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"/>
XML Schema: </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:complexType>
</xsd:element>
</xsd:schema>
28
Expressing Any Order
Problem: create an element, Book, which contains Author, Title, Date, ISBN, and Publisher,
in any order (Note: this is very difficult and ugly with DTDs).
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="[Link]
targetNamespace="[Link]
xmlns="[Link]
elementFormDefault="qualified">
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
XML Schema: <xsd:all>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:all>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
29
MinOccurs And MaxOccurs
XML SCHEMA
DTD
MinOccurs MaxOccurs
* 0 Unbounded
+ 1 (default value) Unbounded
? 0 1 (default value)
30
Empty Complex Type
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="[Link]
targetNamespace="[Link]
xmlns="[Link]
elementFormDefault="qualified">
<xsd:element name="gallery">
<xsd:complexType>
<xsd:sequence>
Schema: <xsd:element name="image" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="href" type="xsd:anyURI" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
31
Referring Elements Declarations
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="[Link]
targetNamespace="[Link]
xmlns="[Link]
elementFormDefault="qualified">
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Book">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Title" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="Author" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="Date" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:schema>
32
Inlining Element Declarations
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="[Link]
targetNamespace="[Link]
xmlns="[Link]
elementFormDefault="qualified">
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType> Note that we have moved
<xsd:sequence> all the element declarations
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/> inline, and we are no
<xsd:element name="Date" type="xsd:string"/> longer referring to the
<xsd:element name="ISBN" type="xsd:string"/>
element declarations.
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence> This results in a much
</xsd:complexType> more compact schema!
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
33
Type Derivation
34
Creating your own Datatypes
A new datatype can be defined from an existing datatype
(called the "base" type) by specifying values for one or more of
the optional facets for the base type.
Two techniques: Type restriction and Type extension.
We can restrict and extend simple and complex types.
Example. The integer data type has 8 optional facets:
• totalDigits
• pattern
• whitespace
• enumeration
• maxInclusive
• maxExclusive
• minInclusive
• minExclusive
35
Restriction of Simple Types (facet)
36
General Form of restricting simple types by Specifying Facet Values
<xsd:simpleType name="US-Flag-Colors">
<xsd:restriction base="xsd:string">
This creates a new type called US-Flag-Colors.
<xsd:enumeration value="red"/>
An element declared to be of this type
<xsd:enumeration value="white"/>
must have either the value red, or white, or blue.
<xsd:enumeration value="blue"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="Emailadress">
<xsd:restriction base="xsd:string"> This creates a new type called Emailadress.
An element declared to be of this type
<xsd:pattern value="(.)+@(.)+"/> must have the form string@string.
</xsd:restriction>
</xsd:simpleType>
38
Restriction of Complex Types
<xsd:complexType name="typeContacts">
<xsd:sequence>
<xsd:element name="personne" maxOccurs="unbounded" type="typePersonne"/>
</xsd:sequence>
<xsd:attributeGroup ref="InfosMaj"/>
</xsd:complexType>
<xsd:complexType name="typeContactsLimité">
<xsd:complexContent>
<xsd:restriction base="typeContacts">
<xsd:sequence>
<xsd:element name="personne"maxOccurs="50" type="typePersonne"/>
</xsd:sequence>
<xsd:attributeGroup ref="InfosMaj"/>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
39
Extension of Simple types
Element with Simple Content (don’t contain other elements) and Attributes
Example:
<elevation units="feet">5440</elevation>
<xsd:element name="elevation">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:integer">
<xsd:attribute name="units" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
40
Extension of Complex Types
<xsd:complexType name="Publication">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
ISBN
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:gYear"/>
</xsd:sequence>
</xsd:complexType > Title
<xsd:complexType name="BookPublication">
<xsd:complexContent>
<xsd:extension base="Publication"> Author
<xsd:sequence> Date
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
Publisher
</xsd:complexType >
BookPublication Publication
41
Type Derivation and Attributes
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"
maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
<xsd:attributeGroup ref="BookAttributes"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:attributeGroup name="BookAttributes">
<xsd:attribute name="Category" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="autobiography"/>
<xsd:enumeration value="non-fiction"/>
<xsd:enumeration value="fiction"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="InStock" type="xsd:boolean" default="false"/>
<xsd:attribute name="Reviewer" type="xsd:string" default=" "/>
</xsd:attributeGroup>
42
Controlling Type Derivations
Example: Fixing a Facet Value
<xsd:simpleType name= "ClassSize">
<xsd:restriction base="xsd:nonNegativeInteger">
<xsd:minInclusive value="10" fixed="true"/>
<xsd:maxInclusive value="60"/>
</xsd:restriction>
</xsd:simpleType>
43
Prohibiting Derivations
Sometimes we may want to create a type and disallow all
derivations of it, or just disallow extension derivations, or
disallow restriction derivations.
– Rationale: "For example, I may create a complexType and make it publicly
available for others to use. However, I don't want them to extend it with their
proprietary extensions or subset it to remove, say, copyright information." (Jon
Cleaver)
44
ar y
S u mm
AB rief
45
Declaring Attributes
xsd:string required
optional
xsd:integer
prohibited
xsd:boolean
...
2 <xsd:attribute name="name" use="how-its-used" value="value">
<xsd:simpleType>
<xsd:restriction base="simple-type">
<xsd:facet value="value"/>
…
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
46
Summary of Declaring Elements
<xsd:simpleType name="US-Flag-Colors>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="red"/>
<xsd:enumeration value="white"/>
<xsd:enumeration value="blue"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="flag" type="US-Flag-Colors"/>
An alternative formulation of the above flag example is to inline the simpleType definition:
<xsd:element name="flag">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="red"/>
<xsd:enumeration value="white"/>
<xsd:enumeration value="blue"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
47
Summary of Declaring Elements (cont.)
<xsd:element name="Person">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="FirstName" type="xsd:string"/>
<xsd:element name="Surname" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
An alternate formulation of the above Person example is to create a named complexType and then use that type:
<xsd:complexType name="PersonType">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="FirstName" type="xsd:string"/>
<xsd:element name="Surname" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="Person" type="PersonType"/>
48
Summary of Declaring Elements (cont.)
<xsd:complexType name="Publication">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:gYear"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="BookPublication">
<xsd:complexContent>
<xsd:extension base="Publication" >
<xsd:sequence>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="Book" type="BookPublication"/>
49
Summary of Declaring Elements (cont.)
<xsd:complexType name="Publication">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:gYear"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name= "SingleAuthorPublication">
<xsd:complexContent>
<xsd:restriction base="Publication">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:gYear"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="Catalogue" type="SingleAuthorPublication"/>
50
Summary of Declaring Elements
<xsd:element name="apple">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="variety" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
51
Advanced XML Schema
Conception
52
Element Substitution
Oftentimes in daily conversation there are several ways to
express something.
– In Boston we use the words "T" and "subway" interchangeably. For example, "we took the
T into town", or "we took the subway into town".
• Thus, "T" and "subway" are substitutable. Which one is used may depend upon what part of the
state you live in, what mood you're in, or any number of factors.
53
Element Substitution (cont…)
<transportation>
Instance doc:
<subway>Red Line</subway>
</transportation>
Alternative
<transportation>
instance doc
<T>Red Line</T>
(substitute T </transportation>
for subway):
54
Element Substitution with Derived Types
PublicationType
BookType MagazineType
55
Uniqueness & Keys
DTDs provide the ID attribute datatype for uniqueness (i.e., an
ID value must be unique throughout the entire document, and
the XML parser enforces this).
XML Schema has much enhanced uniqueness capabilities:
– enables you to define element content to be unique.
– enables you to define non-ID attributes to be unique.
– enables you to define a combination of element content and attributes to be
unique.
– enables you to distinguish between unique versus key.
– enables you to declare the range of the document over which something is
unique
56
unique vs key
Example:
In the BookStore we should be able to express that each Book's
ISBN element is unique. Further, let's make the ISBN elements keys
(i.e., both unique and required to exist).
57
Example 1
58
Example 2
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="[Link]
targetNamespace="[Link]
xmlns="[Link]
xmlns:bk="[Link]
elementFormDefault="qualified">
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/> Note: ISBN
<xsd:element name="ISBN" type="xsd:string" minOccurs="0"/>
<xsd:element name="Publisher" type="xsd:string"/> is optional
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType> Require
<xsd:unique name="UNIQ">
<xsd:selector xpath="bk:Book"/> every ISBN
<xsd:field xpath="bk:ISBN"/>
</xsd:unique> be unique.
</xsd:element>
</xsd:schema>
59
Referencing a key
60
More XML Schema
61
Anonymous types and Named Types
<?xml version="1.0"?>
<Library xmlns:xsi="[Link]
xsi:schemaLocation= Validating against
"[Link]
[Link] two schemas
[Link]
[Link]">
<Books>
<Book xmlns="[Link]
<Title>My Life and Times</Title>
<Author>Paul McCartney</Author>
The <Book> elements are
<Date>1998</Date>
<ISBN>1-56592-235-2</ISBN>
defined in [Link], and
<Publisher>Macmillan Publishing</Publisher>
</Book>
the <Employee> elements
<Book xmlns="[Link]
<Title>Illusions The Adventures of a Reluctant Messiah</Title>
are defined in [Link].
<Author>Richard Bach</Author>
<Date>1977</Date>
<ISBN>0-440-34319-4</ISBN>
<Publisher>Dell Publishing Co.</Publisher>
</Book>
<Book xmlns="[Link]
1. A schema validator will
<Title>The First and Last Freedom</Title>
<Author>J. Krishnamurti</Author>
validate each Book element
<Date>1954</Date>
<ISBN>0-06-064831-7</ISBN>
against [Link].
<Publisher>Harper & Row</Publisher>
</Book>
2. It will validate each
</Books>
<Employees>
Employee element against
<Employee xmlns="[Link]
<Name>John Doe</Name>
[Link].
<SSN>123-45-6789</SSN>
</Employee>
<Employee xmlns="[Link]
<Name>Sally Smith</Name>
<SSN>000-11-2345</SSN>
</Employee>
</Employees>
</Library>
[Link]
63
Assembling a Schema from Multiple Schema Documents
[Link] [Link]
<xsd:schema …>
<xsd:include schemaLocation="[Link]"/>
<xsd:include schemaLocation="[Link]"/>
…
</xsd:schema>
[Link]
64
Assembling a Schema from Multiple Schema Documents with
Different Namespaces
The import element allows you to access elements and types in
a different namespace
Namespace Namespace
A B
[Link] [Link]
<xsd:schema …>
<xsd:import namespace="A"
schemaLocation="[Link]"/>
<xsd:import namespace="B"
schemaLocation="[Link]"/>
…
</xsd:schema>
[Link]
65
Assembling a Schema from Multiple Schema Documents with Different
Namespaces
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="[Link]
targetNamespace="[Link]
xmlns:nikon="[Link]
xmlns:olympus="[Link]
xmlns:pentax="[Link]
elementFormDefault="qualified">
These import
<xsd:import namespace="[Link]
schemaLocation="[Link]"/> elements give
<xsd:import namespace="[Link] us access to
schemaLocation="[Link]"/> the components
<xsd:import namespace="[Link] in these other Use of the
schemaLocation="[Link]"/> schemas. body_type
<xsd:element name="camera">
that is
<xsd:complexType>
<xsd:sequence> defined
<xsd:element name="body" type="nikon:body_type"/> in the
<xsd:element name="lens" type="olympus:lens_type"/> Nikon
<xsd:element name="manual_adapter" type="pentax:manual_adapter_type"/> namespace
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:schema>
[Link]
66
Redefining a Type from the Included Schema
67
Redefining a Type from the Included Schema
<xsd:complexType name="BookType">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
[Link] <xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:redefine schemaLocation="[Link]">
<xsd:complexType name="BookType">
<xsd:complexContent> Redefining BookType to
<xsd:extension base="BookType">
<xsd:sequence> also have a Summary
[Link] <xsd:element name="Summary" type="xsd:string"/> element.
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:redefine>
68
Annotating Schemas
The <annotation> element is used for documenting the
schema, both for humans and for programs.
– Use <documentation> for providing a comment to humans
– Use <appinfo> for providing a comment to programs
• The content is any well-formed XML
<xsd:annotation>
<xsd: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).
</xsd:documentation>
<xsd:appinfo>
<assert test="A > B">A should be greater than B</assert>
</xsd:appinfo>
<xsd:/annotation>
69
Conclusions
70
Conclusions (1)
Namespaces, data types…make XML Shema very powerful
However, it is not "all powerful". There are many constraints that it cannot
express. Here are some examples:
• Ensure that the value of the aircraft <Elevation> element is greater than
the value of the obstacle <Height> element.
• Ensure that:
W if the value of the attribute, mode, is "air", then the value of the
element, <Transportation>, is either airplane or hot-air balloon
W if mode="water" then <Transportation> is either boat or hovercraft
W if mode="ground" then <Transportation> is either car or bicycle.
• Ensure that the value of the <PaymentReceived> is equal to the value of
<PaymentDue>, where these elements are in separate documents!
To check all our constraints we will need to supplement XML Schemas with
another tool.
71
Conclusions (2)
XSLT/XPath
– The first approach is to supplement the XSD document with a
stylesheet
Schematron
– The second approach is to embed the additional constraints within
<appinfo> elements in the XSD document. Then, a tool
(Schematron) will extract and process those constraints.
72
References
73
XML Schema Validators
Command Line Only
– XSV by Henry Thompson
• [Link]
Has a Programmatic API
– xerces by Apache
• [Link]
– IBM Schema Quality Checker (Note: this tool is only used to check your schema. It cannot be used to
validate an instance document against a schema.)
• [Link]
– MSXML4.0
• [Link]
GUI Oriented
– XML Spy
• [Link]
– Turbo XML
• [Link]
74
References
[Link] (Primer)
[Link] (Structures)
[Link] (Datatypes)
1. XML Schemas
2. …More on XML Schemas
3. XML Schemas Reference Manual
75
XML & Data Bases????
XML et les technologies qui lui sont associées est une sorte de
SGBD
– Le stockage (les documents XML),
– Des Schémas (DTDs, XML Schemas, RELAX NG, etc.),
– Des langages de requêtes (XQuery, XPath, XQL, etc.),
– Des interfaces de programmation (SAX, DOM, JDOM)
Mais:
– Un stockage efficace,
– Les index,
– La sécurité,
– La gestion des transactions,
– L’accès multi-utilisateurs,
– Les déclencheurs (triggers),
– Les requêtes sur plusieurs documents, etc.
76