Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: " 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 – Schemas Outline 7.1Introduction 7.2Schemas vs. DTDs 7.3Microsoft XML Schema: Describing Elements."— Presentation transcript:

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 7.4Microsoft XML Schema: Describing Attributes 7.5Microsoft XML Schema: Data types 7.6W3C XML Schema 7.7Case Study: Writing a Microsoft XML Schema for the Day Planner Application

2  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

3  2002 Prentice Hall, Inc. All rights reserved. 3 7.2 Schema vs. DTDs DTDs –Cannot ensure proper element content hello is valid –Use EBNF grammar Schema –Can ensure proper element content –Use XML syntax

4  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

5  2002 Prentice Hall, Inc. All rights reserved. Outline 5 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 2 3 4 5 6 7 8 <ElementType name = "message" content = "textOnly" 9 model = "closed"> 10 Text messages 11 12 13 <ElementType name = "greeting" model = "closed" 14 content = "mixed" order = "many"> 15 16 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 27 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)

6  2002 Prentice Hall, Inc. All rights reserved. Outline 6 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 2 3 4 5 6 7 8 Welcome to XML Schema! 9 This is the first message. 10 11 12 This is the second message. 13 Reference intro-schema.xml through namespace declaration Element greeting marks up text and message element

7  2002 Prentice Hall, Inc. All rights reserved. 7 Fig. 7.2 XML document that conforms to intro-schema.xml

8  2002 Prentice Hall, Inc. All rights reserved. Outline 8 Fig. 7.3 Well-formed, but invalid XML document. Element message cannot contain children 1 2 3 4 5 6 7 8 Welcome to XML Schema! 9 10 This is a message that contains another message. 11 This is the inner message. 12 13 14 Element message cannot contain children

9  2002 Prentice Hall, Inc. All rights reserved. 9 Fig. 7.3 Well-formed, but invalid XML document

10  2002 Prentice Hall, Inc. All rights reserved. 10 Fig. 7.4 ElementType element attributes.

11  2002 Prentice Hall, Inc. All rights reserved. 11 Fig. 7.4 ElementType element attributes. (Part 2)

12  2002 Prentice Hall, Inc. All rights reserved. 12 Fig. 7.5 Element ElementType’s child elements.

13  2002 Prentice Hall, Inc. All rights reserved. 13 Fig. 7.6 Element element attributes.

14  2002 Prentice Hall, Inc. All rights reserved. 14 Fig. 7.7 Element group’s attributes.

15  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

16  2002 Prentice Hall, Inc. All rights reserved. 16 Fig. 7.8 Element AttributeType’s attributes.

17  2002 Prentice Hall, Inc. All rights reserved. 17 Fig. 7.9 Element attribute’s attributes.

18  2002 Prentice Hall, Inc. All rights reserved. Outline 18 Fig. 7.10 Demonstrating AttributeType and attribute. Define element contact to contain only elements AttributeType defines element contact ’s name attribute property 1 2 3 4 5 6 7 8 <ElementType name = "contact" content = "eltOnly" order = "seq" 9 model = "closed"> 10 11 12 13 14 15 16 17 18 19 20 21 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

19  2002 Prentice Hall, Inc. All rights reserved. Outline 19 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 39 40 41 42 43 44 45 Element phone can contain only text and has attribute location

20  2002 Prentice Hall, Inc. All rights reserved. Outline 20 Fig. 7.11 Contact list that conforms to contact- schema.xml. 1 2 3 4 5 6 7 Jane Doe 8 123 Main St. 9 Sometown 10 Somestate 11 12345 12 617-555-1234 13 978-555-4321 14

21  2002 Prentice Hall, Inc. All rights reserved. 21 Fig. 7.11 Contact list that conforms to contact-schema.xml.

22  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

23  2002 Prentice Hall, Inc. All rights reserved. 23 Fig. 7.12Some Microsoft XML Schema data types.

24  2002 Prentice Hall, Inc. All rights reserved. Outline 24 Fig. 7.13 Using Microsoft XML Schema data types. 1 2 3 4 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 13 14 15 16 <ElementType name = "shipping" content = "eltOnly" order = "seq" 17 model = "closed"> 18

25  2002 Prentice Hall, Inc. All rights reserved. Outline 25 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 22 23 24 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 33 34 35 36 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

26  2002 Prentice Hall, Inc. All rights reserved. Outline 26 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 2 3 4 5 6 7 8 2000-08-01 9 10 11 12 2000-08-20 13 14 15 16 Java How to Program 3rd edition. 17 18 19 20 C How to Program 3rd edition. 21 22 23 24 C++ How to Program 3rd edition. 25 26 Define two shipping elements with unique identifiers s1 and s2 book elements use shippedBy attribute to reference s1 and s2

27  2002 Prentice Hall, Inc. All rights reserved. 27 Fig. 7.14 XML document conforming to id- schema.xml.

28  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

29  2002 Prentice Hall, Inc. All rights reserved. Outline 29 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 2 3 4 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 12 13 14 <ElementType name = "book" content = "eltOnly" order = "seq" 15 model = "closed"> 16 17 <AttributeType name = "isbn" dt:type = "string" 18 required = "yes"/> 19 20 21 <AttributeType name = "inStock" dt:type = "enumeration" 22 dt:values = "yes no" default = "no"/> 23 24 Element book has attributes isbn and inStock Attribute inStock is an enumeration (assigned either yes or no )

30  2002 Prentice Hall, Inc. All rights reserved. Outline 30 Fig. 7.16Schema for an inventory document. (Part 2) Either quantity or available can be used in book element 25 26 27 28 29 30 31 32 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 Either quantity or available can be used in book element

31  2002 Prentice Hall, Inc. All rights reserved. Outline 31 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 2 3 4 5 6 7 8 Java How to Program 3/e 9 68.00 10 200 11 12 13 14 Perl How to Program 15 68.00 16 2000-12-15 17 18 First book in inventory has 200 books in stock Second book in not in stock, but will be available on Dec. 15, 2000

32  2002 Prentice Hall, Inc. All rights reserved. 32 7.6 W3C XML Schema W3C XML Schema –Candidate Recommendation (at time of this writing)

33  2002 Prentice Hall, Inc. All rights reserved. Outline 33 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <xsd:element ref = "greeting" minOccurs = "0" 19 maxOccurs = "1"/> 20 <xsd:element ref = "message" minOccurs = "1" 21 maxOccurs = "unbounded"/> 22 23 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

34  2002 Prentice Hall, Inc. All rights reserved. Outline 34 Fig. 7.19Document that conforms to xml- schema.xsd 1 2 3 4 5 6<myMessage 7 xmlns:xsd = "http://www.w3.org/2000/10/XMLSchema-instance" 8 xsd:noNamespaceSchemaLocation = "xml-schema.xsd"> 9 10 Welcome to W3C XML Schema! 11 This is a message. 12 This is another message. 13 14

35  2002 Prentice Hall, Inc. All rights reserved. 35 Fig. 7.19 Document that conforms to xml-schema.xsd.

36  2002 Prentice Hall, Inc. All rights reserved. 36 7.7 Case Study: Writing a Microsoft XML Schema for the Day Planner Application Day-planner application –Use Microsoft XML Schema

37  2002 Prentice Hall, Inc. All rights reserved. Outline 37 Fig. 7.20Microsoft XML Schema for dayplanner.xml. Define years as int (whole number values) 1 2 3 4 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 12 13 14 15 16 17 18 19 Define years as int (whole number values)

38  2002 Prentice Hall, Inc. All rights reserved. Outline 38 Fig. 7.20Microsoft XML Schema for dayplanner.xml. (Part 2) Define months and days as int s (whole number values) Define time as int (whole number values) 20 21 22 23 24 25 26 27 28 29 30 <ElementType name = "note" content = "textOnly" model = "closed" 31 dt:type = "string"> 32 33 34 35 36 37 Define months and days as int s (whole number values) Define time as int (whole number values)


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

Similar presentations


Ads by Google