222
1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1) http://www.w3.org/TR/xmlschema-1/ (Structures)

1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1) (Structures)

Embed Size (px)

Citation preview

Page 1: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

1

XML Schemas

Modified version of:Roger L. CostelloXML Technologies Course (Part 1)

http://www.w3.org/TR/xmlschema-1/ (Structures)

Page 2: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

2

Lab Exercises

•Periodically there appears an icon at the bottom, right of the slide indicating that it is time to do a lab exercise. •The corresponding exercises can be found on the website of Roger L. Costello at

http://www.w3.org/TR/xmlschema-1/

Page 3: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

3

XML Schema Overview

• On the next few slides is a very quick, high-level introduction to XML Schema.

• The purpose is to give the "big picture" before jumping into all the nitty-gritty details of creating XML schemas.

Page 4: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

4

What is XML Schema?

An XML vocabulary

for expressing

the business rules of one’s data

Page 5: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

5

Example<location> <latitude>32.904237</latitude> <longitude>73.620290</longitude> <uncertainty units="meters">2</uncertainty></location>

To be valid, this data element must meet constraints (data business rules):

1.The location must comprise a latitude, followed by a longitude,followed by an indication of the uncertainty of the lat/lon measurements.2.The latitude must be a decimal with a value between -90 to +903.The longitude must be a decimal with a value between -180 to +1804.For both latitude and longitude the number of digits to the rightof the decimal point must be exactly six digits.5.The value of uncertainty must be a non-negative integer6.The uncertainty units must be either meters or feet.

We can express all these data constraints using XML Schemas

Page 6: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

6

Validating your data<location> <latitude>32.904237</latitude> <longitude>73.620290</longitude> <uncertainty units="meters">2</uncertainty></location>

Declare a location element. Require that its content be latitude, longitude, and uncertainty.Declare a latitude element. Require that its value be between -90 and +90.Declare a longitude element. Require that its value be between -180 and +180. Declare a uncertainty element with a units attribute. Require that the element's value be between 0 and 10.Require that the attribute's value be either feet or meters.

XML Schema

XML Schemavalidator

Data is ok!XML

Page 7: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

7

What Does an XML Schema Accomplish?

Declare a location element. Require that its content be latitude, longitude, and uncertainty.Declare a latitude element. Require that its value be between -90 and +90.Declare a longitude element. Require that its value be between -180 and +180. Declare a uncertainty element with a units attribute. Require that the element's value be between 0 and 10.Require that the attribute's value be either feet or meters.

XML SchemaAnswer: It creates an XML vocabulary: <location>, <latitude>, <longitude>, <uncertainty>It specifies the contents of each element, and the restrictionson the content.

Page 8: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

8

It Does One More Thing ...

An XML Schema specifies that the XML vocabularythat is being created shall be in a "namespace"

Namespace = http://www.example.org/targetDeclare a location element. Require that its content be latitude, longitude, and uncertainty.Declare a latitude element. Require that its value be between -90 and +90.Declare a longitude element. Require that its value be between -180 and +180. Declare a uncertainty element with a units attribute. Require that the element's value be between 0 and 10.Require that the attribute's value be either feet or meters.

XML Schema

Page 9: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

9

http://www.example.org/target Namespace

<location>

<latitude><longitude>

<uncertainty>

http://www.example.org/target

Page 10: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

10

Constraints: in a Document or in Middleware (code)?

• An XML Schema is an XML document. The constraints on the data are expressed in a document.

• All of the constraints could be expressed in a programming language (e.g., Java), in your system's middleware.

• Why express data constraints in a document?

• Answer: by expressing the data constraints in a document (and using XML to express the constraints),the schema itself becomes information! Not only is the XML instance data being checked information, but the schema itself is information. Thus, just like the XML instance document, the schema can be shipped around, mined, morphed, searched, etc.

Page 11: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

11

Time to Start!

Page 12: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

12

Purpose of XML Schemas (… and DTDs)

Specify• the structure of instance documents

“this element contains these elements, whichcontains these other elements, etc.”

• the data type of each element/attribute

"this element shall hold an integer with the range 0 to 12,000”

(DTDs don't do too well with specifying data types like this)

Page 13: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

13

Motivation for XML SchemasPeople were dissatisfied with DTDs• It's a different syntax

– You write your XML (instance) document using one syntax and the DTD using another syntax bad, inconsistent

• Limited data type capability– DTDs have very limited capability for specifying data types.

You can't, for example, express

"I want the <elevation> element to hold an integer with a range of 0 to 12,000"

– Better a set of data types compatible with those in databases

• DTD supports 10 data types; • XML Schemas supports 44+ data types

Page 14: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

14

Highlights of XML Schema

XML Schemas are an advancement over DTDs:

• Enhanced data types

– 44+ versus 10

– User-defined data typesExample: "This is a new type based on the string type and elements of this type must follow this pattern: ddd-dddd, where 'd' represents a digit".

• Written in the same syntax as instance documents

– less syntax to remember (although cumbersome syntax)

• Object-oriented'ish

– Can extend or restrict a type (derive new type definitions on the basis of old ones)

Page 15: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

15

Example

We convert the BookStore.dtd (next page)

to the XML Schema

syntax

• Straight, one-to-one conversion, i.e.,

Title, Author, Date, ISBN, and Publisher

will hold strings, just like is done in the DTD

• We will gradually modify the XML Schema

to use stronger types

Page 16: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

16

BookStore.dtd

<!ELEMENT BookStore (Book+)><!ELEMENT Book (Title, Author, Date, ISBN, Publisher)><!ELEMENT Title (#PCDATA)><!ELEMENT Author (#PCDATA)><!ELEMENT Date (#PCDATA)><!ELEMENT ISBN (#PCDATA)><!ELEMENT Publisher (#PCDATA)>

Page 17: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

17

ATTLIST

ELEMENT

ID#PCDATA

NMTOKEN

ENTITY

CDATA

This is the vocabulary that DTDs provide to define yournew vocabulary

Page 18: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

18

ATTLIST

ELEMENT

ID#PCDATA

NMTOKEN

ENTITY

CDATA

BookStore

BookTitle

Author

Date ISBN

Publisher

This is the vocabulary that DTDs provide to define yournew vocabulary

This is the new vocabulary

Page 19: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

19

ATTLIST

ELEMENT

ID#PCDATA

NMTOKEN

ENTITY

CDATA

BookStore

BookTitle

Author

Date ISBN

Publisher

This is the vocabulary that DTDs provide to define yournew vocabulary

A difference between XML Schemas and DTDs is that the XML Schema vocabularyis associated with a name (namespace). Likewise, the new vocabulary that you define must be associated with a name (namespace). With DTDs neither set of vocabulary is associated with a name (namespace) [because DTDs pre-dated namespaces].

This is the new vocabulary

Page 20: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

20<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/></xsd:schema>

BookStore.xsd (see example01)

(explanations onsucceeding pages)

xsd = Xml-Schema Definition

Page 21: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

21

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/></xsd:schema>

<!ELEMENT Title (#PCDATA)><!ELEMENT Author (#PCDATA)><!ELEMENT Date (#PCDATA)><!ELEMENT ISBN (#PCDATA)><!ELEMENT Publisher (#PCDATA)>

<!ELEMENT Book (Title, Author, Date, ISBN, Publisher)>

<!ELEMENT BookStore (Book+)>

Page 22: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

22

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/></xsd:schema>

All XML schemas have “schema” as the root element.

Page 23: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

23

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/></xsd:schema>

The elements anddata types thatare used to constructschemas - schema - element - complexType - sequence - stringcome from the namespacehttp://…/XMLSchema

Page 24: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

24

elementcomplexType

schemasequence

http://www.w3.org/2001/XMLSchema

XMLSchema Namespace

string

integer

boolean

Page 25: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

25

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/></xsd:schema>

Indicates that theelements definedby this schema - BookStore - Book - Title - Author - Date - ISBN - Publisherare to go in thenamespacehttp://www.books.org

Page 26: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

26

BookStore

BookTitle

Author

Date ISBN

Publisher

http://www.books.org (targetNamespace)

Book Namespace (targetNamespace)

Page 27: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

27

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/></xsd:schema>

The default namespace ishttp://www.books.orgwhich is thetargetNamespace!

This is referencing a Book element declaration. The Book in what namespace? Since there is no namespace qualifierit is referencing the Book element in the default namespace, which is thetargetNamespace! Thus, this is a reference to theBook element declarationin this schema.

Page 28: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

28

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/></xsd:schema>

This is a directive to any instance documents which conform to this schema: Any elements used by the instance document whichwere declared in this schema must be namespacequalified.

Page 29: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

29

Referencing a schema<?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>July, 1998</Date> <ISBN>94303-12021-43892</ISBN> <Publisher>McMillin Publishing</Publisher> </Book> ...</BookStore>

1. The default namespace declaration tells the schema-validator that all of the elements used in this instance document come from the http://www.books.org

namespace.

1

2

3

Page 30: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

30

Referencing a schema<?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>July, 1998</Date> <ISBN>94303-12021-43892</ISBN> <Publisher>McMillin Publishing</Publisher> </Book> ...</BookStore>

2. Attribute schemaLocation says that the http://www.books.org namespace is defined by BookStore.xsd (i.e., schemaLocation contains a pair of values).

1

2

3

Page 31: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

31

Referencing a schema<?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>July, 1998</Date> <ISBN>94303-12021-43892</ISBN> <Publisher>McMillin Publishing</Publisher> </Book> ...</BookStore>

3. The schemaLocation attribute we are using is the one in the XMLSchema-instance namespace.

1

2

3

Page 32: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

32

schemaLocation

type

noNamespaceSchemaLocation

http://www.w3.org/2001/XMLSchema-instance

XMLSchema-instance Namespace

nil

Page 33: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

33

Referencing a schema in an XML instance document

BookStore.xml BookStore.xsd

targetNamespace="http://www.books.org"schemaLocation="http://www.books.org BookStore.xsd"

- defines elements in namespace http://www.books.org

- uses elements from namespace http://www.books.org

A schema defines a new vocabulary. Instance documents use that new vocabulary.

Page 34: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

34

Two levels of checking

BookStore.xml BookStore.xsd XMLSchema.xsd(schema-for-schemas)

Validate that the xml documentconforms to the rules describedin BookStore.xsd

Validate that BookStore.xsd is a validschema document, i.e., it conformsto the rules described in theschema-for-schemas

Page 35: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

35

Default Value for minOccurs and maxOccurs

• The default value for minOccurs is "1"• The default value for maxOccurs is "1"

<xsd:element ref="Title" minOccurs="1" maxOccurs="1"/>

<xsd:element ref="Title"/>

Equivalent!

Do Lab1

Page 36: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

36

Qualify XMLSchema, Default targetNamespace

In the first example, we explicitly qualified all elements

from the XML Schema namespace.

The targetNamespace was the default namespace.

BookStore

BookTitle

Author

Date

ISBNPublisher

http://www.books.org (targetNamespace)http://www.w3.org/2001/XMLSchema

elementcomplexType

schema

sequence

string

integer

boolean

Page 37: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

37

Default XMLSchema, Qualify targetNamespace

Alternatively (equivalently), we can design our schema

so that XMLSchema is the default namespace.

BookStore

BookTitle

Author

Date

ISBNPublisher

http://www.books.org (targetNamespace)http://www.w3.org/2001/XMLSchema

elementcomplexType

schema

sequence

string

integer

boolean

Page 38: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

38<?xml version="1.0"?><schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns:bk="http://www.books.org" elementFormDefault="qualified"> <element name="BookStore"> <complexType> <sequence> <element ref="bk:Book" maxOccurs="unbounded"/> </sequence> </complexType> </element> <element name="Book"> <complexType> <sequence> <element ref="bk:Title"/> <element ref="bk:Author"/> <element ref="bk:Date"/> <element ref="bk:ISBN"/> <element ref="bk:Publisher"/> </sequence> </complexType> </element> <element name="Title" type="string"/> <element name="Author" type="string"/> <element name="Date" type="string"/> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/></schema>

(see example02)

Note that http://…/XMLSchemais the default namespace.Consequently, thereare no namespacequalifiers on - schema - element - complexType - sequence - string

Page 39: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

39<?xml version="1.0"?><schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns:bk="http://www.books.org" elementFormDefault="qualified"> <element name="BookStore"> <complexType> <sequence> <element ref="bk:Book" maxOccurs="unbounded"/> </sequence> </complexType> </element> <element name="Book"> <complexType> <sequence> <element ref="bk:Title"/> <element ref="bk:Author"/> <element ref="bk:Date"/> <element ref="bk:ISBN"/> <element ref="bk:Publisher"/> </sequence> </complexType> </element> <element name="Title" type="string"/> <element name="Author" type="string"/> <element name="Date" type="string"/> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/></schema>

(see example02)

Here we arereferencing aBook element.Where is thatBook elementdefined? In what namespace?The bk: prefixindicates whatnamespace thiselement is in. “bk:” has been set tobe the same as thetargetNamespace.

Page 40: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

40

“bk:” References the targetNamespace

BookStore

Book

Title

Author

DateISBN

Publisher

http://www.books.org (targetNamespace)http://www.w3.org/2001/XMLSchema

bkDo Lab1.1

elementcomplexType

schema

sequence

string

integer

boolean

Consequently, bk:Book refers to the Book element in the targetNamespace.

Page 41: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

41

Inlining Element Declarations

• In the previous examples we declared an element

and then we ref ’ed to that element declaration.

Alternatively, we can inline the element declarations.

• On the following slide is an alternate (equivalent) way

of representing the schema shown previously,

using inlined element declarations.

Page 42: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

42

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema> (see example03)

Note that we have moved all the element declarationsinline, and we are nolonger ref'ing to the element declarations.This results in a much more compact schema!

Designing a schema by inlining everything is called the Russian Doll design.

Page 43: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

43

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema> (see example03)

Do Lab 2

Anonymous types (no name)

Page 44: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

44

Named Types

The following slide shows

an alternate (equivalent) schema

which uses a named complexType.

Page 45: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

45

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" type="BookPublication"

maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name="BookPublication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType></xsd:schema>

The advantage of splitting out Book'selement declarationsand wrapping them in a named type isthat now this type can be reused byother elements.

(see example04)

Named type

Page 46: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

46

<xsd:element name="A" type="foo"> <xsd:complexType> … </xsd:complexType></xsd:element>

type Attribute or complexType Child Element, but not both!

An element declaration can have a type attribute, or a complexType child element, but it cannot have both • a type attribute and • a complexType child element.

Page 47: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

47

Summary of Declaring Elements (two ways to do it)

<xsd:element name="name" type="type" minOccurs="int"

maxOccurs="int"/>

A simple type (e.g., xsd:string)or the name ofa complexType (e.g., BookPublication)

1

A nonnegativeinteger

A nonnegativeinteger or "unbounded"

Note: minOccurs and maxOccurs can only be used in nested (local) element declarations.

Page 48: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

48

Summary of Declaring Elements (two ways to do it)

<xsd:element name="name" type="type" minOccurs="int"

maxOccurs="int"/>

1

<xsd:element name="name" minOccurs="int" maxOccurs="int"> <xsd:complexType> … </xsd:complexType></xsd:element>

2

Page 49: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

49

Problem• Defining the Date element to be of type string is unsatisfactory

(it allows any string value to be input as the content of the Date element, including non-date strings).– Constrain the allowable content that Date can have.

Modify the BookStore schema to restrict the content of the Date element to just date values

(actually, year values; see next two slides).• Similarly, constrain the content of the ISBN element to content

of this form:

– d-ddddd-ddd-d or – d-ddd-ddddd-d or – d-dd-dddddd-d,

where 'd' stands for 'digit'

Page 50: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

50

The date Data Type• A built-in data type

(i.e., schema validators know about this data type)• Used to represent a specific day (year-month-day)• Elements declared to be of type date must follow this form:

CCYY-MM-DD– range for CC is: 00-99– range for YY is: 00-99– range for MM is: 01-12– range for DD is:

• 01-28 if month is 2• 01-29 if month is 2 and the gYear is a leap gYear• 01-30 if month is 4, 6, 9, or 11• 01-31 if month is 1, 3, 5, 7, 8, 10, or 12

Example: 1999-05-31 represents May 31, 1999

Page 51: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

51

The gYear Data Type

• A built-in data type (Gregorian calendar year)

• Elements declared to be of type gYear must follow this form: CCYY

– range for CC is: 00-99

– range for YY is: 00-99

Example: 1999 indicates the gYear 1999

Page 52: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

52

(see example05)

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:simpleType name="ISBNType"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{1}-\d{5}-\d{3}-\d{1}"/> <xsd:pattern value="\d{1}-\d{3}-\d{5}-\d{1}"/> <xsd:pattern value="\d{1}-\d{2}-\d{6}-\d{1}"/> </xsd:restriction> </xsd:simpleType>

Here we are defining a new (user-defined) data-type, called ISBNType.

Page 53: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

53

(see example05)

<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:gYear"/> <xsd:element name="ISBN" type="ISBNType"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>

Declaring Date to be of type gYear, and ISBN to be of type ISBNType (defined above)

Page 54: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

54

<xsd:simpleType name="ISBNType"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{1}-\d{5}-\d{3}-\d{1}"/> <xsd:pattern value="\d{1}-\d{3}-\d{5}-\d{1}"/> <xsd:pattern value="\d{1}-\d{2}-\d{6}-\d{1}"/> </xsd:restriction></xsd:simpleType>

"I hereby declare a new type called ISBNType. It is a restricted form ofthe string type. Elements declared of this type must conform to one of the following patterns:- First Pattern: 1 digit followed by a dash followed by 5 digits followed by another dash followed by 3 digits followed by another dash followed by 1 more digit, or- Second Pattern: 1 digit followed by a dash followed by 3 digits followed by another dash followed by 5 digits followed by another dash followed by 1 more digit, or- Third Pattern: 1 digit followed by a dash followed by 2 digits followed by another dash followed by 6 digits followed by another dash followed by 1 more digit."

These patterns are specified using Regular Expressions. In a few slides we will see more of the Regular Expression syntax.

Page 55: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

55

Equivalent Expressions<xsd:simpleType name="ISBNType"> <xsd:restriction base="xsd:string"> <xsd:pattern value=“\d{1}-\d{5}-\d{3}-\d{1}“ /> <xsd:pattern value=“\d{1}-\d{3}-\d{5}-\d{1}“ /> <xsd:pattern value=“\d{1}-\d{2}-\d{6}-\d{1}“ /> </xsd:restriction></xsd:simpleType>

<xsd:simpleType name="ISBNType"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{1}-\d{5}-\d{3}-\d{1}

|\d{1}-\d{3}-\d{5}-\d{1} | \d{1}-\d{2}-\d{6}-\d{1}"/> </xsd:restriction></xsd:simpleType>

The vertical bars means "or"

Page 56: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

56

<xsd:complexType> or <xsd:simpleType>?

When do you use the complexType element and when

do you use the simpleType element?

– Use the complexType element when you want to define child elements and/or attributes of an element

– Use the simpleType element when you want to create a new type that is a refinement of a built-in type (string, date, gYear, etc)

Page 57: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

57

Built-in Data Types

Primitive data types– string– boolean– decimal– float– double– duration– dateTime– time– date– gYearMonth– gYear– gMonthDay

Atomic, built-in– "Hello World"– {true, false, 1, 0}– 7.08– 12.56E3, 12, 12560, 0, -0, INF, -INF, NAN – 12.56E3, 12, 12560, 0, -0, INF, -INF, NAN – P1Y2M3DT10H30M12.3S– format: CCYY-MM-DDThh:mm:ss– format: hh:mm:ss.sss– format: CCYY-MM-DD– format: CCYY-MM– format: CCYY– format: --MM-DD

Note: 'T' is the date/time separator INF = infinity NAN = not-a-number

Page 58: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

58

Built-in Data Types (cont.) Primitive data types

– gDay

– gMonth

– hexBinary

– base64Binary

– anyURI

– QName

– NOTATION

Atomic, built-in

– format: ---DD (note the 3 dashes)

– format: --MM

– a hex string

– a base64 string

– http://www.xfront.com

– a namespace qualified name

– a NOTATION from the XML spec

Page 59: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

59

Built-in Data Types (cont.)• Derived types

– normalizedString– token– language– IDREFS– ENTITIES– NMTOKEN– NMTOKENS– Name– NCName– ID– IDREF– ENTITY– integer– nonPositiveInteger

• Subtype of primitive data type– A string without tabs, line feeds, or carriage returns– String w/o tabs, l/f, leading/trailing spaces– any valid xml:lang value, e.g., EN, FR, ... – must be used only with attributes– must be used only with attributes– must be used only with attributes– must be used only with attributes– XML names– non-colonized name (no namespace qualifier)– must be used only with attributes– must be used only with attributes– must be used only with attributes– 456– negative infinity to 0

Page 60: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

60

Built-in Data Types (cont.)

• Derived types– negativeInteger– long– int – short– byte– nonNegativeInteger– unsignedLong– unsignedInt– unsignedShort– unsignedByte– positiveInteger

• Subtype of primitive data type– negative infinity to -1– -9223372036854775808 to

9223372036854775807 – -2147483648 to 2147483647– -32768 to 32767– -127 to 128– 0 to infinity– 0 to 18446744073709551615– 0 to 4294967295– 0 to 65535– 0 to 255– 1 to infinity

Do Lab 3

Note: the following types can only be used with attributes (which we will discuss later): ID, IDREF, IDREFS, NMTOKEN, NMTOKENS, ENTITY, and ENTITIES.

Page 61: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

61

Creating Your Own Data Types

• A new data type can be defined from an existing data type (called the "base" type) by specifying values for one or more of the optional facets for the base type.

• Example. The string primitive data type has six optional facets:– length– minLength– maxLength– pattern– enumeration– whitespace (legal values: preserve, replace, collapse)

Page 62: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

62

Creating a New Data Type by Specifying Facet Values

1. This creates a new data type called 'TelephoneNumber'. 2. Elements of this type can hold string values, 3. But the string length must be exactly 8 characters long and 4. The string must follow the pattern: ddd-dddd, where 'd' represents a 'digit'.

(In this example the regular expression makes the length facet redundant.)

<xsd:simpleType name="TelephoneNumber"> <xsd:restriction base="xsd:string"> <xsd:length value="8"/> <xsd:pattern value="\d{3}-\d{4}"/> </xsd:restriction></xsd:simpleType>

1

2

3

4

Page 63: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

63

Enumeration Types

<xsd:simpleType name="shape"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="circle"/> <xsd:enumeration value="triangle"/> <xsd:enumeration value="square"/> </xsd:restriction></xsd:simpleType>

This creates a new type called shape.An element declared to be of this typemust have either the value circle, or triangle, or square.

Page 64: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

64

Facets of the integer Data Type

• The integer data type has 8 optional facets:– totalDigits – pattern– whitespace– enumeration– maxInclusive– maxExclusive– minInclusive– minExclusive

Page 65: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

65

Example

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

This creates a new data type called 'EarthSurfaceElevation'.Elements declared to be of this type can hold an integer. However, the integer is restricted to have a value between -1290 and 29035, inclusive.

Page 66: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

66

General Form of Creating a New Data Type by Specifying Facet Values<xsd:simpleType name= "name"> <xsd:restriction base= "xsd:source"> <xsd:facet value= "value"/> <xsd:facet value= "value"/> … </xsd:restriction>

</xsd:simpleType>

Facets: - length - minlength - maxlength - pattern - enumeration - minInclusive - maxInclusive - minExclusive - maxExclusive ...

Sources: - string - boolean - number - float - double - duration - dateTime - time ...

Page 67: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

67

Multiple Facets – “and”ed or “or”ed?

An element declared to be of type TelephoneNumbermust be a string of length=8 and the string must follow the pattern: 3 digits, dash, 4 digits.

<xsd:simpleType name="TelephoneNumber"> <xsd:restriction base="xsd:string"> <xsd:length value="8"/> <xsd:pattern value="\d{3}-\d{4}"/> </xsd:restriction></xsd:simpleType>

Page 68: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

68

Multiple Facets – “and”ed or “or”ed?

<xsd:simpleType name="shape"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="circle"/> <xsd:enumeration value="triangle"/> <xsd:enumeration value="square"/> </xsd:restriction></xsd:simpleType>

An element declared to be of type shape must be a string with a value of either circle, or triangle, or square.

Patterns, enumerations => "or" them togetherAll other facets => "and" them together

Page 69: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

69

Creating a simpleType from Another simpleType

• Thus far we have created a simpleType using one of the built-in data types as our base type.

• However, we can create a simpleType that uses another simpleType as the base. See next slide.

Page 70: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

70

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

<xsd:simpleType name= "BostonAreaSurfaceElevation"> <xsd:restriction base="EarthSurfaceElevation"> <xsd:minInclusive value="0"/> <xsd:maxInclusive value="120"/> </xsd:restriction></xsd:simpleType>

This simpleType uses EarthSurfaceElevation as its base type.

Page 71: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

71

Fixing a Facet Value

Sometimes when defining a simpleType we want to require that one (or more) facets have an unchanging value. That is, we want to make the facet a constant.

<xsd:simpleType name= "ClassSize"> <xsd:restriction base="xsd:nonNegativeInteger"> <xsd:minInclusive value="10" fixed="true"/> <xsd:maxInclusive value="60"/> </xsd:restriction></xsd:simpleType>

simpleTypes whichderive from thissimpleType may not change this facet.

Page 72: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

72

<xsd:simpleType name= "ClassSize"> <xsd:restriction base="xsd:nonNegativeInteger"> <xsd:minInclusive value="10" fixed="true"/> <xsd:maxInclusive value="60"/> </xsd:restriction></xsd:simpleType>

<xsd:simpleType name= "BostonIEEEClassSize"> <xsd:restriction base="ClassSize"> <xsd:minInclusive value="15"/> <xsd:maxInclusive value="60"/> </xsd:restriction></xsd:simpleType>

Error! Cannotchange the valueof a fixed facet!

Page 73: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

73

Example. Create a schema element declaration for an elevation element.Declare the elevation element to be an integer with a range -1290 to 29035

<elevation>5240</elevation>

Here's one way of declaring the elevation element:

<xsd:simpleType name="EarthSurfaceElevation"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="-1290"/> <xsd:maxInclusive value="29035"/> </xsd:restriction> </xsd:simpleType> <xsd:element name="elevation" type="EarthSurfaceElevation"/>

Element Containing a User-Defined Simple Type

Page 74: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

74

Element Containing a User-Defined Simple Type (cont.)

Here's an alternative method for declaring elevation:

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

The simpleType definition isdefined inline, it is an anonymoussimpleType definition.

The disadvantage of this approach is that this simpleType may not bereused by other elements.

Page 75: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

75

Summary of Declaring Elements (three ways to do it)

<xsd:element name="name" type="type" minOccurs="int"

maxOccurs="int"/>

1

<xsd:element name="name" minOccurs="int" maxOccurs="int"> <xsd:complexType> … </xsd:complexType></xsd:element>

2

Page 76: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

76

Summary of Declaring Elements (three ways to do it)

3

<xsd:element name="name" minOccurs="int" maxOccurs="int"> <xsd:simpleType> <xsd:restriction base="type"> … </xsd:restriction> </xsd:simpleType></xsd:element>

Page 77: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

77

Annotating Schemas• The <annotation> element is used for documenting the schema,

both for humans and for machines.– Use <documentation> for providing a comment to humans– Use <appinfo> for providing a comment to machines

• The content is any well-formed XML• Note that annotations have no effect on schema validation

<xsd:annotation> <xsd:documentation> The following constraint is not expressible with XML Schema: The value of element A should be greater than the value of element B. We will express this constraint in the appinfo section (below). </xsd:documentation> <xsd:appinfo> <assert test="A &gt; B">A should be greater than B</assert> </xsd:appinfo><xsd:/annotation>

Page 78: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

78

Where Can You Put Annotations?

• You cannot put annotations at just any random location in the schema.

• Here are the rules for where an annotation element can go:– annotations may occur

before and after any global component– annotations may occur

only at the beginning of non-global components

Page 79: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

79

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>

Suppose that you want to annotate, say, the Date elementdeclaration. What do we do? See next page ...

Can putannotationsonly at theselocations

Page 80: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

80

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"> <xsd:annotation> <xsd:documentation>This is how to annotate the Date element!

</xsd:documentation> </xsd:annotation> </xsd:element> <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>

Inline the annotation within the Date element declaration.

Page 81: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

81

Two Optional Attributes for the documentation Element

In the previous example we showed

<xsd:documentation>

with no attributes. Actually, it can have two attributes:• source: this attribute contains a URL to a file which

contains supplemental information• xml:lang: this attribute specifies the language that the

documentation was written in

<xsd:documentation source="http://www.xfront.com/BookReview.txt" xml:lang="FR"/>

Page 82: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

82

One Optional Attribute for the appinfo Element

In the previous example we showed

<xsd:appinfo>

with no attributes.

Actually, it can have one attribute:

source: this attribute contains a URL to a file which contains supplemental information

<xsd:appinfo source="http://www.xfront.com/Assertions.xml"/>

Page 83: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

83

The Big Picture

• Let's back up for a moment and look at XML Schemas from a "big picture" point of view.

Page 84: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

84

Code to check the structure and content(data type) of the data

Code to actuallydo the work

"In a typical program, up to 60% of the code is spent checking the data!"

- source unknown

Saving Code Using XML Schemas

Continued -->

Page 85: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

85

Code to check the structure and content

of the data

Code to actuallydo the work

If your data is structured asXML, and there is a schema,then you can hand the data-checking task off to a schema validator.

Thus, your code is reducedby up to 60%!!!

Saving Code Using XML Schemas(cont.)

Page 86: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

86

Classic Use of XML Schemas(Trading Partners - B2B)

Supplier Consumer

P.O.

SchemaValidator

P.O.Schema

Softwareto process

P.O.

"P.O. isokay" P.O.

(Schema at third-party, neutral web site)

P.O = Purchase Order

Page 87: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

87

What are XML Schemas?

Data Model• With XML Schemas you specify how your XML data

will be organized, and the data types of your data. That is, with XML Schemas you model how your data is to be represented in an instance document.

A Contract• Organizations agree to structure their XML

documents in conformance with an XML Schema. Thus, the XML Schema acts as a contract between the organizations.

Page 88: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

88

XML Schemas Contain Metadata

An XML Schema document contains lots of data about

the data in the XML instance documents, such as

• the data type of the data,

• the data's range of values,

• how the data is related to another piece of data

(parent/child, sibling relationship)

Page 89: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

89

XML Schema GUI

P.O.Schema

GUI Builder

P.O.HTML

Supplier Web

Server

Page 90: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

90

XML Schema API

P.O.Schema

API Builder

P.O.API

Page 91: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

91

XML Schema Smart Editor

P.O.Schema

Smart Editor (e.g.,

XML Spy)

Helps you build yourinstance documents.For example, it popsup a menu showing you what is valid next.It knows this by lookingat the XML Schema!

Page 92: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

92

XML Schema

ValidateXML documents

AutomaticGUI generation

AutomaticAPI generation

Semantic Web???

Smart Editor

Do Lab 4

Page 93: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

93

Regular Expressions

Recall that the string data type has a pattern facet. The value of a pattern facet is a regular expression. Below are some examples of regular expressions:

Regular Expression

- Chapter \d - Chapter&#x020;\d - a*b - [xyz]b - a?b - a+b - [a-c]x

Example

- Chapter 1 - Chapter 1 - b, ab, aab, aaab, … - xb, yb, zb - b, ab - ab, aab, aaab, … - ax, bx, cx

Page 94: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Regular Expressions in XML Schema

• XML Schema has its own flavour of reg. exp.s

• limited features

• only for validation, not for extraction

• allow for efficient processing

(“text-directed engines” = det. fin. automata)

– disjunction only at top level

94

Page 95: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

95

Regular Expressions (cont.)Regular Expression

– [a-c]x– [-ac]x– [ac-]x– [^0-9]x– \Dx– Chapter\s\d

– (ho){2} there– (ho\s){2} there– .abc

Example– ax, bx, cx– -x, ax, cx– ax, cx, -x– any non-digit char followed by x– any non-digit char followed by x– Chapter followed by a blank

followed by a digit– hoho there– ho ho there– any (one) char followed by abc

Page 96: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

96

Regular Expressions (cont.)• a{1,3}x• a{2,}x• \w\s\w

• ax, aax, aaax

• aax, aaax, aaaax, …

• word character (alphanumeric plus dash) followed by a space followed by a word character

• [a-zA-Z-[Ol]]* • A string comprised of any lower and upper case letters, except "O" and "l"

• \. • The period "." (Without the backward slash the period means "any character")

Page 97: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

97

Regular Expressions (cont.)

• \n• \r• \t• \\• \|• \-• \^• \?• \*• \+• \(• \)

• linefeed• carriage return• tab• The backward slash \• The vertical bar |• The hyphen - • The caret ^• The question mark ?• The asterisk *• The plus sign +• The open paren (• The close paren )

Page 98: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

98

Regular Expressions

• \p{L}• \p{Lu}

• \p{Ll}• \p{N}• \p{Nd}• \p{P}• \p{Sc}

• A letter, from any language• An uppercase letter, from any

language• A lowercase letter, from any language• A number - Roman, fractions, etc• A digit from any language• A punctuation symbol• A currency sign, from any language

Page 99: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

99

Regular Expressions (concluded)<xsd:simpleType name="money"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\p{Sc}\p{Nd}+(\.\p{Nd}\p{Nd})?"/> </xsd:restriction></xsd:simpleType><xsd:element name="cost" type="money"/>

currency sign from any language, followed by one or more digits from any language, optionally followed by a period and two digits from any language

<cost>$45.99</cost><cost>¥300</cost>

Page 100: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

100

Example R.E.

[1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]

0 to 99 100 to 199 200 to 249 250 to 255

What does this mean?Where could it be used?

Page 101: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

101

IP Data Type Definition<xsd:simpleType name="IP"> <xsd:restriction base="xsd:string"> <xsd:pattern value="(([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3} ([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"> <xsd:annotation> <xsd:documentation> data type for representing IP addresses. Examples, 129.83.64.255, 64.128.2.71, etc. This data type restricts each field of the IP address to have a value between zero and 255, i.e., [0-255].[0-255].[0-255].[0-255] Note: in the value attribute (above) the regular expression has been split over two lines. This is for readability purposes only. In practice the R.E. would all be on one line. </xsd:documentation> </xsd:annotation> </xsd:pattern> </xsd:restriction></xsd:simpleType>

Page 102: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

102

Regex for different platform line breaks?

Different systems use different line break characters.

<?xml version="1.0"?> \r\n<Test> \r\n <para xml:space="preserve">This is a \r\n simple paragraph. What \r\n do you think of it?</para> \r\n</Test> \r\n

An XML parser "normalizes" all line breaks. After normalization the XML document looks like this:

<?xml version="1.0"?> \n<Test> \n <para xml:space="preserve">This is a \n simple paragraph. What \n do you think of it?</para> \n</Test> \n

Page 103: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

103

Regex for different platform line breaks?

Note:

1. All line breaks have been normalized to \n.

Consequence: you don't have to be concerned about different platformsusing different line break characters since all XML documents will havetheir line break characters normalized to \n regardless of theplatform. (So, if you're writing an XML Schema regex expression you cansimply use \n to indicate line break, regardless of the platform.)

2. The xml:space="preserve" attribute has no impact on line breaknormalization.

3. Suppose that you want a line break character in your XML document,other than \n. For example, suppose that you want \r in your XMLdocument. By default, it would get normalized to \n. To prevent this,use a character reference: &#xD;

Do Lab 5

Page 104: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

104

Derived Types

Two forms of subclassing complexType definitions

• by extension: extend the parent complexType with more elements

• by restriction: create a type that is a subset of the base type.

Two ways to subset the elements:

– redefine a base type element to have a restricted range of values, or

– redefine a base type element to have a more restricted number of occurrences

Page 105: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

105

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="BookPublication"> <xsd:complexContent> <xsd:extension base="Publication" > <xsd:sequence> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" type="BookPublication" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>

Note thatBookPublication extendsthe Publication type, i.e., we are doingDerive by Extension

(see example06)

Page 106: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

106

<xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence></xsd:complexType ><xsd:complexType name="BookPublication"> <xsd:complexContent> <xsd:extension base="Publication"> <xsd:sequence> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent></xsd:complexType >

Elements declared to be of type BookPublication will have 5 child elements - Title, Author, Date, ISBN, and Publisher. The elements in the derived type are

appended to the elements in the base type.

Page 107: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

107

Title

AuthorDatePublication

ISBN

PublisherBookPublication

Page 108: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

108

Publication

TitleAuthorDate

BookPublication

ISBNPublisher

"extends"

Do Lab 6

Page 109: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

109

Derive by Restriction<xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence></xsd:complexType><xsd:complexType name= "SingleAuthorPublication"> <xsd:complexContent> <xsd:restriction base="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:restriction> </xsd:complexContent></xsd:complexType>

Elements of type SingleAuthorPublication will have 3 child elements –

Title, Author, and Date.there must be exactly one Author element.

Page 110: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

110

Deleting an Element in the Base Type<xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string" minOccurs="0"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence></xsd:complexType><xsd:complexType name= "ZeroAuthorPublication"> <xsd:complexContent> <xsd:restriction base="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:restriction> </xsd:complexContent></xsd:complexType>

In this subtype we have eliminated the Author element, i.e., the subtype is just comprised of an unbounded number of Title elements followed by a single Date element.

If the base type has an element with minOccurs="0", and the subtype wishes to not have that element, then it can simply leave it out.

Page 111: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

111

Derive by Restriction (cont.)

• Question

– why do I have to repeat all the declarations from the base type? Why can't I simply show the delta (i.e., show those declarations that are changed)?

– What's the advantage of doing derived by restriction if I have to repeat everything? I'm certainly not saving on typing.

• Answer:

– Even though you have to retype everything in the base type there are advantages to explicitly associating a type with a base type. In a few slides we will see element substitution - the ability to substitute one element for another. A restriction of element substitution is that the substituting element have a type that derives from the type of the element it is substituting. Thus, it is beneficial to link the type.

– Also, later we will see that an element’s content model may be substituted by the content model of derived types. Thus, the content of an element that has been declared to be of type Publication can be substituted with a SingleAuthorPublication content since SingleAuthorPublication derives from Publication. We will discuss this type substitutability in detail later.

Page 112: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

112

Prohibiting Derivations

• Sometimes we may want to create a type and disallow all derivations of it, or just disallow extension derivations, or disallow restriction derivations.

• Rationale: "For example, I may create a complexType and make it publicly available for others to use. However, I don't want them to extend it with their proprietary extensions or subset it to remove, say, copyright information." (Jon Cleaver)

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

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

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

Page 113: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

113

Terminology: Declaration vs Definition

In a schema:

• You declare elements and attributes. Schema components that are declared are those that have a representation in an XML instance document.

• You define components that are used just within the schema document(s). Schema components that are defined are those that have no representation in an XML instance document.

Declarations:- element declarations- attribute declarations

Definitions:- type (simple, complex) definitions- attribute group definitions- model group definitions

Page 114: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

114

Global versus Local

• Global element declarations, global type definitions:– These are element declarations/type definitions

that are immediate children of <schema>

• Local element declarations, local type definitions:– These are element declarations/type definitions

that are nested within other elements/types.

Page 115: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

115<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="BookPublication"> <xsd:complexContent> <xsd:extension base="Publication" > <xsd:sequence> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" type="BookPublication" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>

Global type definition

Global type definition

Global element declaration

Local element declarationsLocal type definition

Page 116: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

116

Global vs Local … What's the Big Deal?

Question:

So what if an element or type is global or local.

What practical impact does it have?

Answer:

Only global elements/types can be referenced (i.e., reused).

Thus, if an element/type is local then it is effectively

invisible to the rest of the schema

(and to other schemas).

Page 117: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

117

Element SubstitutionOftentimes in daily conversation there are several ways

to express something.

• In Boston we use the words "T" and "subway" interchangeably. For example, "we took the T into town", or "we took the subway into town".

• Thus, "T" and "subway" are substitutable. Which one is used may depend upon what part of the state you live in, what mood you're in, or any number of factors.

• We would like to be able to express this substitutability in XML Schemas.

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

Page 118: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

118

substitutionGroup

We can define a group of substitutable elements

(called a substitutionGroup)

by declaring an element (called the head) and then

declaring other elements which state that they are substitutable for

the head element. <xsd:element name="subway" type="xsd:string"/> <xsd:element name="T" substitutionGroup="subway" type="xsd:string"/>

subway is the head elementT is substitutable for subway

Anywhere a head element can be used in an instance document, any member of the substitutionGroup can be substituted!

Page 119: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

119

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

<transportation> <subway>Red Line</subway> </transportation>

Schema:

Instance doc:

<transportation> <T>Red Line</T> </transportation>

Alternative instance doc(substitute Tfor subway):

This example shows the <subway> element being substituted with the <T> element.

Page 120: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

120

International Clients

• We can use substitutionGroups to create elements customized for our international clients.

On the next slide is shown a Spanish version of the element.

Page 121: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

121

<xsd:element name="subway" type="xsd:string"/> <xsd:element name="metro" substitutionGroup="subway" type="xsd:string"/> <xsd:complexType name="transport"> <xsd:sequence> <xsd:element ref="subway"/> </xsd:sequence> </xsd:complexType> <xsd:element name="transportation" type="transport"/> <xsd:element name="transporte" substitutionGroup="transportation"/>

<transportation> <subway>Red Line</subway> </transportation>

Schema:

Instance doc:

<transportation> <metro>Linea Roja</metro></transportation>

Alternative instance doc(customizedfor Spanish clients):

Page 122: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

122

Notes About Using substitutionGroup

• The elements that are declared to be in the substitution group (e.g., subway and T) must be declared as global elements

• If the type of a substitutionGroup element is the same as the head element then you can omit it (the type)

In our Subway example we could have omitted the type attribute in the declaration of the T element since it is the same as Subway’s type (xsd:string).

<xsd:element name="T" substitutionGroup="subway"/>

Page 123: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

123

Notes about using substitutionGroup (cont.)

• The type of every element in the substitutionGroup must be the same as, or derived from, the type of the head element.

<xsd:element name="A" type="xxx"/>

<xsd:element name="B" substitutionGroup="A" type="yyy"/>

This type must be the same as "xxx" or,it must be derived from "xxx".

Page 124: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

124

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

Element Substitution with Derived Types

Page 125: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

125

BookType and MagazineType Derive from PublicationType

PublicationType

BookType MagazineType

In order for Book and Magazine to be in a substitutionGroup withPublication, their type (BookType and MagazineType, respectively)must be the same as, or derived from Publication's type (PublicationType)

Page 126: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

126

<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="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:restriction> </xsd:complexContent></xsd:complexType>

Page 127: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

127<?xml version="1.0"?><BookStore …> <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> <Magazine> <Title>Natural Health</Title> <Date>1999</Date> </Magazine> <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>

<BookStore> can contain any element in the substitutionGroup with Publication!

Page 128: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

128

Blocking Element Substitution

• An element may wish to block other elements from substituting with it.

• This is achieved by adding a block attribute.

<xsd:element name="…" type="…" block="substitution"/>

Page 129: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

129

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

<transportation> <subway>Red Line</subway> </transportation>

Schema:

Instance doc:

<transportation> <T>Red Line</T> </transportation>

Not allowed!

There is no error in declaring T to be substitutable with subway.The error occurs only when you try to do substitution in the instance document.

Page 130: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

130

One More Note About substitutionGroup

1. Transitive: If element A can substitute for element B, and element B can substitute for element C, then element A can substitute for element C.

A --> B --> C then A --> C

2. Non-symmetric: If element A can substitute for element B, it is not the case that element B can substitute for element A.

Do Lab 7

Page 131: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

131

Attributes

• On the next slide we see a version of the BookStoreDTD that uses attributes.

• Then, on the following slide we see how this is implemented using XML Schemas.

Page 132: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

132

<!ELEMENT BookStore (Book+)><!ELEMENT Book (Title, Author, Date, ISBN, Publisher)><!ATTLIST Book Category (autobiography | non-fiction | fiction) #REQUIRED InStock (true | false) "false" Reviewer CDATA " "><!ELEMENT Title (#PCDATA)><!ELEMENT Author (#PCDATA)><!ELEMENT Date (#PCDATA)><!ELEMENT ISBN (#PCDATA)><!ELEMENT Publisher (#PCDATA)>

BookStore.dtd

Page 133: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

133 <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:attributeGroup ref="BookAttributes"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:attributeGroup name="BookAttributes"> <xsd:attribute name="Category" use="required"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="autobiography"/> <xsd:enumeration value="non-fiction"/> <xsd:enumeration value="fiction"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> <xsd:attribute name="InStock" type="xsd:boolean" default="false"/> <xsd:attribute name="Reviewer" type="xsd:string" default=" "/> </xsd:attributeGroup>

(see example07)

Reviewer CDATA " "

InStock (true | false)

"false"

Category (autobiography | non-fiction | fiction) #REQUIRED

Page 134: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

134

<xsd:attribute name="Category" use="required"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="autobiography"/> <xsd:enumeration value="non-fiction"/> <xsd:enumeration value="fiction"/> </xsd:restriction> </xsd:simpleType></xsd:attribute>

• “Instance documents are required to have the Category attribute(as indicated by use="required").”

• “The value of Category must be either

autobiography, non-fiction, or fiction

(as specified by the enumeration facets)."

Note: attributes can only have simpleTypes (i.e., attributes cannot have child

elements).

Page 135: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

135

Summary of Declaring Attributes (2 ways to do it)

<xsd:attribute name="name" type="simple-type" use="how-its-used"

default/fixed="value"/>

xsd:stringxsd:integerxsd:boolean...

1

requiredoptionalprohibited

The "use" attribute must beoptional if you usedefault or fixed.

Page 136: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

136

Summary of Declaring Attributes (2 ways to do it)

2

<xsd:attribute name="name" use="how-its-used" default/fixed="value"> <xsd:simpleType> <xsd:restriction base="simple-type"> <xsd:facet value="value"/> … </xsd:restriction> </xsd:simpleType></xsd:attribute>

Page 137: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

137

use="prohibited"Question: When would use="prohibited" be used? Answer: Useful when you create a master type that lists all possible attributes, and then subtypes can delete the attributes that are not needed.

<xsd:complexType name="box"> <xsd:complexContent> <xsd:restriction base="shape"> <xsd:attribute name="length" type="xsd:nonNegativeInteger"/> <xsd:attribute name="height" type="xsd:nonNegativeInteger"/> <xsd:attribute name="width" type="xsd:nonNegativeInteger"/> <xsd:attribute name="radius" type="xsd:nonNegativeInteger" use="prohibited"/> <xsd:attribute name="diameter" type="xsd:nonNegativeInteger" use="prohibited"/> </xsd:restriction> </xsd:complexContent></xsd:complexType>

<xsd:complexType name="shape"> <xsd:attribute name="length" type="xsd:nonNegativeInteger"/> <xsd:attribute name="height" type="xsd:nonNegativeInteger"/> <xsd:attribute name="width" type="xsd:nonNegativeInteger"/> <xsd:attribute name="radius" type="xsd:nonNegativeInteger"/> <xsd:attribute name="diameter" type="xsd:nonNegativeInteger"/></xsd:complexType>

Page 138: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

138

use Use it Only with Local Attribute Declarations

• The "use" attribute only makes sense in the context of an element declaration.

Example: “For each Book element, the Category attribute is required".

• When declaring a global attribute do not specify a "use"

Page 139: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

139

<xsd:element name="Book"> <xsd:complexType> <xsd:sequence> … </xsd:sequence> <xsd:attribute ref="Category" use="required"/> … </xsd:complexType></xsd:element><xsd:attribute name="Category"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="autobiography"/> <xsd:enumeration value="fiction"/> <xsd:enumeration value="non-fiction"/> </xsd:restriction> </xsd:simpleType></xsd:attribute>

Local attribute declaration. Use the “use” attribute here.

Global attribute declaration. Must NOT have a "use" (“use” only makes sense inthe context of an element)

Page 140: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

140

Inlining Attributes

There another way of expressing the last example• the attributes are inlined within the Book declaration• rather than being separately defined in an

attributeGroup

Page 141: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

141

(see example08)

<xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> <xsd:attribute name="Category" use="required"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="autobiography"/> <xsd:enumeration value="non-fiction"/> <xsd:enumeration value="fiction"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> <xsd:attribute name="InStock" type="xsd:boolean" default="false"/> <xsd:attribute name="Reviewer" type="xsd:string" default=" "/> </xsd:complexType></xsd:element>

Page 142: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

142

Notes about Attributes

• The attribute declarations always come last, after the element declarations.

• The attributes are always with respect to the element that they are defined (nested) within.

<xsd:element name="foo"> <xsd:complexType> <xsd:sequence> … </xsd:sequence> <xsd:attribute name="bar" …/> <xsd:attribute name="boo" …/> </xsd:complexType></xsd:element>

"bar and boo areattributes of foo"

Page 143: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

143

<xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> <xsd:attribute name="Category" use="required"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="autobiography"/> <xsd:enumeration value="non-fiction"/> <xsd:enumeration value="fiction"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> <xsd:attribute name="InStock" type="xsd:boolean" default="false"/> <xsd:attribute name="Reviewer" type="xsd:string" default=" "/> </xsd:complexType></xsd:element>

Do Lab 8.a,

These attributesapply to the element they are nested within (Book)That is, Book has threeattributes – Category, InStock, and Reviewer.

Page 144: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

144

Example.

<elevation units="feet">5440</elevation>

The elevation element has these two constraints: - it has a simple (integer) content - it has an attribute called units

How do we declare elevation?

Element with Simple Content and Attributes

Page 145: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

145

<xsd:element name="elevation"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="xsd:integer"> <xsd:attribute name="units" type="xsd:string" use="required"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType></xsd:element>

1. elevation contains an attribute — therefore, we must use <xsd:complexType>

2. However, elevation does not contain child elements (which is what we generally use <complexType> to indicate). Instead, elevation contains simpleContent.

3. We wish to extend the simpleContent (an integer) ...4. … with an attribute.

12

3

4

Page 146: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

146

Elevation: Use Stronger Data Type

• In the declaration for elevation we allowed it to hold any

integer. Further, we allowed the units attribute

to hold any string.

• Let's restrict elevation to hold – an integer with a range 0 12,000 and – let's restrict units to hold either

the string “feet” or the string “meters”

Page 147: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

147

<xsd:simpleType name="elevationRange"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="0"/> <xsd:maxInclusive value="12000"/> </xsd:restriction></xsd:simpleType>

<xsd:simpleType name="unitsType"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="feet"/> <xsd:enumeration value="meters"/> </xsd:restriction></xsd:simpleType>

<xsd:element name="elevation"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="elevationRange"> <xsd:attribute name="units" type="unitsType" use="required"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType></xsd:element>

Page 148: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

148

Equivalent!<xsd:element name="elevation"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="elevationRange"> <xsd:attribute name="units" type="unitsType" use="required"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType></xsd:element>

<xsd:complexType name="elevationType"> <xsd:simpleContent> <xsd:extension base="elevationRange"> <xsd:attribute name="units" type="unitsType" use="required"/> </xsd:extension> </xsd:simpleContent></xsd:complexType><xsd:element name="elevation" type="elevationType"/>

Page 149: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

149

Extending simpleContent

<xsd:complexType name="SurfaceElevation"> <xsd:simpleContent> <xsd:extension base="EarthSurfaceElevation"> <xsd:attribute name="units" type="xsd:string" fixed="feet"/> </xsd:extension> </xsd:simpleContent></xsd:complexType><xsd:element name="DenverElevation" type="SurfaceElevation"/>

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

<DenverElevation units="feet">______</DenverElevation

value must be between -1290 and 29035

Page 150: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

150

Restricting simpleContent

Value space: -1290 to 29035

Attribute: units (fixed at “feet”)

SurfaceElevation

restrict the value space

Value space: 0 to 120

Attribute: units (fixed at “feet”)

BostonAreaSurfaceElevation

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

<xsd:simpleType name="BostonAreaEarthSurfaceElevation"> <xsd:restriction base="EarthSurfaceElevation"> <xsd:minInclusive value="0"/> <xsd:maxInclusive value="120"/> </xsd:restriction></xsd:simpleType>

Page 151: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

151

Restricting simpleContent<xsd:complexType name="BostonAreaSurfaceElevation"> <xsd:simpleContent> <xsd:restriction base="SurfaceElevation"> <xsd:simpleType> <xsd:restriction base="BostonAreaEarthSurfaceElevation"/> </xsd:simpleType> </xsd:restriction> </xsd:simpleContent></xsd:complexType><xsd:element name="BostonElevation" type="BostonAreaSurfaceElevation"/>

<BostonElevation units="feet">______</BostonElevation

value must be between 0 and 120(see example08.1)

Page 152: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

152

Extending simpleContent vs Restricting simpleContent

<xsd:complexType name="..."> <xsd:simpleContent> <xsd:extension base="Y1"> Z1 </xsd:extension> </xsd:simpleContent></xsd:complexType>

<xsd:complexType name="..."> <xsd:simpleContent> <xsd:restriction base="Y2"> Z2 </xsd:restriction> </xsd:simpleContent></xsd:complexType>

Y1 must be a simpleType (either built-in or user-defined).Z1 must be one or more attribute declarations.

Y2 must be a complexType with simpleContent.Z2 must be a simpleType (either built-in or user-defined).

Page 153: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

153

1. Element with simple content

Declaring an element using a built-in type:

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

Declaring an element using a user-defined simpleType:

<xsd:simpleType name="shapes"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="triangle"/> <xsd:enumeration value="rectangle"/> <xsd:enumeration value="square"/> </xsd:restriction> </xsd:simpleType>

<xsd:element name="geometry" type="shapes"/>

Declaring Elements: Summary

Page 154: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

154

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

<xsd:element name="geometry"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="triangle"/> <xsd:enumeration value="rectangle"/> <xsd:enumeration value="square"/> </xsd:restriction> </xsd:simpleType> </xsd:element>

Declaring Elements: Summary

Page 155: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

155

Declaring Elements: Summary2. Element contains child elements

Defining the child elements inline:

<xsd:element name="Person">

<xsd:complexType>

<xsd:sequence>

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

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

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

</xsd:sequence>

</xsd:complexType>

</xsd:element>

Page 156: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

156

Declaring Elements: SummaryAn alternate formulation of the above Person example is

to create a named complexType and then use that type:

<xsd:complexType name="PersonType">

<xsd:sequence>

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

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

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

</xsd:sequence>

</xsd:complexType>

<xsd:element name="Person" type="PersonType"/>

Page 157: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

157

Declaring Elements: Summary

<xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:complexType>

<xsd:complexType name="BookPublication"> <xsd:complexContent> <xsd:extension base="Publication" > <xsd:sequence> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType>

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

3. Element contains a complexType that is an extension of another complexType

Page 158: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

158

Declaring Elements: Summary

<xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence></xsd:complexType>

<xsd:complexType name= "SingleAuthorPublication"> <xsd:complexContent> <xsd:restriction base="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:restriction> </xsd:complexContent></xsd:complexType>

<xsd:element name="Catalogue" type="SingleAuthorPublication"/>

4. Element contains a complexType that is a restriction of another complexType

Page 159: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

159

Declaring Elements: Summary

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

5. Element Contains Simple Content and Attributes

<apple variety="Cortland">Large, green, sour</apple>

Example.

Page 160: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

160

complexContent vs simpleContent

• With complexContent you extend or restrict a complexType

• With simpleContent you extend or restrict a simpleType

<xsd:complexType name="…"> <xsd:complexContent> <xsd:extension base="X"> … </xsd:extension> </xsd:complexContent></xsd:complexType>

X must be a complexType

<xsd:complexType name="…"> <xsd:simpleContent> <xsd:extension base="Y"> … </xsd:extension> </xsd:simpleContent></xsd:complexType>

Y must be a simpleType

vs.

Page 161: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

161

group Element

• The group element enables you to group together element declarations.

• Note: the group element is just for grouping together element declarations, no attribute declarations allowed!

Page 162: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

162

<xsd:element name="Book" > <xsd:complexType> <xsd:sequence> <xsd:group ref="PublicationElements"/> <xsd:element name="ISBN" type="string"/> <xsd:element name="Reviewer" type="string"/> </xsd:sequence> </xsd:complexType></xsd:element><xsd:element name="CD" > <xsd:complexType> <xsd:sequence> <xsd:group ref="PublicationElements"/> <xsd:element name="RecordingStudio" type="string"/> </xsd:sequence> </xsd:complexType></xsd:element><xsd:group name="PublicationElements"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:string"/> </xsd:sequence></xsd:group>

An example showing the use of the <group> element

Page 163: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

163

Group definitions must be global<xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:group name="PublicationElements"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" minOccurs="0"/> <xsd:element name="Author" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:string"/> </xsd:sequence> </xsd:group> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> ... </xsd:complexType> </xsd:element> One cannot inline the group definition.

Instead, one must use a ref here and define the group globally.

Page 164: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

164

Expressing Alternates<!ELEMENT transportation (train | plane | automobile)>DTD:

XML Schema:

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.travel.org" xmlns="http://www.travel.org" elementFormDefault="qualified"> <xsd:element name="transportation"> <xsd:complexType> <xsd:choice> <xsd:element name="train" type="xsd:string"/> <xsd:element name="plane" type="xsd:string"/> <xsd:element name="automobile" type="xsd:string"/> </xsd:choice> </xsd:complexType> </xsd:element></xsd:schema>

(see example10)

Note: the choice is an exclusive-or, that is, transportation can containonly one element — either train, or plane, or automobile.

Page 165: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

165

Repeatable Choice

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.binary.org" xmlns="http://www.binary.org" elementFormDefault="qualified"> <xsd:element name="binary-string"> <xsd:complexType> <xsd:choice minOccurs="0" maxOccurs="unbounded"> <xsd:element name="zero" type="xsd:unsignedByte" fixed="0"/> <xsd:element name="one" type="xsd:unsignedByte" fixed="1"/> </xsd:choice> </xsd:complexType> </xsd:element></xsd:schema>

<!ELEMENT binary-string (zero | one)*>DTD:

XML Schema:

1. An element can fix its value, using the fixed attribute.2. When you don't specify a value for minOccurs, it defaults to "1". Same for maxOccurs. See the transportation example.

(see example 11)

Page 166: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

166

fixed/default Element Values

When you declare an element you can give it a fixed or default value.

In the instance document, you can leave the element empty.

<element name="zero" fixed="0"/>

…<zero>0</zero>

or equivalently:

<zero/>

<element name="color" default="red"/>…<color>red</color>

or equivalently:

<color/>

Page 167: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

167

Using <sequence> and <choice>

<xsd:element name="life"> <xsd:complexType> <xsd:sequence minOccurs="0" maxOccurs="unbounded"> <xsd:sequence minOccurs="0" maxOccurs="unbounded"> <xsd:element name="work" type="xsd:string"/> <xsd:element name="eat" type="xsd:string"/> </xsd: sequence> <xsd:choice> <xsd:element name="work" type="xsd:string"/> <xsd:element name="play" type="xsd:string"/> </xsd:choice> <xsd:element name="sleep" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element>

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

XML Schema:

Page 168: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

168

Expressing Any Order

<xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:all> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:all> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element>

XML Schema:

Create an element, Book, which contains Author, Title, Date, ISBN, Publisher,in any order (This is very difficult and ugly with DTDs).

<all> means that Book must contain all five child elements, but they may occur in any order (see example 12)

Page 169: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

169

Constraints on using <all>

• Elements declared within <all> must have a maxOccurs value of "1"

(minOccurs can be either "0" or "1")

• If a complexType uses <all> and it extends another type, then that parent type must have empty content.

• The <all> element cannot be nested within either <sequence>, <choice>, or another <all>

• The contents of <all> must be just elements. It cannot contain <sequence> or <choice>

Do Lab 9

Page 170: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

170

Empty Element

<xsd:element name="gallery"> <xsd:complexType> <xsd:sequence> <xsd:element name="image" maxOccurs="unbounded"> <xsd:complexType> <xsd:attribute name="href" type="xsd:anyURI" use="required"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element>

<image href="http://www.xfront.com/InSubway.gif"/>

Schema:

Instancedoc

(snippet):Do Lab 10

<!ELEMENT image EMPTY><!ATTLIST image href CDATA #REQUIRED>

DTD:

(see example 13)

Page 171: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Summary of Defining simpleTypes

1. simpleType that uses a built-in base type:

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

Page 172: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Summary of Defining simpleTypes

2. simpleType that uses another simpleType as the base type:

<xsd:simpleType name= "BostonSurfaceElevation"> <xsd:restriction base="EarthSurfaceElevation"> <xsd:minInclusive value="0"/> <xsd:maxInclusive value="120"/> </xsd:restriction></xsd:simpleType>

Page 173: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Summary of Defining simpleTypes

3. simpleType that defines a list type:

<xsd:simpleType name= "LotteryNumbers"> <xsd:list itemType="OneToNinetyNine"/></xsd:simpleType>

where the data type OneToNinetyNine is declared as:

<xsd:simpleType name= "OneToNinetyNine"> <xsd:restriction base="xsd:positiveInteger"> <xsd:maxInclusive value="99"/> </xsd:restriction></xsd:simpleType>

Page 174: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Summary of Defining simpleTypes

4. An alternate form of the previous, where the list's data type is specified using an inlined simpleType:

<xsd:simpleType name= "LotteryNumbers"> <xsd:list> <xsd:simpleType> <xsd:restriction base="xsd:positiveInteger"> <xsd:maxInclusive value="99"/> </xsd:restriction> </xsd:simpleType> </xsd:list></xsd:simpleType>

Page 175: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Summary of Defining simpleTypes

5. simpleType that defines a union type:

<xsd:simpleType name= "maxOccurs"> <xsd:union memberTypes="xsd:nonNegativeInteger UnboundedType"/></xsd:simpleType>

where the data type UnboundedType is declared as:

<xsd:simpleType name= "UnboundedType"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="unbounded"/> </xsd:restriction></xsd:simpleType>

Page 176: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Summary of Defining simpleTypes

6. An alternate form of the previous, where the data type UnboundedType is specified using an inline simpleType:

<xsd:simpleType name= "maxOccurs"> <xsd:union memberTypes="xsd:nonNegativeInteger"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="unbounded"/> </xsd:restriction> </xsd:simpleType> </xsd:union></xsd:simpleType>

Page 177: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Uniqueness and Keys

DTDs provide the ID attribute data type for uniqueness

XML Schema enables one to:• define element content to be unique • define non-ID attributes to be unique • define a combination of element content and attributes to

be unique• distinguish between unique versus key

• declare the range of the document over which something is unique

Page 178: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

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

nil in XML Schema is like null in SQL

Page 179: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Example: 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 180: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

<?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 181: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

<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 the <Book> elements, and for each <Book> the ISBN element is a key.”

• In other words:within <BookStore>, each <Book> must have an <ISBN> and it must be unique.

• Note: We are using the content of a field as a key!

No longer limited to ID attributes!

Page 182: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

<?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 183: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Notes about <key>

• A key must be nested within an <element>

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

• The <key> element has a <selector> child,which selects the set of elements

for which the key applies.

• The <key> element has a <field> child,that identifies the element or attribute

that is to be the key

There can be multiple <field> elements.

Page 184: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

<?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="FamilyReunion"> <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.

Page 185: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

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

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

Page 186: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

xPath Expressions must be Namespace-qualified

(when elementFormDefault="qualified")

Note: we namespace-qualified the xPath references:

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

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

Note: If the schema had instead set elementFormDefault ="unqualified",

then the xPath expressions would not be namespace-qualified.

Page 187: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Uniqueness Constraints

• The element

<unique>

is used exactly like the <key> element.

It has – a <selector>– one or more <field> elements

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

Page 188: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" xmlns:bk="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string" minOccurs="0"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:unique name="UNIQ"> <xsd:selector xpath="bk:Book"/> <xsd:field xpath="bk:ISBN"/> </xsd:unique> </xsd:element></xsd:schema>

Note: ISBNis optional

RequireISBNsto be unique

Page 189: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

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

A schema-validatorwill verify that each Book that has an ISBN element has a unique value (note that the first Bookdoes not have an ISBN)

Page 190: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Referencing a key

• In DTDs, by declaring an attribute of type IDREF,that attribute must reference an ID attribute

• An XML Parser will verify that the IDREF value corresponds to a legitimate ID value

• Similarly, one can define a keyref constraint, which asserts,

“the value of this element must match the value of an element referred to by this“

Referential Integrity

Page 191: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

<?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 we define ISBN to be akey for Book

We want to ensure that the ISBN for the GuestAuthor matches one of the ISBNs in the BookStore

key element

A keyref element

Page 192: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

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

What are keyrefscalled in relational databases?

Page 193: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

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

Tells the schema-validator to validate that every Book (in BookStore) has an ISBN, and the ISBN is 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 194: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

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 the keyin type and position

Page 195: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Specifying scope of uniqueness in XML Schemas

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

• Where they are placed 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 196: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

nil Content

<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 can be a NMTOKEN value orits content can be undefined.

Empty content vs nil:• Empty: an element with an empty content never has content• nil: an instance document element may indicate no value is available

by setting an attribute - xsi:nil - equal to 'true'

Page 197: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

No targetNamespace(noNamespaceSchemaLocation)

• Sometimes you may wish to create a schema but without associating the elements with a namespace.

• The targetNamespace attribute is actually an optional attribute of <schema>. Thus, if you don’t want to specify a namespace for your schema then simply don’t use the targetNamespace attribute.

• Consequences of having no namespace– In the instance document don’t namespace qualify

the elements.– In the instance document, instead of using

schemaLocation use noNamespaceSchemaLocation.

Page 198: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Title"/> <xsd:element ref="Author"/> <xsd:element ref="Date"/> <xsd:element ref="ISBN"/> <xsd:element ref="Publisher"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/></xsd:schema>

(see example14)

There is notargetNamespaceattribute, and there is no longer a default namespace.

Page 199: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

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

(see example14)

1. There is no default namespace declaration.

So, none of the elements are associated with a namespace.

2. We do not use xsi:schemaLocation (since it requires a pair of values –

a namespace and a URL to the schema for that namespace).

Instead, we use xsi:noNamespaceSchemaLocation.

Page 200: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Assembling an Instance Document from Multiple Schema Documents

• An instance document may be composed of elements from multiple schemas.

• Validation can apply to the entire XML instance document, or to a single element.

Page 201: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

<?xml version="1.0"?><Library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.book.org Book.xsd http://www.employee.org Employee.xsd"> <Books> <Book xmlns="http://www.book.org"> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <ISBN>1-56592-235-2</ISBN> <Publisher>Macmillan Publishing</Publisher> </Book> </Books> <Employees> <Employee xmlns="http://www.employee.org"> <Name>John Doe</Name> <SSN>123-45-6789</SSN> </Employee> <Employee xmlns="http://www.employee.org"> <Name>Sally Smith</Name> <SSN>000-11-2345</SSN> </Employee> </Employees></Library> Library.xml

(see example 15)

Validating againsttwo schemas

The <Book> elements aredefined in Book.xsd, andthe <Employee> elementsare defined in Employee.xsd.The <Library>, <Books>,and <Employees> elementsare not defined in any schema!

1. A schema validator will validate each Book elementagainst Book.xsd. 2. It will validate each Employee element against Employee.xsd.3. It will not validate the otherelements.

Page 202: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Lax vs Strict Validation

• On the previous slide there were elements (Library,

Books, and Employees) for which there was no schema

to validate against.

• Lax validation is where the schema validator skips over

elements for which no schema is available.

• Strict validation is where the schema validator requires

validation of every element

• Most validators perform strict validation,

while some can be told to perform lax validation.

Page 203: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Assembling a Schema from Multiple Schema Documents

The include element allows you to access components in other schemas• All the schemas you include must have the same namespace as your

schema (i.e., the schema that is doing the include)• The net effect of include is as though you had typed all the definitions

directly into the containing schema

<xsd:schema …> <xsd:include schemaLocation="LibraryBook.xsd"/> <xsd:include schemaLocation="LibraryEmployee.xsd"/> …</xsd:schema>

LibraryBook.xsd LibraryEmployee.xsd

Library.xsd

Page 204: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.library.org" xmlns="http://www.library.org" elementFormDefault="qualified"> <xsd:include schemaLocation="LibraryBook.xsd"/> <xsd:include schemaLocation="LibraryEmployee.xsd"/> <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 name="Employees"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Employee" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>

Library.xsd (see example 16)

These arereferencingelementdeclarationsin the otherschemas.

Page 205: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Assembling a Schema from a Schema with no targetNamespace

• A schema can <include> another schema which has no targetNamespace

• The included components take on the targetNamespace of the schema that is doing the <include>

• This is called the Chameleon Effect • The components in the no-namespace schema are

called Chameleon components

Page 206: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xsd:complexType name="ProductType"> <xsd:sequence> <xsd:element name="Type" type="xsd:string"/> </xsd:sequence> </xsd:complexType></xsd:schema>

Product.xsd (see example17)

This schema has no targetNamespace!

Page 207: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.company.org" xmlns="http://www.company.org" elementFormDefault="qualified"> <xsd:include schemaLocation="Person.xsd"/> <xsd:include schemaLocation="Product.xsd"/> <xsd:element name="Company"> <xsd:complexType> <xsd:sequence> <xsd:element name="Person" type="Person" maxOccurs="unbounded"/> <xsd:element name="Product" type="ProductType" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>

Company.xsd (see example17)

This schema <include>s Product.xsd. Thus, the components in Product.xsd are namespace-coercedto the company targetNamespace. Consequently, we can reference those components just as thoughthey had originally been declared in a schema with the same targetNamespace.

Page 208: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Assembling a Schema from Multiple Schema Documents with Different Namespaces

The import element allows you to access elements and types in a different namespace

<xsd:schema …> <xsd:import namespace="A" schemaLocation="A.xsd"/> <xsd:import namespace="B" schemaLocation="B.xsd"/> …</xsd:schema>

NamespaceA

A.xsd

NamespaceB

B.xsd

C.xsd

Page 209: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Camera Schema

Camera.xsd

Nikon.xsd Olympus.xsd Pentax.xsd

Page 210: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.nikon.com" xmlns="http://www.nikon.com" elementFormDefault="qualified"> <xsd:complexType name="body_type"> <xsd:sequence> <xsd:element name="description" type="xsd:string"/> </xsd:sequence> </xsd:complexType></xsd:schema>

Nikon.xsd

Page 211: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.olympus.com" xmlns="http://www.olympus.com" elementFormDefault="qualified"> <xsd:complexType name="lens_type"> <xsd:sequence> <xsd:element name="zoom" type="xsd:string"/> <xsd:element name="f-stop" type="xsd:string"/> </xsd:sequence> </xsd:complexType></xsd:schema>

Olympus.xsd

Page 212: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.pentax.com" xmlns="http://www.pentax.com" elementFormDefault="qualified"> <xsd:complexType name="manual_adapter_type"> <xsd:sequence> <xsd:element name="speed" type="xsd:string"/> </xsd:sequence> </xsd:complexType></xsd:schema>

Pentax.xsd

Page 213: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.camera.org" xmlns:nikon="http://www.nikon.com" xmlns:olympus="http://www.olympus.com" xmlns:pentax="http://www.pentax.com" elementFormDefault="qualified"> <xsd:import namespace="http://www.nikon.com" schemaLocation="Nikon.xsd"/> <xsd:import namespace="http://www.olympus.com" schemaLocation="Olympus.xsd"/> <xsd:import namespace="http://www.pentax.com" schemaLocation="Pentax.xsd"/> <xsd:element name="camera"> <xsd:complexType> <xsd:sequence> <xsd:element name="body" type="nikon:body_type"/> <xsd:element name="lens" type="olympus:lens_type"/> <xsd:element name="manual_adapter" type="pentax:manual_adapter_type"/> </xsd:sequence> </xsd:complexType> </xsd:element><xsd:schema>

Camera.xsd (see example 18)

These importelements giveus access tothe componentsin these otherschemas.

Here we are using the body_type that is defined in the Nikon namespace

Page 214: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

<?xml version="1.0"?><c:camera xmlns:c="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"> <c:body> <nikon:description>Ergonomically designed casing for easy handling </nikon:description> </c:body> <c:lens> <olympus:zoom>300mm</olympus:zoom> <olympus:f-stop>1.2</olympus:f-stop> </c:lens> <c:manual_adapter> <pentax:speed>1/10,000 sec to 100 sec</pentax:speed> </c:manual_adapter></c:camera>

Camera.xml

Page 215: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

any Element

The <any> element enables the instance document author to extend his/her document with elements not specified by the schema.

<xsd:element name="Book"> <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:any minOccurs="0"/> </xsd:sequence> </xsd:complexType></xsd:element>

Now an instance document author can optionally extend (after <Publisher>) the content of <Book> elements with any element.

Page 216: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

<?xml version="1.0"?><BookStore xmlns="http://www.BookRetailers.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www. BookRetailers.org BookSeller.xsd http://www.repository.org SchemaRepository.xsd"> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <ISBN>94303-12021-43892</ISBN> <Publisher>McMillin Publishing</Publisher> <Reviewer xmlns="http://www.repository.org"> <Name> <First>Roger</First> <Last>Costello</Last> </Name> </Reviewer> </Book></BookStore>

This instance document uses components from two different schemas.

Page 217: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Extensible Instance Documents

• The <any> element enables instance document authors to create instance documents containing elements above and beyond what was specified by the schema.

• The instance documents are said to be extensible. Contrast this schema with previous schemas where the content of all our elements were always fixed and static.

• We are empowering the instance document author with the ability to define what data makes sense to him/her!

Page 218: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Mixed Content

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

• Mixed content has many applications.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> The content of

the xsl:template element is a mix of string data and elements.

Page 219: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

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 220: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

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

finding your tool <emp> very </emp> useful. </Body></Letter>

Letter.xml (see example36)

Page 221: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

<?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 222: 1 XML Schemas Modified version of: Roger L. Costello XML Technologies Course (Part 1)  (Structures)

Limitations of XML Schema

Some conditions cannot be expressed in XML Schema:• 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• Ensure that the value of the <PaymentReceived>

is equal to the value of <PaymentDue>

What have these examples in common?