0% found this document useful (0 votes)
12 views76 pages

Understanding XML Basics and Schema

The document provides an overview of XML, its basic principles, and the evolution of markup languages. It explains the structure and validation of XML documents, the advantages of XML Schemas over DTDs, and the concept of namespaces in XML. Additionally, it includes examples of XML Schema definitions and their application in defining structured data.

Uploaded by

Belkacem Iskhar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views76 pages

Understanding XML Basics and Schema

The document provides an overview of XML, its basic principles, and the evolution of markup languages. It explains the structure and validation of XML documents, the advantages of XML Schemas over DTDs, and the concept of namespaces in XML. Additionally, it includes examples of XML Schema definitions and their application in defining structured data.

Uploaded by

Belkacem Iskhar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIL 2006

XML
Basic Notions

A. BOUKOTTAYA
Evolution of Mark-Up languages

GenCode Commitee (1967)


GML (1969)

Text Processing SGML


Systems (Standard Generalized
Mark-Up Language) (1986)

Draft with Markup Final printed copy

From Webster’s New Collegiate Dictionary (1959), p.1159

The W3 Consortium The Word Wide Web (1990)


XML Mark-Up language HTML ( Hypertext Mark-Up
(1998) Language)

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

Mandatory heading From: C. Vanoirbeek


To: A. Ballim

Flexible content { Is it a good idea to introduce basic concepts about SGML?

Optional signature { CV

7
XML basic principles (Structure Definition)
 Describing document classes
– Through DTD (Document Type Definition)

<!DOCTYPE Message >


<!ELEMENT Message (Subject, Sender, Receiver+, Body, Sign?) >
<!ELEMENT Subject (#PCDATA) >
<!ELEMENT Sender (#PCDATA) >
<!ELEMENT Receiver (#PCDATA) >
<!ELEMENT Body (Parag*) >
<!ELEMENT Parag (#PCDATA) >
<!ELEMENT Sign (#PCDATA) >

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

RDF RDF Schema


OWL

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

•XML Schemas are a tremendous advancement over DTDs:


–Enhanced data types
•44+ versus 10
•Can create your own data types
–Written in the same syntax as instance documents
•You don't have to learn another language
•You can use your XML editor to edit your Schema files
•You can use your XML parser to parse your Schema files
•You can manipulate your Schema with the XML DOM
•You can transform your Schema with XSLT
–XML Schemas are Object-oriented (Extensible)
•Can extend or restrict a type (derive new type definitions on the basis of old ones)
•XML Schemas are extensible, just like XML, because they are written in XML.
•With an extensible Schema definition you can:
–Reuse your Schema in other Schemas
–Create your own data types derived from standard types
–Reference multiple schemas from the same document

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

[Link] [Link] (targetNamespace)

complexType
element BookStore
sequence Author
schema Book
boolean Title
string Publisher ISBN
integer Date

This is the vocabulary that


XML Schemas provide to define your
new vocabulary

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]

A schema defines a new vocabulary. Instance documents use that


new vocabulary.

21
Multiple levels of checking

[Link] [Link] [Link]


(schema-for-schemas)

Validate that the xml document


conforms to the rules described Validate that [Link] is a valid
in [Link] schema document, i.e., it conforms
to the rules described in the
schema-for-schemas

22
Data Types

23
Elements, Attributes declaration
 Element 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"/>

<!—Types declaration here -->

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

Simple Type or Complex Type


– Use the simpleType element when the element does not contain attributes or child
elements.
– Use the complexType element when you want to define child elements and/or
attributes of an element.

 There are four kinds of complex elements:


– empty elements
– elements that contain only other elements
– elements that contain only text
– elements that contain both other elements and text
Note: Each of these elements may contain attributes as well!

25
Simple Types: Predefined Types

Data Types

Complex Type Simple Type

String Boolean Entity ID IDREF Decimal Binary QName Float Time duration reccuring
duration

Integer timeperiod time

NonPositiveInteger NonNegativeInteger Long date month year century

negativeInteger positiveinteger unsignedLong int

unsignedInt short

unsignedShort byte

unsignedByte

Similar to programming language

26
Simple type: examples

 <xsd:element name="lastname" type="xsd:string"/>


 <xsd:element name="age" type="xsd:integer"/>
 <xsd:element name="date born" type="xsd:date"/>
 <xsd:simpleType name="phonenumber">
<xsd:listitemType="xsd:unsignedByte"/>
</xsd:simpleType>
 <xsd:simpleType name="technicalmemo">
<xsd:unionmemberTypes="xsd:string phonenumber"/>
</xsd:simpleType>

27
Complex types: Constructors and occurrence indicators

 Sequence and choice

DTD: <!ELEMENT life ((work, eat)*, (work | play), sleep)* >

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

Š The default value for minOccurs is "1“


Š The default value for maxOccurs is "1"
<xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Title"/>

XML SCHEMA
DTD
MinOccurs MaxOccurs
* 0 Unbounded
+ 1 (default value) Unbounded
? 0 1 (default value)

30
Empty Complex Type

DTD: <!ELEMENT image EMPTY>


<!ATTLIST image href CDATA #REQUIRED>

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

Instance <image href="[Link]


Doc:

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)

<xsd:simpleType name= "EarthSurfaceElevation">


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

This creates a new datatype called 'EarthSurfaceElevation'.


Elements declared to be of this type can hold an integer.
However, the integer is restricted to have a value between
-1290 and 29035, inclusive.

36
General Form of restricting simple types by Specifying Facet Values

<xsd:simpleType name= "name">


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

</xsd:restriction>
</xsd:simpleType>
Facets:
- length Sources:
- minlength - string
- maxlength - boolean
- pattern - number
- enumeration - float
- minInclusive - double
- maxInclusive - duration
- minExclusive - dateTime
- maxExclusive - time
... ...
37
Enumeration And Pattern facets

<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

Add elements or attributes to a complex type

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

<xsd:simpleType name= "BostonIEEEClassSize">


<xsd:restriction base="ClassSize">
Error! Cannot
<xsd:minInclusive value="15"/>
change the value
<xsd:maxInclusive value="60"/>
of a fixed facet!
</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)

<xsd:complexType name="Publication" final="#all" Publication cannot be extended nor restricted

<xsd:complexType name="Publication" final="restriction" Publication cannot be restricted

<xsd:complexType name="Publication" final="extension" Publication cannot be extended

44
ar y
S u mm
AB rief

45
Declaring Attributes

1 <xsd:attribute name="name" type="simple-type" use="how-its-used" value="value"/>

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

1. Element with Simple Content.

Declaring an element using a built-in type:

<xsd:element name="numStudents" type="xsd:positiveInteger"/>

Declaring an element using a user-defined simpleType:

<xsd:simpleType name="US-Flag-Colors>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="red"/>
<xsd:enumeration value="white"/>
<xsd:enumeration value="blue"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="flag" type="US-Flag-Colors"/>

An alternative formulation of the above flag example is to inline the simpleType definition:

<xsd:element name="flag">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="red"/>
<xsd:enumeration value="white"/>
<xsd:enumeration value="blue"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
47
Summary of Declaring Elements (cont.)

2. Element Contains Child Elements

Defining the child elements inline:

<xsd:element name="Person">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="FirstName" type="xsd:string"/>
<xsd:element name="Surname" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

An alternate formulation of the above Person example is to create a named complexType and then use that type:

<xsd:complexType name="PersonType">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="FirstName" type="xsd:string"/>
<xsd:element name="Surname" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="Person" type="PersonType"/>

48
Summary of Declaring Elements (cont.)

3. Element Contains a complexType that is an Extension of another complexType

<xsd:complexType name="Publication">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd: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.)

4. Element Contains a complexType that is a Restriction of another complexType

<xsd:complexType name="Publication">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd: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

5. Element Contains Simple Content and Attributes

<xsd:element name="apple">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="variety" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>

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.

 We would like to be able to express this substitutability in XML


Schemas.
– That is, we would like to be able to declare in a schema an element called "subway", an
element called "T", and state that "T"may be substituted for "subway". Instance documents
can then use either <subway> or <T>, depending on their preference.

53
Element Substitution (cont…)

<xsd:element name="subway" type="xsd:string"/>


<xsd:element name="T" substitutionGroup="subway" type="xsd:string"/>
Schema: <xsd:element name="transportation">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="subway"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<transportation>
Instance doc:
<subway>Red Line</subway>
</transportation>

Alternative
<transportation>
instance doc
<T>Red Line</T>
(substitute T </transportation>
for subway):

This example shows the <subway> element being substituted with


the <T> element.

54
Element Substitution with Derived Types

PublicationType

BookType MagazineType

<xsd:element name="Publication" type="PublicationType"/>


<xsd:element name="Book" substitutionGroup="Publication" type="BookType"/>
<xsd:element name="Magazine" substitutionGroup="Publication" type="MagazineType"/>
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Publication" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

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

 Key: an element or attribute (or combination) which is


defined to be a key must:
– always be present (minOccurs must be greater than zero)
– be non-nillable (i.e., nillable="false")
– be unique
 Key implies unique, but unique does not imply 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

"Within <BookStore> we define a


<xsd:element name="BookStore"> key, called PK. Select each <Book>, and
... within each <Book> the ISBN element is
<xsd:key name="PK"> a key."
<xsd:selector xpath="bk:Book"/>
<xsd:field xpath="bk:ISBN"/> In other words, within <BookStore>
each <Book> must have an <ISBN> and
</xsd:key>
it must be unique.
</xsd:element>

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

 Recall that by declaring an element of type IDREF then that


element must reference an ID attribute, and an XML Parser
will verify that the IDREF value corresponds to a legitimate
ID value.
 Similarly, you can define a keyref which asserts, "the value
of this element must match the value of an element referred
to by this".

60
More XML Schema

61
Anonymous types and Named Types

<xsd:element name="Book" maxOccurs="unbounded">


<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/> Anonymous
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/> Type
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:element name="Book" type="BookPublication" maxOccurs="unbounded"/>


…………
Named
<xsd:complexType name="BookPublication"> Type
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:string"/>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
Named Type can
</xsd:sequence>
</xsd:complexType>
be reused by other
elements
62
Assembling an Instance Document from Multiple Schema
Documents

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

 The include element allows you to access components in other


schemas
– All the schemas you include must have the same namespace as your schema
(i.e., the schema that is doing the include)
– The net effect of include is as though you had typed all the definitions directly into
the containing schema

[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

The <redefine> element does the same thing as an


<include> element (i.e., it allows you to access
components in other schemas, provided they have the
same namespace), plus it enables you to redefine one
or more components (simpleType, complexType,
attributeGroup, or group)

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

 Note that annotations have no effect on schema validation

<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 &gt; 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)

Two Approaches to Extending XML


Schemas

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

 XML Technologies Course (by: Roger L. Costello)

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

You might also like