Upload
curtis-cunningham
View
229
Download
1
Embed Size (px)
Citation preview
Ceng 520
XML Schemas
10.03.200520.03.2003
IntroductionXML Schemas
2
Part 0: Introduction
Why XML Schema?
10.03.200520.03.2003
IntroductionXML Schemas
3
Purpose of XML schemas
1-The structure of instance documents– defines elements that can appear in a document,
– defines attributes that can appear in a document,
– defines which elements are child elements,
– defines the sequence in which the child elements can appear,
– defines the number of child elements,
– defines whether an element is empty or can include text,
– defines default values for elements and attributes.
Describe document class’s logical structure.
defines:
2-The datatype of each element/attribute
Same Purpose is achieved with DTDSame Purpose is achieved with DTD
10.03.200520.03.2003
IntroductionXML Schemas
4
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.
10.03.200520.03.2003
IntroductionXML Schemas
5
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
10.03.200520.03.2003
IntroductionXML Schemas
6
Example: BookStore.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)>
ATTLIST
ELEMENT
ID
#PCDATA
NMTOKEN
ENTITY
CDATA
BookStore
BookTitle
Author
DateISBN
Publisher
10.03.200520.03.2003
IntroductionXML Schemas
7
Example: BookStore.xsd
<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" 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:date"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/></xsd:schema>
<!ELEMENT Title (#PCDATA)><!ELEMENT Author (#PCDATA)><!ELEMENT Date (#PCDATA)><!ELEMENT ISBN (#PCDATA)><!ELEMENT Publisher (#PCDATA)>
<!ELEMENT Book (Title, Author, Date, ISBN, Publisher)>
<!ELEMENT BookStore (Book)+>
10.03.200520.03.2003
IntroductionXML Schemas
8
Let’s get started!
Data Types Type Derivation Name Spaces Advanced XML SchemaConception More XML Schema!!!!! Conclusions References
10.03.200520.03.2003
IntroductionXML Schemas
9
Part 1: Name Spaces
10.03.200520.03.2003
IntroductionXML Schemas
10
Name spaces
elementcomplexType
schema
sequence
http://www.w3.org/2001/XMLSchema
string
integer
boolean
BookStore
BookTitle
Author
Date
ISBNPublisher
http://www.books.org (targetNamespace)
This is the vocabulary that XML Schemas provide to define yournew vocabulary
One difference between XML Schemas and DTDs is that the XML Schema vocabularyis associated with a name (namespace). Likewise, the new vocabulary that you define must be associated with a name (namespace).
10.03.200520.03.2003
IntroductionXML Schemas
11
Example: BookStore.xsd<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org" xmlns="http://www.books.org" 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>
Elements and data types:
• schema• element• complex Type• sequence• string • boolean• integer • …etc
Come from the namespace:
http://www.w3.org/2001/XMLSchema
An XML schema has « schema » as root element
xsd = Xml-Schema Definition
10.03.200520.03.2003
IntroductionXML Schemas
12
Target Name Space<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org" xmlns="http://www.books.org" 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>
Elements defined by this schema
- BookStore - Book - Title - Author - Date - ISBN - Publisher
are to go in this space name
10.03.200520.03.2003
IntroductionXML Schemas
13
Default Name space
This is referencing a Book element declaration.Since there is no namespace qualifier it is referencing the Book element in the defaultNamespace.
The default namespace ishttp://www.books.orgwhich is the targetNamespace!
<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" 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>
10.03.200520.03.2003
IntroductionXML Schemas
14
<?xml version="1.0"?><schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns:bk="http://www.books.org" elementFormDefault="qualified"> <element name="BookStore"> <complexType> <sequence> <element ref="bk:Book" maxOccurs="unbounded"/> </sequence> </complexType> </element> <element name="Book"> <complexType> <sequence> <element ref="bk:Title"/> <element ref="bk:Author"/> <element ref="bk:Date"/> <element ref="bk:ISBN"/> <element ref="bk:Publisher"/> </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>
Note that http://…/XMLSchemais the defaultnamespace.Consequently, thereare no namespacequalifiers on - schema - element - complexType - sequence - string
Default XMLSchema, Qualify targetNamespace
10.03.200520.03.2003
IntroductionXML Schemas
15
Name Space: Conclusion
BookStore.xml BookStore.xsd
targetNamespace="http://www.books.org"schemaLocation="http://www.books.org BookStore.xsd"
- defines elements in namespace http://www.books.org
- uses elements from namespace http://www.books.org
A schema defines a new vocabulary. Instance documents use that new vocabulary.
10.03.200520.03.2003
IntroductionXML Schemas
16
BookStore.xml BookStore.xsd XMLSchema.xsd(schema-for-schemas)
Validate that the xml documentconforms to the rules describedin BookStore.xsd
Validate that BookStore.xsd is a validschema document, i.e., it conformsto the rules described in theschema-for-schemas
Multiple levels of checking
10.03.200520.03.2003
IntroductionXML Schemas
17
Part 2: Data Types
10.03.200520.03.2003
IntroductionXML Schemas
18
Elements, Attributes and attribute group declaration (1)
Element Declaration:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema">
<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="http://www.w3.org/2000/10/XMLSchema">
<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>
10.03.200520.03.2003
IntroductionXML Schemas
20
<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>
Element Group
10.03.200520.03.2003
IntroductionXML Schemas
21
Attribute group
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema">
<xsd:element name="contacts" type="typeContacts"/>
<xsd:element name="remarque" type="xsd:string"/>
<xsd:complexType name="typeContacts">
<!—content declaration here-->
<xsd:attributeGroup ref="InfosMaj"/>
</xsd:complexType>
<xsd:attributeGroup name="InfosMaj">
<xsd:attribute name="maj" type="xsd:date"/>
<xsd:attribute name="auteur" type="xsd:string"/>
</xsd:attributeGroup>
</xsd:schema>
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema">
<xsd:element name="contacts" type="typeContacts"/>
<xsd:element name="remarque" type="xsd:string"/>
<xsd:complexType name="typeContacts">
<!—content declaration here-->
<xsd:attributeGroup ref="InfosMaj"/>
</xsd:complexType>
<xsd:attributeGroup name="InfosMaj">
<xsd:attribute name="maj" type="xsd:date"/>
<xsd:attribute name="auteur" type="xsd:string"/>
</xsd:attributeGroup>
</xsd:schema>
10.03.200520.03.2003
IntroductionXML Schemas
22
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!
10.03.200520.03.2003
IntroductionXML Schemas
23
Simple Types: Predefined Types
Complex Type
String Boolean Entity ID IDREF
negativeInteger
NonPositiveInteger
positiveinteger
unsignedByte
unsignedShort
unsignedInt
unsignedLong
NonNegativeInteger
byte
short
int
Long
Integer
Decimal Binary QName Float Time duration
date month year century
timeperiod time
reccuringduration
Simple Type
Data Types
Similar to programming language
10.03.200520.03.2003
IntroductionXML Schemas
24
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"/>
10.03.200520.03.2003
IntroductionXML Schemas
25
Complex types: Constructors and occurrence indicators
Sequence and choice
<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.binary.org" xmlns="http://www.binary.org" 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"/> </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>
<!ELEMENT life ((work, eat)*, (work | play), sleep)* >DTD:
XML Schema:
10.03.200520.03.2003
IntroductionXML Schemas
26
<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <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>
XML Schema:
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).
Expressing Any Order
10.03.200520.03.2003
IntroductionXML Schemas
27
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"/>
DTD
*
+
?
MinOccursXML SCHEMA
MaxOccurs0 Unbounded
1 (default value) Unbounded
0 1 (default value)
MinOccurs And MaxOccurs
10.03.200520.03.2003
IntroductionXML Schemas
28
Empty Complex Type
<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.photography.org" xmlns="http://www.photography.org" elementFormDefault="qualified"> <xsd:element name="gallery"> <xsd:complexType> <xsd:sequence> <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>
<image href="http://www.xfront.com/InSubway.gif"/>
Schema:
InstanceDoc:
<!ELEMENT image EMPTY><!ATTLIST image href CDATA #REQUIRED>
DTD:
10.03.200520.03.2003
IntroductionXML Schemas
29
Type Attribute or ComplexType child element
An element declaration can have a type attribute, or a complexType child element, but it cannot have both a type attribute and a complexType child element.
<xsd:element name="A" type="foo"> <xsd:complexType> … </xsd:complexType></xsd:element>
ERROR
10.03.200520.03.2003
IntroductionXML Schemas
30
<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" 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>
Referring Elements Declarations
10.03.200520.03.2003
IntroductionXML Schemas
31
Inlining Element Declarations
<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="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="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:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>
Note that we have movedall the element declarationsinline, and we are nolonger referring to theelement declarations.This results in a much more compact schema!
10.03.200520.03.2003
IntroductionXML Schemas
32
Part 3:Type Derivation
10.03.200520.03.2003
IntroductionXML Schemas
33
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
10.03.200520.03.2003
IntroductionXML Schemas
34
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.
10.03.200520.03.2003
IntroductionXML Schemas
35
<xsd:simpleType name= "name"> <xsd:restriction base= "xsd:source"> <xsd:facet value= "value"/> <xsd:facet value= "value"/> … </xsd:restriction></xsd:simpleType>
Facets: - length - minlength - maxlength - pattern - enumeration - minInclusive - maxInclusive - minExclusive - maxExclusive ...
Sources: - string - boolean - number - float - double - duration - dateTime - time ...
General Form of restricting simple types by Specifying Facet Values
10.03.200520.03.2003
IntroductionXML Schemas
36
Enumeration And Pattern facets
<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>
This creates a new type called US-Flag-Colors.An element declared to be of this typemust have either the value red, or white, or blue.
<xsd:simpleType name="Emailadress">
<xsd:restriction base="xsd:string">
<xsd:pattern value="(.)+@(.)+"/>
</xsd:restriction>
</xsd:simpleType>
This creates a new type called Emailadress.An element declared to be of this typemust have the form string@string.
10.03.200520.03.2003
IntroductionXML Schemas
37
Restriction of Complex Types
<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>
<xsd:complexType name="typeContacts">
<xsd:sequence>
<xsd:element name="personne" maxOccurs="unbounded" type="typePersonne"/>
</xsd:sequence>
<xsd:attributeGroup ref="InfosMaj"/>
</xsd:complexType>
10.03.200520.03.2003
IntroductionXML Schemas
38
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>
10.03.200520.03.2003
IntroductionXML Schemas
39
Extension of Complex Types
<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 >
Title
AuthorDate
Publication
ISBN
Publisher
BookPublication
Add elements or attributes to a complex type
10.03.200520.03.2003
IntroductionXML Schemas
40
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"> <xsd:minInclusive value="15"/> <xsd:maxInclusive value="60"/> </xsd:restriction></xsd:simpleType>
Error! Cannotchange the valueof a fixed facet!
Controlling Type Derivations
10.03.200520.03.2003
IntroductionXML Schemas
41
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
10.03.200520.03.2003
IntroductionXML Schemas
42
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>
10.03.200520.03.2003
IntroductionXML Schemas
43
A Brief Summary
10.03.200520.03.2003
IntroductionXML Schemas
44
<xsd:attribute name="name" type="simple-type" use="how-its-used" value="value"/>
requiredoptionalprohibited
xsd:stringxsd:integerxsd:boolean...
<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>
1
2
Declaring Attributes
10.03.200520.03.2003
IntroductionXML Schemas
45
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>
Summary of Declaring Elements
10.03.200520.03.2003
IntroductionXML Schemas
46
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"/>
10.03.200520.03.2003
IntroductionXML Schemas
47
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"/>
3. Element Contains a complexType that is an Extension of another complexType
10.03.200520.03.2003
IntroductionXML Schemas
48
<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"/>
4. Element Contains a complexType that is a Restriction of another complexType
Summary of Declaring Elements (cont.)
10.03.200520.03.2003
IntroductionXML Schemas
49
<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>
5. Element Contains Simple Content and Attributes
Summary of Declaring Elements
10.03.200520.03.2003
IntroductionXML Schemas
50
Part 4:Advanced XML Schema
Conception
10.03.200520.03.2003
IntroductionXML Schemas
51
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.
10.03.200520.03.2003
IntroductionXML Schemas
52
Element Substitution (cont…)
<xsd:element name="subway" type="xsd:string"/> <xsd:element name="T" substitutionGroup="subway" type="xsd:string"/> <xsd:element name="transportation"> <xsd:complexType> <xsd:sequence> <xsd:element ref="subway"/> </xsd:sequence> </xsd:complexType> </xsd:element>
Schema:
Instance doc:
Alternative instance doc(substitute Tfor subway):
This example shows the <subway> element being substituted with the <T> element.
<transportation> <subway>Red Line</subway> </transportation>
<transportation> <T>Red Line</T> </transportation>
10.03.200520.03.2003
IntroductionXML Schemas
53
An element may wish to block other elements from substituting with it. This is achieved by adding a block attribute.
<xsd:element name="…" type="…" block="substitution"/>
1. Transitive: if element A can substitute for element B, and element B can substitute for element C, then element A can substitute for element C.
A --> B --> C then A --> C
2. Non-symmetric: if element A can substitute for element B, it is not the case that element B can substitute for element A.
Note: Element Substitution is:
Blocking Element Substitution
10.03.200520.03.2003
IntroductionXML Schemas
54
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"/> <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:element>
<xsd:element name="Book" type="BookPublication" maxOccurs="unbounded"/> … … … …
<xsd:complexType name="BookPublication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType>
AnonymousType
AnonymousType
NamedType
NamedType
Named Type can
be reused by otherelements
Named Type can
be reused by otherelements
10.03.200520.03.2003
IntroductionXML Schemas
55
<?xml version="1.0"?><Library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.book.org Book.xsd http://www.employee.org Employee.xsd"> <Books> <Book xmlns="http://www.book.org"> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <ISBN>1-56592-235-2</ISBN> <Publisher>Macmillan Publishing</Publisher> </Book> <Book xmlns="http://www.book.org"> <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 xmlns="http://www.book.org"> <Title>The First and Last Freedom</Title> <Author>J. Krishnamurti</Author> <Date>1954</Date> <ISBN>0-06-064831-7</ISBN> <Publisher>Harper & Row</Publisher> </Book> </Books> <Employees> <Employee xmlns="http://www.employee.org"> <Name>John Doe</Name> <SSN>123-45-6789</SSN> </Employee> <Employee xmlns="http://www.employee.org"> <Name>Sally Smith</Name> <SSN>000-11-2345</SSN> </Employee> </Employees></Library>
Library.xml
Validating againsttwo schemas
The <Book> elements aredefined in Book.xsd, andthe <Employee> elementsare defined in Employee.xsd.
1. A schema validator will validate each Book elementagainst Book.xsd. 2. It will validate each Employee element against Employee.xsd.
Assembling an Instance Document from Multiple Schema Documents
10.03.200520.03.2003
IntroductionXML Schemas
56
<xsd:schema …> <xsd:include schemaLocation="LibraryBook.xsd"/> <xsd:include schemaLocation="LibraryEmployee.xsd"/> …</xsd:schema>
<xsd:schema …> <xsd:include schemaLocation="LibraryBook.xsd"/> <xsd:include schemaLocation="LibraryEmployee.xsd"/> …</xsd:schema>
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
LibraryBook.xsd LibraryEmployee.xsd
Library.xsd
10.03.200520.03.2003
IntroductionXML Schemas
57
Assembling a Schema from Multiple Schema Documents with Different NamespacesThe import element allows you to access elements and types in
a different namespace
NamespaceA
NamespaceA
A.xsd
NamespaceB
NamespaceB
B.xsd
C.xsd
<xsd:schema …> <xsd:import namespace="A" schemaLocation="A.xsd"/> <xsd:import namespace="B" schemaLocation="B.xsd"/> …</xsd:schema>
<xsd:schema …> <xsd:import namespace="A" schemaLocation="A.xsd"/> <xsd:import namespace="B" schemaLocation="B.xsd"/> …</xsd:schema>
10.03.200520.03.2003
IntroductionXML Schemas
58
<?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>
Camera.xsd
These importelements giveus access tothe componentsin these otherschemas.
Use of thebody_typethat isdefinedin the Nikonnamespace
Assembling a Schema from Multiple Schema Documents with Different Namespaces
10.03.200520.03.2003
IntroductionXML Schemas
59
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)
10.03.200520.03.2003
IntroductionXML Schemas
60
<xsd:complexType name="BookType"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence></xsd:complexType>
LibraryBook.xsd
<xsd:redefine schemaLocation="LibraryBook.xsd"> <xsd:complexType name="BookType"> <xsd:complexContent> <xsd:extension base="BookType"> <xsd:sequence> <xsd:element name="Summary" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType></xsd:redefine>
Library.xsd
Redefining BookType toalso have a Summaryelement.
Redefining a Type from the Included Schema
10.03.200520.03.2003
IntroductionXML Schemas
61
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 > B">A should be greater than B</assert> </xsd:appinfo><xsd:/annotation>
10.03.200520.03.2003
IntroductionXML Schemas
62
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.
10.03.200520.03.2003
IntroductionXML Schemas
63
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).
10.03.200520.03.2003
IntroductionXML Schemas
64
<xsd:element name="BookStore"> ... <xsd:key name="PK"> <xsd:selector xpath="bk:Book"/> <xsd:field xpath="bk:ISBN"/> </xsd:key> </xsd:element>
"Within <BookStore> we define a key, called PK. Select each <Book>, andwithin each <Book> the ISBN element isa key."
In other words, within <BookStore>each <Book> must have an <ISBN> andit must be unique.
Example 1
10.03.200520.03.2003
IntroductionXML Schemas
65
Example 2
<?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="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" minOccurs="0"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:unique name="UNIQ"> <xsd:selector xpath="bk:Book"/> <xsd:field xpath="bk:ISBN"/> </xsd:unique> </xsd:element></xsd:schema>
Note: ISBNis optional
Requireevery ISBNbe unique.
10.03.200520.03.2003
IntroductionXML Schemas
66
Part 5:Conclusions
10.03.200520.03.2003
IntroductionXML Schemas
67
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: if the value of the attribute, mode, is "air", then the value of the
element, <Transportation>, is either airplane or hot-air balloon
if mode="water" then <Transportation> is either boat or hovercraft
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.
10.03.200520.03.2003
IntroductionXML Schemas
68
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.
10.03.200520.03.2003
IntroductionXML Schemas
69
Part 6:References
10.03.200520.03.2003
IntroductionXML Schemas
70
XML Schema Validators Command Line Only
– XSV by Henry Thompson• ftp://ftp.cogsci.ed.ac.uk/pub/XSV/XSV12.EXE
Has a Programmatic API
– xerces by Apache• http://www.apache.org/xerces-j/index.html
– 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.)
• http://www.alphaworks.ibm.com/tech/xmlsqc
– MSXML4.0• http://www.microsoft.com
GUI Oriented
– XML Spy• http://www.xmlspy.com
– Turbo XML• http://www.extensibility.com
10.03.200520.03.2003
IntroductionXML Schemas
71
References
http://www.w3.org/TR/xmlschema-0/ (Primer) http://www.w3.org/TR/xmlschema-1/ (Structures)http://www.w3.org/TR/xmlschema-2/ (Datatypes)