83
1 (c) [2001]. Roger L. Costello. All Rights Reserved. … more on XML Schemas Roger L. Costello XML Technologies Course

Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

1Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

… more on XML Schemas

Roger L. Costello

XML Technologies Course

Page 2: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

2Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

places. However, there are some situations where it is illegal to use the same name.

• Where can the same name be used, and where will there be a name conflict? Here are the rules that regulate names:

– 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

Page 3: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

3Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

Page 4: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

4Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<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

Page 5: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

5Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

Global elementsymbol space

Global typesymbol space

BookOnCars

Title

Title

Global attributesymbol space

Title

Page 6: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

6Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

Page 7: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

7Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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?

Page 8: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

8Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

BookOnCars

Chapter Section

TitleTitle

TitleTitle

TitleName collisions!!!

CarBooks Namespace?

Page 9: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

9Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

Page 10: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

10Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

BookOnCars

Title

CarBooks Namespace [1]

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

Page 11: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

11Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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; thus, there would be many name collisions if attributes were made part of a default namespace. Instead, we say that attributes are not in a default namespace. Rather, they are associated with elements which are in the namespace. (See next slide for example)

Page 12: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

12Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<?xml version="1.0"?><Book xmlns="http://www.books.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.

Default namespace declaration

Page 13: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

13Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

But, but, but, ...

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

<?xml version="1.0"?><BookStore xmlns="http://www.books.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> ...</BookStore>

Default name-space declarationasserts that allthese elementsare in theBook namespace.

Page 14: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

14Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

Unqualified Local Elements - How?

• Okay, so 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"

Page 15: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

15Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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 elements must be qualified.

• Alternatively, you can assign elementFormDefault the value "unqualified". The "unqualified" value means that in an instance document only the global elements can be qualified and the local elements must not be qualified.

Page 16: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

16Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<?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="unqualified"> <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>

(see example 26)

Notice thatthere isonly oneglobalelement inthe schema

Page 17: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

17Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<?xml version="1.0"?><bk:BookStore xmlns:bk="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.books.org BookStore.xsd"> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <ISBN>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></bk:BookStore>

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

(see example 26)

Page 18: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

18Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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"

Page 19: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

19Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

Recall the Camera Example

Camera.xsd

Nikon.xsd Olympus.xsd Pentax.xsd

Page 20: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

20Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

Camera.xml

This is what the instance document looks like whenelementFormDefault="qualified"

<?xml version="1.0"?><camera xmlns="http://www.camera.org" xmlns:nikon="http://www.nikon.com" xmlns:olympus="http://www.olympus.com" xmlns:pentax="http://www.pentax.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.camera.org Camera.xsd> <body> <nikon:description>Ergonomically designed casing for easy handling</nikon:description> </body> <lens> <olympus:zoom>300mm</olympus:zoom> <olympus:f-stop>1.2</olympus:f-stop> </lens> <manual_adapter> <pentax:speed>1/10,000 sec to 100 sec</pentax:speed> </manual_adapter></camera>

<nikon:description>Ergonomically designed casing for easy handling</nikon:description>

<olympus:zoom>300mm</olympus:zoom><olympus:f-stop>1.2</olympus:f-stop>

<pentax:speed>1/10,000 sec to 100 sec</pentax:speed>

Page 21: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

21Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<?xml version="1.0"?><my:camera xmlns:my="http://www.camera.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.camera.org Camera.xsd"> <body> <description>Ergonomically designed casing for easy handling</descriptio </body> <lens> <zoom>300mm</zoom> <f-stop>1.2</f-stop> </lens> <manual_adapter> <speed>1/10,000 sec to 100 sec</speed> </manual_adapter></my:camera>

Instance document with namespaces hidden(localized) within the schema

<body> <description>Ergonomically designed casing for easy handling</description></body><lens> <zoom>300mm</zoom> <f-stop>1.2</f-stop></lens><manual_adapter> <speed>1/10,000 sec to 100 sec</speed></manual_adapter>

Instance document when elementFormDefault="unqualified"

--> The fact that the <description> element comes from theNikon schema, the <zoom> and <f-stop> elements come fromthe Olympus schema, and the <speed> element comes from thePentax schema is totally transparent to the instance document.

Instance Document

Page 22: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

22Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

Use qualified or unqualified?

• In an instance document, regardless of whether you qualify all elements or just the global elements (as dictated by the value of elementFormDefault) they all get validated.

• So, what difference does it make whether you assign elementFormDefault the value of qualified or unqualified?

• Case 1: elementFormDefault="unqualified" and thus in the instance documents 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 in the instance documents 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; for copyright/traceability purposes it may be desirable to explicitly expose the namespaces in the instance document

– Con: exposes namespace complexity to the instance documents

Page 23: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

23Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

Type Substitutability

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

• Here’s the principle of type substitutability: A base type can be substituted by any derived type.

– Example. Suppose that BookType is derived from PublicationType. If we declare an element, Publication, to be of type PublicationType (the base type) then in the instance document Publication's content can be either a PublicationType or a BookType.

Page 24: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

24Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<?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="unqualified"> <xsd:complexType name="PublicationType"> <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="BookType"> <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 name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Publication" maxOccurs="unbounded" type="PublicationType"/> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>

(see example 27)

BookType extendsPublicationType

Publication is of typePublicationType(the base type)

PublicationType is the base type

Note this

Page 25: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

25Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<?xml version="1.0"?><bk:BookStore xmlns:bk="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.books.org BookStore.xsd"> <Publication> <Title>Staying Young Forever</Title> <Author>Karin Granstrom Jordan, M.D.</Author> <Date>1999</Date> </Publication> <Publication xsi:type="bk:BookType"> <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> </Publication> <Publication xsi:type="bk:BookType"> <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> </Publication></bk:BookStore>

BookStore.xml (see example 27)

This Publication’scontent model isPublicationType

This Publication’scontent model isBookType

This Publication’scontent model isBookType

Page 26: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

26Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<Publication xsi:type="bk:BookType"> <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></Publication>

"The Publication element is declared to be of type PublicationType. BookTypeis derived from PublicationType. Therefore, BookType is a PublicationType. Thus, thecontent of Publication can be a BookType. 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 BookType is a global type definition. Byqualifying BookType (bk:BookType) we are asserting that BookType comes from the http://www.books.org namespace.

Page 27: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

27Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

http://www.books.org Namespace

PublicationType (T)

BookType (T)

Publication (E)

T = TypeE = Element

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

Page 28: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

28Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

Do Lab 13

Page 29: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

29Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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 of that complexType.

Page 30: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

30Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<xsd:complexType name="PublicationType"> <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="BookType"> <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 name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Publication" maxOccurs="unbounded" type="PublicationType"/> </xsd:sequence> </xsd:complexType> </xsd:element>

<Publication xsi:type="bk:BookType"> <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></Publication>

Schema:

Instance doc:

The PublicationType, and types derived from PublicationTypemay be substituted for the content model of Publication, e.g., BookType may be used.

Page 31: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

31Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<xsd:complexType name="PublicationType"> <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="BookType"> <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 name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Publication" maxOccurs="unbounded" type="PublicationType" block="#all"/> </xsd:sequence> </xsd:complexType> </xsd:element>

<Publication xsi:type="bk:BookType"> <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></Publication>

Schema:

Instance doc:

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

Page 32: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

32Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<xsd:complexType name="PublicationTypr" 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="BookType"> <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 name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Publication" minOccurs="0" maxOccurs="unbounded" type="PublicationType"/> </xsd:sequence> </xsd:complexType> </xsd:element>

<Publication xsi:type="bk:BookType"> <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></Publication>

Schema:

Instance doc:

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

Page 33: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

33Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

Page 34: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

34Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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 substitutionGroup’ed to the abstract type may appear in its place.

Page 35: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

35Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<xsd:complexType name="PublicationType"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="BookType"> <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="MagazineType"> <xsd:complexContent> <xsd:restriction base="PublicationType"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string" minOccurs="0" maxOccurs="0"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:restriction> </xsd:complexContent> </xsd:complexType> <xsd:element name="Publication" abstract="true" 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>

BookStore.xsd (see example 28)

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

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

Page 36: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

36Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<?xml version="1.0"?><BookStore xmlns="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.books.org BookStore.xsd"> <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></BookStore>

(see example 28)

An XML Instance Document Conforming to BookStore.xsd

Page 37: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

37Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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.

Page 38: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

38Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<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="BookType"> <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="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" 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)

Page 39: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

39Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<?xml version="1.0"?><BookStore xmlns="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.books.org BookStore.xsd"> <Book xsi:type="BookType"> <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 Keyote</Author> <Date>1951</Date> </Book></BookStore>

(see example 29)

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

Page 40: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

40Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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)

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

(as provided by type derivation)

Do Lab 14

Page 41: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

41Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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)

Page 42: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

42Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<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 (snippet, see example30)

<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 (snippet , see example30)

Redefining BookType toalso have a Summaryelement.

Page 43: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

43Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

<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 (snippet , see example30)

Page 44: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

44Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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.

Page 45: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

45Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<xsd:element name="Books"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded" type="BookType/> </xsd:sequence> </xsd:complexType></xsd:element>

Library.xsd (snippet , see example30)

Because BookType has been redefined, Book's content now also includes Summary.

Page 46: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

46Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

Page 47: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

47Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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 targetNamespace (just as it did when we <include>d a no-namespace schema.

Page 48: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

48Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

LibraryBook.xsd (see example31)

Note that there is no targetNamespace!

Page 49: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

49Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<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 (snippet , see example31)

As soon as we redefine LibraryBook.xsd it takes on the library targetNamespace.

Page 50: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

50Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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.books.org" xmlns="http://www.books.org" elementFormDefault="qualified" version="1.0"> ...</xsd:schema>

Page 51: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

51Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

nil Content

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

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

– nil: 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.

Page 52: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

52Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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.

Page 53: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

53Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

Page 54: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

54Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

Page 55: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

55Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<?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"> <xsd:complexType mixed="true"> <xsd:sequence> <xsd:element name="emp" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>

Letter.xsd (see example36)

Page 56: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

56Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

This is thedefinition ofthe ur-type.

Page 57: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

57Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

anyType

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

• The anyType is equivalent to ur-type.

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

Page 58: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

58Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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 you can use the "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

Page 59: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

59Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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. To date, all the schema-validators use the schemaLocation idiom.

Page 60: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

60Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

When would you NOT want to use schemaLocation?

If you have a server application that processes XML documents, are you really going to trust the author of the document to provide you the correct schema to validate that instance against? For instance, you accept purchase orders that conform to a vocabulary specified by OAG or CommerceOne or what have you. Your application can handle any XML document that validates against these schemas. If schemaLocation were not optional, Joe Hacker could send you an instance that claims to be from one of these vocabularies and tells you you can get the schema off of some web site in the former Soviet Union. This schema could be anything and the instance will be valid against it, but bad things happen to your application. What is the purpose of validating against a schema if you have no control over where that schema came from?

All the XML Schema compliant parsers I am aware of will use schemaLocation unless you (the application) override it. This is how it should be. In my XML world, xsi:schemaLocation is a security risk and I will not use a parser that doesn't allow me to disable it.

David Cleary

Page 61: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

61Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

Page 62: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

62Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

Page 63: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

63Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

Using ISBN as a Key

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

• 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).

Page 64: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

64Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<?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"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:key name="PK"> <xsd:selector xpath="bk:Book"/> <xsd:field xpath="bk:ISBN"/> </xsd:key> </xsd:element></xsd:schema>

(see example32)

Page 65: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

65Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

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

Page 66: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

66Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

Must namespace-qualify xPath expressions

(when elementFormDefault="qualified")

Note that we namespace-qualified the xPath references:

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

This is required, even though the targetNamespace is thedefault namespace. This is an xPath requirement.

Note that if the schema had instead set elementFormDefault="unqualified" then the xPath expressions would not be namespace-qualified.

Page 67: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

67Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

(see example32)

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

Page 68: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

68Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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.

Page 69: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

69Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

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

Page 70: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

70Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

Page 71: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

71Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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.

Page 72: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

72Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

(see example 34)

Note: ISBNis optional

Requireevery ISBNbe unique.

Page 73: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

73Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<?xml version="1.0"?><BookStore xmlns="http://www.books.org/namespaces/BookStore" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.books.org/namespaces/BookStore BookStore24.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></BookStore>

(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!)

Page 74: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

74Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

Page 75: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

75Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<?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 AuthorSigningAtLibrary.xsd"> <Books> <Book> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </Book> ... </Books> <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 theBookStore.

A key element

A keyref element

Page 76: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

76Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

AuthorSigningAtLibrary.xsd (snippet, see example35)

Page 77: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

77Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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

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

<xsd:keyref name="isbnRef" refer="PK"> <xsd:selector xpath="bk:GuestAuthors/bk:Author/bk:BookForSigning"/> <xsd:field xpath="bk: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.

Page 78: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

78Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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.

Page 79: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

79Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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.

Page 80: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

80Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

Using xml:lang and xml:space

• There is a schema, xml.xsd, which has the attribute declarations for xml:lang and xml:space.

• You can import xml.xsd into your schema and then use these attributes.

Page 81: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

81Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

<?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:xml="http://www.w3.org/XML/1998/namespace" elementFormDefault="qualified"> <xsd:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="http://www.w3.org/2001/xml.xsd"/> <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:attribute ref="xml:lang"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>

(see example39)

Page 82: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

82Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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.

Page 83: Copyright (c) [2001]. Roger L. Costello. All Rights Reserved. 1 … more on XML Schemas Roger L. Costello XML Technologies Course

83Copyright (c) [2001]. Roger L. Costello. All Rights Reserved.

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.

See my tutorial on Extending XML Schemas for more info ...