79
1 … more on XML Schemas

… more on XML Schemas

Embed Size (px)

DESCRIPTION

… more on XML Schemas. Name Conflicts. Whereas DTDs required every element to have a unique name, XML Schemas enable you to use the same name in multiple places . Where can the same name be used, and where will there be a name conflict? Here are the things to remember: - PowerPoint PPT Presentation

Citation preview

1

… more on XML Schemas

2

Name Conflicts

• Whereas DTDs required every element to have a unique name, XML Schemas enable you to use the same name in multiple places.

• Where can the same name be used, and where will there be a name conflict? Here are the things to remember:– Type definitions (complexType and simpleType) are placed in one symbol space. Element declarations are placed in

a second symbol space and attribute declarations are placed in a third symbol space.• Hence, you can have a type and an element and an attribute all with the same name!

– Each type definition creates a new symbol space

3

What's Legal?• Legal

– Element, attribute, type (complex or simple) with the same name– Same name in different Symbol Spaces*– Same name in different namespaces

• Illegal– Same name and same Symbol Space but different type*

• Legal– Same name and same Symbol Space and same type– Note: (*) there are exceptions due to (un)qualified locals.

4

<xsd:element name="foo"> <xsd:complexType> <xsd:sequence> <xsd:element name="bar" type="xsd:string"/> ... <xsd:element name="bar" type="xsd:string"/> </xsd:sequence> </xsd:complexType</xsd:element>

Same name, type, Symbol Space --> Legal

<xsd:element name="foo"> <xsd:complexType> <xsd:sequence> <xsd:element name="bar" type="xsd:string"/> ... <xsd:element name="bar" type="xsd:integer"/> </xsd:sequence> </xsd:complexType</xsd:element>

Same name, Symbol Space, different type --> Illegal

5<xsd:element name="BookOnCars"> <xsd:complexType> <xsd:sequence> <xsd:element name="Chapter"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Section" > <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> </xsd:sequence> </xsd:complexType></xsd:element><xsd:complexType name="Title"> <xsd:sequence> <xsd:element name="CarManufacturer" type="xsd:string"/> <xsd:element name="Year" type="year"/> </xsd:sequence></xsd:complexType><xsd:element name="Title" type="xsd:string"/><xsd:attribute name="Title" type="xsd:string"/>

ScopeTest.xsd (see example 22)

Global elementsymbol space

Global typesymbol space

BookOnCars

Title

Title

Global attributesymbol space

Title

6

anonymoussymbol space

Chapter

Title

anonymoussymbol space

Title

Section

anonymoussymbol space

Title

Titlesymbol space

CarManufacturer

Year

<xsd:element name="BookOnCars"> <xsd:complexType> <xsd:sequence> <xsd:element name="Chapter"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Section"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> </xsd:sequence> </xsd:complexType></xsd:element><xsd:complexType name="Title"> <xsd:sequence> <xsd:element name="CarManufacturer" type="xsd:string"/> <xsd:element name="Year" type="year"/> </xsd:sequence></xsd:complexType><xsd:element name="Title" type="xsd:string"/><xsd:attribute name="Title" type="xsd:string"/>

ScopeTest.xsd (see example 22)

7

But, but, but, ...

• what does this all mean in terms of the namespace that the schema document is defining???– i.e., the different Symbol Spaces are allowing multiple items with the same name. Is this

going to result in a lot of name collisions in the namespace?

8

BookOnCars

Chapter Section

TitleTitle

TitleTitle

TitleName collisions!!!

CarBooks Namespace?

9

Global/Local Elements and Namespaces

• Only global elements are in the namespace!

• Local elements are associated with the global elements.

• In our example, the only elements in the namespace are BookOnCars and Title (the globally-declared Title element)

• BookOnCars has two local elements associated with it - Chapter and Title.

– Chapter has two elements associated with it - Title and Section

• Section has one element associated with it - Title

10

BookOnCars

Title

CarBooks Namespace [1]

[1] Later we will see that the namespace also contains the global types and attributes.

11

Same Situation with Attributes

• Attributes in an XML document are in the same situation as the schema local elements– There can be many attributes with the same

name in an XML document; – Attributes are associated with elements which

are in the namespace. (See next slide for example)

12

<?xml version="1.0"?><Book xmlns="http://www.publishing.org/namespaces/Book"> <Chapter title="Intro to Photography"> <Section title="35mm Cameras"> <Body title="Using the Camera"> The secret to using a 35mm camera is … </Body> </Section> </Chapter></Book>

Book Namespace

Book

Section

Body

Chapter

Notice that there are multiple title attributes.If they were all in the namespace then there would be a 3-way name collision.

The namespace does not include the attributes.

[ On the other hand, one could argue that the attributesare in the namespace by virtue of the fact that theyare associated with elements which are in the namespace. ]

Default namespace declaration

13

But, but, but, ...

• In the instance documents we have been qualifying all elements, thus indicating that they are all in the namespace:

<?xml version="1.0"?><BookCatalogue xmlns =" http://www.publishing.org" ...> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>July, 1998</Date> <ISBN>94303-12021-43892</ISBN> <Publisher>McMillin Publishing</Publisher> </Book> ...</BookCatalogue>

Default name-space declarationasserts that allthese elementsare in thePublishing name-space.

14

Unqualified Local Elements - How?

• Local elements are not really in targetNamespace (rather, they are in it, but only by association with a global element which is in it). So how can we indicate to instance document creators that they should only qualify the global elements?– Answer: – elementFormDefault="unqualified“ // in xsd:schema

• Notes:– Global elements/attributes names must be qualified– Whether a specific local element/attribute name is qualified or not

is determined by schema designers by using form/elementFormDefault/attributeFormDefault

15

elementFormDefault

• In all of our examples thus far we have set the value of this schema attribute to "qualified". The "qualified" value means that in an instance document all element instances must be qualified.

• Alternatively, you can assign elementFormDefault the value "unqualified". The "unqualified" value means that in an instance document all local element instances must not be qualified.

16

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.publishing.org" xmlns="http://www.publishing.org" elementFormDefault="unqualified"> <xsd:element name="BookCatalogue"> <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“ form=“qualified” /> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>

(see example 23)

Notice thatthere isonly oneglobalelement inthe schema

17

<?xml version="1.0"?><b:BookCatalogue xmlns:b="http://www.publishing.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.publishing.org BookCatalogue.xsd"> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <b:ISBN>94303-12021-43892</b:ISBN> <Publisher>McMillin Publishing</Publisher> </Book> <Book> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <b:ISBN>0-440-34319-4</b:ISBN> <Publisher>Dell Publishing Co.</Publisher> </Book> <Book> <Title>The First and Last Freedom</Title> <Author>J. Krishnamurti</Author> <Date>1954</Date> <b:ISBN>0-06-064831-7</b:ISBN> <Publisher>Harper &amp; Row</Publisher> </Book></b:BookCatalogue>

Now we don’t use a default namespacedeclaration, and we qualify only the globalelement - BookCatalogue.

(see example 19)

18

What’s Validated?

• In the previous example, do the local, unqualified elements - Book, Title, Author, Date, ISBN, Publisher - get validated? Yes! Everything is validated just as before, when we used elementFormDefault="qualified"

19

Use qualified or unqualified?• Case 1: elementFormDefault="unqualified" and only the global elements are

qualified

– Pro: hides namespace complexity in the schema

– Con: if the schema is modified by making local declarations global then all instance documents are impacted; the user needs to keep track of which elements are global versus which elements are local.

• Case 2: element elementFormDefault="qualified" and thus all elements are qualified

– Pro: if the schema is modified by making local declarations global then the instance documents are not impacted; the user doesn’t need to keep track of which elements are global and which elements are local.

– Con: exposes namespace complexity to the instance documents: Users needs to care about which elements are both global and local in the target namespace.

20

Examples1. <xsd:schema … elementFormDefault="unqualified" >2. <xsd:element name="title" type="xsd:int"/>3. <xsd:element name="book">4. <xsd:complexType><xsd:choice>5. <xsd:element name="title" type="xsd:string"

form="qualified"/>6. <xsd:element ref="title"/> 7. <xsd:element name="title" type="xsd:long"/>8. </xsd:choice></xsd:complexType></xsd:element>9. </xsd:schema>Notes: 1. form attr can override elementFormDefault attr2. 5 and 7 are consistent (two locals with the same names (title) but distinct

targetNamespaces due to form=“qualified”)3. 6 and 7 are consistent (different target namespaces):4. 5 and 6 are inconsistent (one local, one global but same targetNamespace due to

form=“qualified” (but is consistent if 5 has type='xsd:int' ).

21

Type Substitutability (Polymorphism)

• As we saw earlier, substitutionGroup gives us "element substitutability", i.e., the ability to substitute one element for another. Now we will see how to achieve "type substitutability", i.e., the ability to substitute an element’s content model with another content model.

• Here’s how type substitutability works: A base type can be substituted by any derived type.– Example. Suppose that the Book type is derived from Publication. If we declare an element, Listing, to be of type Publication (the base type) then in the instance document Listing's content can be either a Publication or a Book (since Book is a Publication).

22

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.publishing.org" xmlns="http://www.publishing.org" elementFormDefault="unqualified"> <xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:year"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="Book"> <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="Catalogue"> <xsd:complexType> <xsd:sequence> <xsd:element name="Listing" minOccurs="0" maxOccurs="unbounded" type="Publication"/> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>

(see example 24)

Book extendsPublication

Listing is of typePublication (thebase type)

Publication isthe base type

Note this

23

<?xml version="1.0"?><cat:Catalogue xmlns:cat ="http://www.publishing.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.publishing.org BookCatalogue.xsd"> <Listing> <Title>Staying Young Forever</Title> <Author>Karin Granstrom Jordan, M.D.</Author> <Date>December, 1999</Date> </ Listing> < Listing xsi:type="cat:Book"> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </ Listing> < Listing xsi:type="cat:Book"> <Title>The First and Last Freedom</Title> <Author>J. Krishnamurti</Author> <Date>1954</Date> <ISBN>0-06-064831-7</ISBN> <Publisher>Harper &amp; Row</Publisher> </ Listing></cat:Catalogue>

BookCatalogue.xml (see example 24)

This Listing’scontent model isthe Publication type

This Listing’scontent model isthe Book type

This Listing’scontent model isthe Book type

24

<Listing xsi:type="cat:Book"> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher></Listing>

"The Listing element is declared to be of type Publication. Bookis derived from Publication. Therefore, Book is a Publication. Thus, thecontent of Listing can be a Book. However, to indicate that the content is not the source type, but rather a derived type, we need to specifythe derived type that is being used. The attribute 'type' comes from theXML Schema Instance (xsi) namespace."

Note in the schema that the Book type is a global type definition. Byqualifying Book (cat:Book) we are asserting that the Book type comes from the catalogue namespace.

25

Why is xsi:type Needed?

• Why, in an instance document, do we need to indicate the derived type being used? Couldn’t the schema validator figure out which type was being used?– Answer:

• Easier to implement a schema validator

• Good practice

– c.f: In OO Language like Java, a run time object also contain its actual type information.

26

Catalogue Namespace

Publication (T)

Book (T)

Catalogue (E)

T = TypeE = Element

A namespace consists of all of the global stuff in the schema - global elements, attributes and types!

27

Summary of the Contents of the Namespace that a Schema Creates

• What is in the namespace that a schema creates?– The namespace is comprised of only the global

stuff:• Global elements

• Global attributes

• Global complexTypes/SimpleTypes

• Global AttributeGroup/GroupDo Lab 13

28

block Attribute

• You may add an attribute, block, to either an element or a complexType definition. – If you add a block attribute to an element then

the content model of that element may not be replaced by a derived type

– If you add a block attribute to a complexType then that complexType’s content model may not be replaced by a derived type in any element which is declared to be of that complexType.

29

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

<Listing xsi:type="cat:Book"> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher></Listing>

Schema:

Instance doc:

The Publication type, and types derived from Publicationmay be substituted for the content model of Listing, e.g., Book may be used.

30 <xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:year"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="Book"> <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="Catalogue"> <xsd:complexType> <xsd:sequence> <xsd:element name="Listing" minOccurs="0" maxOccurs="unbounded" type="Publication" block="#all"/> </xsd:sequence> </xsd:complexType> </xsd:element>

<Listing xsi:type="cat:Book"> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher></Listing>

Schema:

Instance doc:

This prohibits the use of types derived from Publication from being used as the content model of Listing, i.e.., this is not allowed

31

<xsd:complexType name="Publication" block="#all"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:year"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="Book"> <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="Catalogue"> <xsd:complexType> <xsd:sequence> <xsd:element name="Listing" minOccurs="0" maxOccurs="unbounded" type="Publication"/> </xsd:sequence> </xsd:complexType> </xsd:element>

<Listing xsi:type="cat:Book"> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher></Listing>

Schema:

Instance doc:

This prohibits Publication’s content model from being replaced by a derived typein any element declared to be of Publication type, such as Listing, i.e., this is not allowed

32

Block Attribute• block="extension"

– Prohibits you from substituting a derived-by-extension type for an element's content

• block="restriction"– Prohibits you from substituting a derived-by-restriction type for an

element's content• block="#all"

– Prohibits you from substituting any derived type for an element's content• block="substitution"

– This prohibits element substitution

• possible values: (#all | listOf(extension |restriction | substituion)) // for element/@block; no 'substitution' for complexType

33

Abstract Elements

• You can declare an element to be abstract– Example. <xsd:element name="Publication" type="PublicationType" abstract="true"/>

• An abstract element is a template/placeholder element:

– If an element is declared abstract then in an XML instance document that element may not appear.

• Example. The <Publication> element shown above may not appear in an instance document.

– However, elements that are members of a substitutionGroup whose head is an abstract element may appear in its place.

34 <xsd:complexType name="PublicationType"> <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:year"/> </xsd:sequence> </xsd:complexType> <xsd:element name="Publication" type="PublicationType" abstract="true"/> <xsd:element name="Book" substitutionGroup="Publication"> <xsd:complexType> <xsd:complexContent> <xsd:extension base="PublicationType"> <xsd:sequence> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> <xsd:element name="Magazine" substitutionGroup="Publication"> <xsd:complexType> <xsd:complexContent> <xsd:restriction base="PublicationType"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string" minOccurs="0" maxOccurs="0"/> <xsd:element name="Date" type="xsd:year"/> </xsd:sequence> </xsd:restriction> </xsd:complexContent> </xsd:complexType> </xsd:element> <xsd:element name="Catalogue"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Publication" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>

(see example 25)

Since the Publicationelement is abstract,only substitutionGroup’edelements can appearas children of Catalogue.

The Book andMagazine elementsare substitutionGroup'ed to the Publication element.

35

<?xml version="1.0"?><Catalogue xmlns="http://www.publishing.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.publishing.org Catalogue.xsd"> <Magazine> <Title>Natural Health</Title> <Date>1999</Date> </Magazine> <Book> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </Book> <Book> <Title>The First and Last Freedom</Title> <Author>J. Krishnamurti</Author> <Date>1954</Date> <ISBN>0-06-064831-7</ISBN> <Publisher>Harper &amp; Row</Publisher> </Book></Catalogue>

(see example 25)

An XML Instance Document Conforming to Catalogue.xsd

36

Abstract complexType

• You can declare a complexType to be abstract– Example. <xsd:complexType name="PublicationType" abstract="true"/>

• An abstract complexType is a template/placeholder type:

– If an element is declared to be a type that is abstract then in an XML instance document the content model of that element may not be that of the abstract type.

• Example. An element declared to be of type PublicationType (shown above) may not have that type’s content model.

– However, complexType’s that are derived from the abstract type may substitute for the abstract type.

37

<xsd:complexType name="PublicationType" abstract="true"> <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:year"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="Book"> <xsd:complexContent> <xsd:extension base="PublicationType"> <xsd:sequence> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="SingleAuthorPublication"> <xsd:complexContent> <xsd:restriction base="PublicationType"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:year"/> </xsd:sequence> </xsd:restriction> </xsd:complexContent> </xsd:complexType> <xsd:element name="BookCatalogue"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" minOccurs="0" maxOccurs="unbounded" type="PublicationType"/> </xsd:sequence> </xsd:complexType> </xsd:element>

Note that PublicationTypeis declared abstract.

Book derives from PublicationType. By defaultabstract="false". Thus, thistype can substitute for the PublicationType.

(see example 26)

38

<?xml version="1.0"?><BookCatalogue xmlns="http://www.publishing.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.publishing.org BookCatalogue.xsd"> <Book xsi:type="Book"> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <ISBN>94303-12021-43892</ISBN> <Publisher>McMillin Publishing</Publisher> </Book> <Book xsi:type="SingleAuthorPublication"> <Title>FooManchu</Title> <Author>Don Knox</Author><Date>1951</Date> </Book> <Book xsi:type="PublicationType"> // error! <Title>FooManchu</Title> <Author>Don Knox</Author><Date>1951</Date> </Book> <Book> // error! <Title>FooManchu</Title> <Author>Don Knox</Author><Date>1951</Date> </Book></BookCatalogue>

The content model of each <Book> element must be from a type that derives from PublicationType.In the schema there are two such types - Book and SingleAuthorPublication.

39

Review of Abstract Elements and Abstract complexTypes

• If you declare an element to be abstract– > Use element substitution for the abstract element

(as provided by substitutionGroup) name/type

• If you declare a complexType to be abstract– > Use type substitution for the abstract type (as

provided by type derivation)– element name not changed!!– use xsi:type to refer to actual type– use the content model of the actual type

Do Lab 14

40

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)

41

<xsd:complexType name="CardCatalogueEntry"> <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>

LibraryBookCatalogue.xsd (snippet, see example27)

<xsd:redefine schemaLocation="LibraryBookCatalogue.xsd"> <xsd:complexType name="CardCatalogueEntry"> <xsd:complexContent> <xsd:extension base="CardCatalogueEntry"> <xsd:sequence> <xsd:element name="Review" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:redefine>

Library.xsd (snippet , see example27)

42

This <redefine> element does two things: - it includes the components from LibraryBookCatalogue.xsd - it redefines CardCatalogueEntry (in LibraryBookCatalogue.xsd) by extending it with a new element (Review).

<xsd:redefine schemaLocation="LibraryBookCatalogue.xsd"> <xsd:complexType name="CardCatalogueEntry"> <xsd:complexContent> <xsd:extension base="CardCatalogueEntry"> <xsd:sequence> <xsd:element name="Review" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:redefine>

Library.xsd (snippet , see example27)

43

Note about <redefine>

• When a schema redefines a component then it's as though the old version of the component no longer exists. Any reference to the redefined component (either in the included schema or in the including schema) is to this new version.

44

<xsd:element name="BookCatalogue"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" minOccurs="0" maxOccurs="unbounded" type="CardCatalogueEntry"/> </xsd:sequence> </xsd:complexType></xsd:element>

LibraryBookCatalogue.xsd (snippet , see example27)

Because CardCatalogueEntry has been redefined, Book's content now also includes Review.

45

<redefine> Element

<redefine schemaLocation="URL to schema document"> [simpleType or complexType or attributeGroup or group]*</redefine>

“The <redefine> element can redefine zero or more components in the referenced schema.”

46

Redefining a Schema with no targetNamespace

• External components from schemas that have no namespace can also be redefined.

• The redefined components become part of the redefining schema's namespace.

• Thus, a schema with no namespace may "blend in" with a variety of different schemas and take on each schema's namespace!

47

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xsd:complexType name="CardCatalogueEntry"> <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:schema>

BookCatalogue.xsd (see example28)

Note that there is no targetNamespace!

48

<xsd:redefine schemaLocation="BookCatalogue.xsd"> <xsd:complexType name="CardCatalogueEntry"> <xsd:complexContent> <xsd:extension base="CardCatalogueEntry"> <xsd:sequence> <xsd:element name="Review" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType></xsd:redefine>

Library.xsd (snippet , see example28)

As soon as we redefine BookCatalogue.xsd it takes on the library namespace.

49

Note about redefining a schema with no targetNamespace

• If the components in the schema with no targetNamespace ref one another then the <include>ing schema must be designed so that the targetNamespace is the default namespace!

<xsd:complexType name="CardCatalogueEntry"> <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:element name="BookCatalogue" type="CardCatalogueEntry"/>

When this schema takes on a namespace then type="CardCatalogueEntry" will be referring tothe default namespace. If the default namespace is not the targetNamespace then this referencewill break!

50

Version Management

• The schema element has an optional attribute, version, which you may use to indicate the version of your schema (for private version documentation of the schema)

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.publishing.org" xmlns="http://www.publishing.org" elementFormDefault="qualified" version="1.0"> ...</xsd:schema>

51

nillable Content

• You can indicate in a schema that an element may be null in the instance document. Empty content vs null:

– Empty: an element with an empty content is constrained to have no content.

– null: an instance document element may indicate no value is available by setting an attribute - xsi:nil - equal to 'true'

<xsd:element name="PersonName"> <xsd:complexType> <xsd:element name="forename" type="xsd:NMTOKEN"/> <xsd:element name="middle" type="xsd:NMTOKEN" nillable="true"/> <xsd:element name="surname" type="xsd:NMTOKEN"/> </xsd:complexType></xsd:element>

<PersonName> <forename>John</forename> <middle xsi:nil="true"/> <surname>Doe</surname></PersonName>

XML Schema:

XML instancedocument:

The content of middle canbe a NMTOKEN value or,its content can be undefined.

52

ur-type

• The ur-type is the base type for all types which do not specify a value for the base attribute. It is the type for all elements which do not specify a type.– Example: <xsd:element name="foo"/>

<xsd:complexType name="xsd:ur-type" mixed="true"> <xsd:sequence> <xsd:any minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> <xsd:anyAttribute/></xsd:complexType>

53

anyType

• You may declare an element to be of type "anyType"

• The anyType is equivalent to ur-type.

• Whereas ur-type cannot be used directly, anyType can (see next slide)

54

Note about ur-type

You cannot directly use the ur-type:

<xsd:element name="foo" type="xsd:ur-type"/>

Instead, it is the default type when a type is not specified in an element declaration, or when you specify the type as "anyType"

<xsd:element name="bar"/> <xsd:element name="bar" type="xsd:anyType"/>

No type specified so it defaults to ur-type anyType is equivalent to ur-type

55

Note about schemaLocation

• schemaLocation is just a hint to the XML Parser

• "The choice of which schema to use ultimately lies with the consumer. If you as a consumer wish to rely on the schemaLocation idiom, then you should purchase/use processors that will honor that for you. The reason that some other processors might not provide that service to you is that they are designed to run in environments where it is impractical or undesirable to allow the document author to force reference to and use of some particular schema document." (Noah Mendelsohn, XML Schema WG)

• For this tutorial I have used an XML Schema Validator which uses the schemaLocation idiom.

56

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

57

unique vs key

• Key: an element or attribute (or combination thereof) 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

58

Using ISBN as a Key

• When a book is published it has an ISBN, which is guaranteed to be unique.

• In the BookCatalogue 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).

59

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.publishing.org" xmlns="http://www.publishing.org" elementFormDefault="qualified"> <xsd:element name="BookCatalogue"> <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:key name="PK"> <xsd:selector xpath="Book"/> <xsd:field xpath="ISBN"/> </xsd:key> </xsd:element></xsd:schema>

(see example 29)

60

<xsd:element name="BookCatalogue"> ... <xsd:key name="PK"> <xsd:selector xpath="Book"/> <xsd:field xpath="ISBN"/> </xsd:key> </xsd:element>

"Within <BookCatalogue> we define a key, called PK. Select each <Book>, andwithin each <Book> the ISBN element isa key."

In other words, within <BookCatalogue>each <Book> must have an <ISBN> andit must be unique.

This is nice! We are using the content of a field as a key! (No longer limited to ID attributesfor defining uniqueness.)

61

<?xml version="1.0"?><BookCatalogue xmlns="http://www.publishing.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.publishing.org BookCatalogue.xsd"> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <ISBN>94303-12021-43892</ISBN> <Publisher>McMillin Publishing</Publisher> </Book> <Book> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </Book> <Book> <Title>The First and Last Freedom</Title> <Author>J. Krishnamurti</Author> <Date>1954</Date> <ISBN>0-06-064831-7</ISBN> <Publisher>Harper &amp; Row</Publisher> </Book></BookCatalogue>

(see example 29)

A schema-validatorwill verify that eachBook has an ISBNelement and that thevalues are all unique.

62

Notes about <key>

• It must be nested within an <element>

• It must come at the end of <element> (after the content model, and attribute declarations)

• Use the <selector> element as a child of <key> to select a set of elements for which the key applies.

• Use the <field> element as a child of <key> to identify the element or attribute that is to be the key– There can be multiple <field> elements. See next

example.

63<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.CostelloReunion.org" xmlns="http://www.CostelloReunion.org" elementFormDefault="qualified"> <xsd:element name="Y2KFamilyReunion"> <xsd:complexType> <xsd:sequence> <xsd:element name="Participants" > <xsd:complexType> <xsd:sequence> <xsd:element name="Name" minOccurs="0" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="First" type="xsd:string"/> <xsd:element name="Last" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:key name="PK"> <xsd:selector xpath="Participants/Name"/> <xsd:field xpath="First"/> <xsd:field xpath="Last"/> </xsd:key> </xsd:element></xsd:schema>

The key is the combination of the First and Last name.(See example30)

64

<?xml version="1.0"?><Y2KFamilyReunion xmlns="http://www.CostelloReunion.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.CostelloReunion.org Y2KFamilyReunion.xsd"> <Participants> <Name><First>Peter</First><Last>Brown</Last></Name> <Name><First>Peter</First><Last>Costello</Last></Name> </Participants></Y2KFamilyReunion>

A schema-validator will verify that each First name/Last name combination is unique.

65

unique

• The <unique> element is used exactly like the <key> element is used. It has a <selector> and one or more <field> elements, just like <key> has.

• The only difference is that the schema validator will simply validate that, whenever present, the values are unique.

66

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.publishing.org" xmlns="http://www.publishing.org" elementFormDefault="qualified"> <xsd:element name="BookCatalogue"> <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="Book"/> <xsd:field xpath="ISBN"/> </xsd:unique> </xsd:element></xsd:schema>

(see example 31)

Note: ISBNis optional

Requireevery ISBNbe unique.

67

<?xml version="1.0"?><BookCatalogue xmlns="http://www.publishing.org/namespaces/BookCatalogue" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.publishing.org/namespaces/BookCatalogue BookCatalogue24.xsd"> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <Publisher>McMillin Publishing</Publisher> </Book> <Book> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </Book> <Book> <Title>The First and Last Freedom</Title> <Author>J. Krishnamurti</Author> <Date>1954</Date> <ISBN>0-06-064831-7</ISBN> <Publisher>Harper &amp; Row</Publisher> </Book></BookCatalogue>

(see example 31)

A schema-validatorwill verify that eachBook which has an ISBN element, has a unique value (notethat the first Bookdoes not have anISBN. That's perfectlyvalid!)

68

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 these elements must match the value of an element referred to by this".

69

<?xml version="1.0"?><Library xmlns="http://www.library.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.library.org AuthorSigningAtLibrary27.xsd"> <BookCatalogue> ... <Book> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </Book> ... </BookCatalogue> <GuestAuthors> <Author> <Name>Richard Bach</Name> <BookForSigning> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <ISBN>0-440-34319-4</ISBN> </BookForSigning> </Author> </GuestAuthors></Library>

Suppose that we define akey for ISBN (i.e., eachbook must have an ISBNand it must be unique)

We would like to ensurethat the ISBN for the GuestAuthor matchesone of the ISBNs in theBookCatalogue.

A key element

A keyref element

70

<xsd:element name="Library"> <xsd:complexType> <xsd:sequence> <xsd:element ref="BookCatalogue"/> <xsd:element ref="GuestAuthors"/> </xsd:sequence> </xsd:complexType> <xsd:key name="PK"> <xsd:selector xpath="BookCatalogue/Book"/> <xsd:field xpath="ISBN"/> </xsd:key> <xsd:keyref name="isbnRef" refer="PK"> <xsd:selector xpath="GuestAuthors/Author/BookForSigning"/> <xsd:field xpath="ISBN"/> </xsd:keyref> </xsd:element>

AuthorSigningAtLibrary.xsd (snippet, see example32)

71

<xsd:key name="PK"> <xsd:selector xpath="BookCatalogue/Book"/> <xsd:field xpath="ISBN"/></xsd:key>

This tells the schema validator to validate thatevery Book (in BookCatalogue) has an ISBN, andthat ISBN must be unique.

<xsd:keyref name="isbnRef" refer="PK"> <xsd:selector xpath="GuestAuthors/Author/BookForSigning"/> <xsd:field xpath="ISBN"/></xsd:keyref>

This tells the schema validator that the ISBN of the Bookthat the Author is signing must refer to one of the ISBNelements in the collection defined by the PK key.

72

Note about key and keyref

• If there are 2 fields in the key, then there must be 2 fields in the keyref, if there are 3 fields in the key, then there must be 3 fields in the keyref, etc.

• Further, the fields in the keyref must match in type and position to the key.

73

Specifying scope of uniqueness in XML Schemas

• The key/keyref/unique elements may be placed anywhere in your schema (that is, at the bottom of any element declaration)

• Where you place them determines the scope of the uniqueness

• Example. We may desire to have uniqueness in a localized region of instance documents. Thus, we would use key/keyref/unique within the element for that region.

74

Mixed Content

• In all of our examples the content of each element was either

– all elements, or

– all data• An element that contains a mix of elements and (string) data is called "mixed content".

• Mixed content has many applications. For example, XSLT uses mixed content

frequently in template rules, e.g.,

<xsl:template match="Book"> The title of the book is: <xsl:value-of select="Title/text()"/> The author of the book is: <xsl:value-of select="Author/text()"/></xsl:template>

Notice that the content ofthe xsl:template element isa mix of string data and elements.

75

Specifying Mixed Content when Declaring an Element

• The <complexType> element has an optional attribute, mixed. By default, mixed="false".

• To specify that an element can have mixed content use <complexType mixed="true">

76

<?xml version="1.0"?><Letter xmlns="http://www.letter.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.letter.org Letter.xsd"> <Body> Dear Sirs: This letter is to inform you that we are are finding your tool <emp> very </emp> useful. </Body></Letter>

Letter.xml (see example33)

77

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.letter.org" xmlns="http://www.letter.org" elementFormDefault="qualified"> <xsd:element name="Letter"> <xsd:complexType> <xsd:sequence> <xsd:element name="Body" minOccurs="0" maxOccurs="unbounded"> <xsd:complexType mixed="true"> <xsd:sequence> <xsd:element name="emp" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>

Letter.xsd (see example33)

78

Not “All Powerful”• XML Schemas is 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.

• inadequate for semantic validation!!. (context sensitive!)

79

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.