38
2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1 Introduction 7.2 Schemas vs. DTDs 7.3 Microsoft XML Schema: Describing Elements 7.4 Microsoft XML Schema: Describing Attributes 7.5 Microsoft XML Schema: Data types 7.6 W3C XML Schema 7.7 Case Study: Writing a Microsoft XML Schema for the Day Planner Application

2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

Embed Size (px)

Citation preview

Page 1: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

1

Chapter 7 – Schemas

Outline7.1 Introduction7.2 Schemas vs. DTDs7.3 Microsoft XML Schema: Describing Elements7.4 Microsoft XML Schema: Describing Attributes7.5 Microsoft XML Schema: Data types7.6 W3C XML Schema7.7 Case Study: Writing a Microsoft XML Schema for the Day Planner Application

Page 2: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

2

7.1 Introduction

• DTDs– Not flexible enough to meet certain programming needs

• Cannot be manipulated (searched, transformed, etc.)

• Not XML documents

• Schemas– Alternative to DTDs

– XML documents (using XML syntax)

– Two major models• W3C XML Schema

– Early development stage (at time of this writing)

• Microsoft XML Schema

Page 3: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

3

7.2 Schema vs. DTDs

• DTDs– Cannot ensure proper element content

<quantity>hello</quantity> is valid

– Use EBNF grammar

• Schema– Can ensure proper element content

– Use XML syntax

Page 4: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

4

7.3 Microsoft XML Schema: Describing Elements

• Elements– Primary building blocks for creating XML documents

• Microsoft XML Schema

– Element ElementType defines an element

» Contains attributes that describe element’s content

Page 5: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

Outline5

Fig. 7.1 Microsoft XML Schema document.

Element Schema is Microsoft XML root element

Microsoft XML namespace URI

Define element message

Define element greeting

Define element myMessage

Element myMessage must contain any number of message elements (but at least one)

1 <?xml version = "1.0"?>

2

3 <!-- Fig. 7.2 : intro-schema.xml -->

4 <!-- Microsoft XML Schema showing the ElementType -->

5 <!-- element and element element -->

6

7 <Schema xmlns = "urn:schemas-microsoft-com:xml-data">

8 <ElementType name = "message" content = "textOnly"

9 model = "closed">

10 <description>Text messages</description>

11 </ElementType>

12

13 <ElementType name = "greeting" model = "closed"

14 content = "mixed" order = "many">

15 <element type = "message"/>

16 </ElementType>

17

18 <ElementType name = "myMessage" model = "closed"

19 content = "eltOnly" order = "seq">

20

21 <element type = "greeting" minOccurs = "0"

22 maxOccurs = "1"/>

23 <element type = "message" minOccurs = "1"

24 maxOccurs = "*"/>

25

26 </ElementType>

27 </Schema>

Element Schema is Microsoft XML root element

Microsoft XML namespace URI

Define element message to contain only text

Define element greeting to contain both elements and character data

Define element myMessage to contain only elements

Element myMessage must contain any number of message

elements (but at least one)

Page 6: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

Outline6

Fig. 7.2 XML document that conforms to intro-schema.xml.

Reference intro-schema.xml through namespace declaration

Element greeting marks up text and message element

1 <?xml version = "1.0"?>

2

3 <!-- Fig. 7.2 : intro.xml -->

4 <!-- Introduction to Microsoft XML Schema -->

5

6 <myMessage xmlns = "x-schema:intro-schema.xml">

7

8 <greeting>Welcome to XML Schema!

9 <message>This is the first message.</message>

10 </greeting>

11

12 <message>This is the second message.</message>

13 </myMessage>

Reference intro-schema.xml through namespace declaration

Element greeting marks up text and message element

Page 7: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

7

Fig. 7.2 XML document that conforms to intro-schema.xml

Page 8: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

Outline8

Fig. 7.3 Well-formed, but invalid XML document.

Element message cannot contain children

1 <?xml version = "1.0"?>

2

3 <!-- Fig. 7.3 : intro2.xml -->

4 <!-- An invalid document -->

5

6 <myMessage xmlns = "x-schema:intro-schema.xml">

7

8 <greeting>Welcome to XML Schema!</greeting>

9

10 <message>This is a message that contains another message.

11 <message>This is the inner message.</message>

12 </message>

13

14 </myMessage>

Element message cannot contain children

Page 9: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

9

Fig. 7.3 Well-formed, but invalid XML document

Page 10: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

10

Fig. 7.4 ElementType element attributes.

Attribute Name Description

content Describes the element’s content. The valid values for this attribute are empty (an empty element), eltOnly (may contain only elements), textOnly (may contain only text) and mixed (may contain both elements and text). The default value for this attribute is mixed.

dt:type Defines the element’s data type. Data types exist for real numbers, integers, booleans, etc. Namespace prefix dt qualifies data types. We discuss data types in detail in Section 7.5.

name The element’s name. This is a required attribute.

model Specifies whether elements not defined in the schema are permitted in the element. Valid values are open (the default, which permits the inclusion of elements defined outside the schema) and closed (only elements defined inside the schema are permitted). We use only closed models.

Page 11: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

11

Fig. 7.4 ElementType element attributes. (Part 2)

Attribute Name Description

order Specifies the order in which child elements must occur. The valid values for this attribute are one (exactly one child element is permitted), seq (child elements must appear in the order in which they are defined) and many (child elements can appear in any order, any number of times). The default value is many if attribute content is mixed and is seq if attribute content has the value eltOnly.

Page 12: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

12

Fig. 7.5 Element ElementType’s child elements.

Element Name Description

description Provides a description of the ElementType.

datatype Specifies the data type for the ElementType element. We will discuss data types in Section 7.5.

element Specifies a child element by name.

group Groups related element elements and defines their order and frequency.

AttributeType Defines an attribute.

attribute Specifies an AttributeType for an element.

Page 13: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

13

Fig. 7.6 Element element attributes.

Attribute Name Description

type A required attribute that specifies a child element’s name (i.e., the name defined in the ElementType).

minOccurs Specifies the minimum number of occurrences an element can have. The valid values are 0 (the element is optional) and 1 (the element must occur one or more times). The default value is 1.

maxOccurs Specifies the maximum number of occurrences an element can have. The valid values are 1 (the element occurs at most once) and * (the element can occur any number of times). The default value is 1 unless the ElementType’s content attribute is mixed.

Page 14: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

14

Fig. 7.7 Element group’s attributes.

Attribute Name Description

order Specifies the order in which the elements occur. The valid values are one (contains exactly one child element from the group), seq (all child elements must appear in the sequential order in which they are listed) and many (the child elements can appear in any order, any number of times).

minOccurs Specifies the minimum number of occurrences an element can have. The valid values are 0 (the element is optional) and 1 (the element must occur at least once). The default value is 1.

maxOccurs Specifies the maximum number of occurrences an element can have. The valid values are 1 (the element occurs at most once) and * (the element can occur any number of times). The default value is 1 unless the ElementType’s content attribute is mixed.

Page 15: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

15

7.4 Microsoft XML Schema: Describing Attributes

• Attributes– Describe elements

– Element AttributeType defines attributes• May contain description elements

• May contain datatype elements

Page 16: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

16

Fig. 7.8 Element AttributeType’s attributes.

Attribute Name Description

default Specifies the attribute’s default value.

dt:type Defines the element’s data type. Data types exist for real numbers, integers, booleans, enumerations (i.e., a series of values from which one can be selected), etc. Namespace prefix dt qualifies data types. We discuss data types in detail in Section 7.5.

dt:values Contains an enumeration data type’s values. We discuss the enumeration data type in Section 7.5.

name The attribute name. This is a required attribute.

required Indicates whether the attribute is required. The valid values for this attribute are yes and no. The default value is no.

Page 17: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

17

Fig. 7.9 Element attribute’s attributes.

Attribute Name Description

default Specifies the attribute’s default value. This value overrides the value defined in the AttributeType element.

type Specifies the name of the AttributeType for the attribute. This is a required attribute.

required Indicates whether the attribute is required. Valid values for this attribute are yes and no. The default value is no.

Page 18: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

Outline18

Fig. 7.10 Demonstrating AttributeType and attribute.

Define element contact to contain only elements

AttributeType defines element contact’s name attribute property

1 <?xml version = "1.0"?>

2

3 <!-- Fig. 7.10 : contact-schema.xml -->

4 <!-- Defining attributes -->

5

6 <Schema xmlns = "urn:schemas-microsoft-com:xml-data">

7

8 <ElementType name = "contact" content = "eltOnly" order = "seq"

9 model = "closed">

10

11 <AttributeType name = "owner" required = "yes"/>

12 <attribute type = "owner"/>

13

14 <element type = "name"/>

15 <element type = "address1"/>

16 <element type = "address2" minOccurs = "0" maxOccurs = "1"/>

17 <element type = "city"/>

18 <element type = "state"/>

19 <element type = "zip"/>

20 <element type = "phone" minOccurs = "0" maxOccurs = "*"/>

21 </ElementType>

22

23 <ElementType name = "name" content = "textOnly"

24 model = "closed"/>

25

26 <ElementType name = "address1" content = "textOnly"

27 model = "closed"/>

28

AttributeType defines element contact’s name attribute property

Define element contact to contain only elements

Page 19: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

Outline19

Fig. 7.10 Demonstrating AttributeType and attribute. (Part 2)

Element phone can contain only text and has attribute location

29 <ElementType name = "address2" content = "textOnly"

30 model = "closed"/>

31

32 <ElementType name = "city" content = "textOnly"

33 model = "closed"/>

34

35 <ElementType name = "state" content = "textOnly"

36 model = "closed"/>

37

38 <ElementType name = "zip" content = "textOnly" model = "closed"/>

39

40 <ElementType name = "phone" content = "textOnly" model = "closed">

41 <AttributeType name = "location" default = "home"/>

42 <attribute type = "location"/>

43 </ElementType>

44

45 </Schema>

Element phone can contain only text and has attribute location

Page 20: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

Outline20

Fig. 7.11 Contact list that conforms to contact-schema.xml.

1 <?xml version = "1.0"?>

2

3 <!-- Fig. 7.11 : contact.xml -->

4 <!-- A contact list marked up as XML -->

5

6 <contact owner = "Bob Smith" xmlns = "x-schema:contact-schema.xml">

7 <name>Jane Doe</name>

8 <address1>123 Main St.</address1>

9 <city>Sometown</city>

10 <state>Somestate</state>

11 <zip>12345</zip>

12 <phone>617-555-1234</phone>

13 <phone location = "work">978-555-4321</phone>

14 </contact>

Page 21: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

21

Fig. 7.11 Contact list that conforms to contact-schema.xml.

Page 22: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

22

7.5 Microsoft XML Schema: Data Types

• Indicate content type for element or attribute– Not possible with DTDs

– Possible with Schemas• Namespace prefix dt for using data types

Page 23: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

23

Fig. 7.12 Some Microsoft XML Schema data types.

Data Type Description

boolean 0 (false) or 1 (true).

char A single character (e.g., "D").

string A series of characters (e.g., "Deitel").

float A real number (e.g., 123.4567890).

int A whole number (e.g., 5).

date A date formatted as YYYY-MM-DD (e.g., 2000-04-25).

time A time formatted as HH-MM-SS (e.g., 14:30:00).

id Text that uniquely identifies an element or attribute.

idref A reference to an id.

enumeration A series of values from which only one may be chosen.

Page 24: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

Outline24

Fig. 7.13 Using Microsoft XML Schema data types.

1 <?xml version = "1.0"?>

2

3 <!-- Fig. 7.13 : id-schema.xml -->

4 <!-- Using datatype ID -->

5

6 <Schema xmlns = "urn:schemas-microsoft-com:xml-data"

7 xmlns:dt = "urn:schemas-microsoft-com:datatypes">

8

9 <ElementType name = "bookstore" content = "eltOnly"

10 order = "many" model = "closed">

11

12 <element type = "shipping"/>

13 <element type = "book"/>

14 </ElementType>

15

16 <ElementType name = "shipping" content = "eltOnly" order = "seq"

17 model = "closed">

18

Page 25: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

Outline25

Fig. 7.13 Using Microsoft XML Schema data types. (Part 2)

Assign id to value of attribute dt.type (define shipID as unique identifier for element shipping)

Define element book to contain only text

Specify attribute shippedBy’s data type as idref

19 <AttributeType name = "shipID" dt:type = "id"

20 required = "yes"/>

21 <attribute type = "shipID"/>

22

23 <element type = "duration"/>

24 </ElementType>

25

26 <ElementType name = "duration" content = "textOnly"

27 model = "closed" dt:type = "date"/>

28

29 <ElementType name = "book" content = "textOnly" model = "closed"

30 dt:type = "string">

31

32 <AttributeType name = "shippedBy" dt:type = "idref"/>

33 <attribute type = "shippedBy"/>

34 </ElementType>

35

36 </Schema>

Assign id to value of attribute dt.type (define shipID as unique identifier for element shipping)

Define element book to contain only text

Specify attribute shippedBy’s data type as idref

Page 26: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

Outline26

Fig. 7.14 XML document conforming to id-schema.xml.

Define two shipping elements with unique identifiers s1 and s2

book elements use shippedBy attribute to reference s1 and s2

1 <?xml version = "1.0"?>

2

3 <!-- Fig. 7.14 : id.xml -->

4 <!-- Demonstrating ID and IDREF -->

5

6 <bookstore xmlns = "x-schema:id-schema.xml">

7 <shipping shipID = "s1">

8 <duration>2000-08-01</duration>

9 </shipping>

10

11 <shipping shipID = "s2">

12 <duration>2000-08-20</duration>

13 </shipping>

14

15 <book shippedBy = "s1">

16 Java How to Program 3rd edition.

17 </book>

18

19 <book shippedBy = "s2">

20 C How to Program 3rd edition.

21 </book>

22

23 <book shippedBy = "s2">

24 C++ How to Program 3rd edition.

25 </book>

26 </bookstore>

Define two shipping elements with unique identifiers s1 and s2

book elements use shippedBy attribute to

reference s1 and s2

Page 27: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

27

Fig. 7.14 XML document conforming to id-schema.xml.

Page 28: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

28

Fig. 7.15 Invalid XML document.

shippingBy is assigned value other than s1 and s2

Outline

shippingBy is assigned value other than s1 and s2

Page 29: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

Outline29

Fig. 7.16Schema for an inventory document.

Element book has attributes isbn and inStock

Attribute inStock is an enumeration (assigned either yes or no)

1 <?xml version = "1.0"?>

2

3 <!-- Fig. 7.16 : inventory-schema.xml -->

4 <!-- Data type example -->

5

6 <Schema xmlns = "urn:schemas-microsoft-com:xml-data"

7 xmlns:dt = "urn:schemas-microsoft-com:datatypes">

8

9 <ElementType name = "inventory" content = "eltOnly"

10 model = "closed">

11 <element type = "book" minOccurs = "0" maxOccurs = "*"/>

12 </ElementType>

13

14 <ElementType name = "book" content = "eltOnly" order = "seq"

15 model = "closed">

16

17 <AttributeType name = "isbn" dt:type = "string"

18 required = "yes"/>

19 <attribute type = "isbn"/>

20

21 <AttributeType name = "inStock" dt:type = "enumeration"

22 dt:values = "yes no" default = "no"/>

23 <attribute type = "inStock"/>

24

Element book has attributes isbn and inStock

Attribute inStock is an enumeration (assigned

either yes or no)

Page 30: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

Outline30

Fig. 7.16Schema for an inventory document. (Part 2)

Either quantity or available can be used in book element

25 <element type = "name"/>

26 <element type = "price"/>

27

28 <group order = "one">

29 <element type = "quantity"/>

30 <element type = "available"/>

31 </group>

32 </ElementType>

33

34 <ElementType name = "name" content = "textOnly" model = "closed"

35 dt:type = "string"/>

36

37 <ElementType name = "price" content = "textOnly" model = "closed"

38 dt:type = "float"/>

39

40 <ElementType name = "quantity" content = "textOnly"

41 dt:type = "int" model = "closed"/>

42

43 <ElementType name = "available" content = "textOnly"

44 dt:type = "date" model = "closed"/>

45 </Schema>

Either quantity or available can be used in book element

Page 31: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

Outline31

Fig. 7.17XML document conforming to inventory-schema.xml.

First book in inventory has 200 books in stock

Second book in not in stock, but will be available on Dec. 15, 2000

1 <?xml version = "1.0"?>

2

3 <!-- Fig. 7.17 : inventory.xml -->

4 <!-- Data type example -->

5

6 <inventory xmlns = "x-schema:inventory-schema.xml">

7 <book isbn = "0-13-012507-5" inStock = "yes">

8 <name>Java How to Program 3/e</name>

9 <price>68.00</price>

10 <quantity>200</quantity>

11 </book>

12

13 <book isbn = "0-13-028418-1" inStock = "no">

14 <name>Perl How to Program</name>

15 <price>68.00</price>

16 <available>2000-12-15</available>

17 </book>

18 </inventory>

First book in inventory has 200 books in stock

Second book in not in stock, but will be available on Dec. 15, 2000

Page 32: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

32

7.6 W3C XML Schema

• W3C XML Schema– Candidate Recommendation (at time of this writing)

Page 33: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

Outline33

Fig. 7.18W3C XML Schema document.

W3C XML Schema equivalent of Microsoft XML Schema (intro-schema.xsd)

Element element defines elements

Attributes name and type specify element’s name and data type

Element complexType can define mixed content

Attribute ref references existing element definition for message

1 <?xml version = "1.0"?>

2

3 <!-- Figure 7.20 : xml-schema.xsd -->

4 <!-- Example W3C XML Schema -->

5

6 <xsd:schema xmlns:xsd = "http://www.w3.org/2000/10/XMLSchema">

7 <xsd:element name = "message" type = "xsd:string"/>

8

9 <xsd:element name = "greeting" type = "greetingType"/>

10

11 <xsd:complexType name = "greetingType" content = "mixed">

12 <xsd:element ref = "message"/>

13 </xsd:complexType>

14

15 <xsd:element name = "myMessage" type = "myMessageType"/>

16

17 <xsd:complexType name = "myMessageType">

18 <xsd:element ref = "greeting" minOccurs = "0"

19 maxOccurs = "1"/>

20 <xsd:element ref = "message" minOccurs = "1"

21 maxOccurs = "unbounded"/>

22 </xsd:complexType>

23 </xsd:schema>

W3C XML Schema equivalent of Microsoft XML Schema (intro-schema.xsd)

Element element defines elements

Attributes name and type specify element’s name and data type

Attribute ref references existing element definition for message

Element complexType can define mixed content

Page 34: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

Outline34

 Fig. 7.19Document that conforms to xml-schema.xsd

1 <?xml version = "1.0"?>

2

3 <!-- Fig. 7.19 : intro3.xml -->

4 <!-- Introduction to W3C XML Schema -->

5

6 <myMessage

7 xmlns:xsd = "http://www.w3.org/2000/10/XMLSchema-instance"

8 xsd:noNamespaceSchemaLocation = "xml-schema.xsd">

9

10 <greeting>Welcome to W3C XML Schema!</greeting>

11 <message>This is a message.</message>

12 <message>This is another message.</message>

13

14 </myMessage>

Page 35: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

35Fig. 7.19 Document that conforms to

xml-schema.xsd.

Page 36: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

367.7 Case Study: Writing a Microsoft XML Schema for the Day Planner

Application• Day-planner application

– Use Microsoft XML Schema

Page 37: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

Outline37

Fig. 7.20Microsoft XML Schema for dayplanner.xml.

Define years as int (whole number values)

1 <?xml version = "1.0"?>

2

3 <!-- Fig. 7.20 : planner-schema.xml -->

4 <!-- Microsoft XML Schema for day planner -->

5

6 <Schema xmlns = "urn:schemas-microsoft-com:xml-data"

7 xmlns:dt = "urn:schemas-microsoft-com:datatypes">

8

9 <ElementType name = "planner" content = "eltOnly"

10 model = "closed">

11 <element type = "year" minOccurs = "0" maxOccurs = "*"/>

12 </ElementType>

13

14 <ElementType name = "year" content = "eltOnly" model = "closed">

15 <AttributeType name = "value" dt:type = "int"/>

16 <attribute type = "value"/>

17 <element type = "date" minOccurs = "0" maxOccurs = "*"/>

18 </ElementType>

19

Define years as int (whole number values)

Page 38: 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements

2002 Prentice Hall, Inc. All rights reserved.

Outline38

Fig. 7.20Microsoft XML Schema for dayplanner.xml. (Part 2)

Define months and days as ints (whole number values)

Define time as int (whole number values)

20 <ElementType name = "date" content = "eltOnly" model = "closed">

21 <AttributeType name = "month" dt:type = "int"/>

22 <attribute type = "month"/>

23

24 <AttributeType name = "day" dt:type = "int"/>

25 <attribute type = "day"/>

26

27 <element type = "note" minOccurs = "0" maxOccurs = "*"/>

28 </ElementType>

29

30 <ElementType name = "note" content = "textOnly" model = "closed"

31 dt:type = "string">

32

33 <AttributeType name = "time" dt:type = "int"/>

34 <attribute type = "time"/>

35 </ElementType>

36

37 </Schema>

Define months and days as ints (whole number values)

Define time as int (whole number values)