XML Schemas Microsoft XML Schemas W 3 C

  • Slides: 65
Download presentation
XML Schemas Microsoft XML Schemas W 3 C XML Schemas

XML Schemas Microsoft XML Schemas W 3 C XML Schemas

Objectives of Schemas n n n To understand what a schema is. To understand

Objectives of Schemas n n n To understand what a schema is. To understand the basic differences between DTDs and schema. To be able to create Microsoft XML Schema. To become familiar with both Microsoft XML Schema and W 3 C XML Schema. To use schema to describe elements and attributes. To use schema data types.

n In Chapter 5 (XML Step by Step), we studied Document Type Definitions (DTDs).

n In Chapter 5 (XML Step by Step), we studied Document Type Definitions (DTDs). These describe an XML document's structure. DTDs are inherited from SGML. Many developers in the XML community feel DTDs are not flexible enough to meet today's programming needs.

n For example, DTDs cannot be manipulated (e. g. , searched, transformed into different

n For example, DTDs cannot be manipulated (e. g. , searched, transformed into different representation such as HTML, etc. ) in the same manner as XML documents can because DTDs are not XML documents.

From DTDs to Schemas n We introduce an alternative to DTDs— called schemas—for validating

From DTDs to Schemas n We introduce an alternative to DTDs— called schemas—for validating XML documents. Like DTDs, schemas must be used with validating parsers. Schemas are expected to replace DTDs as the primary means of describing document structure.

Schema Models n n Two major schema models exist: W 3 C XML Schema

Schema Models n n Two major schema models exist: W 3 C XML Schema and Microsoft XML Schema. Because W 3 C XML Schema technology is still in the early stages of development, we focus primarily on the well-developed Microsoft XML Schema. [Note: New schema models (e. g. , RELAX— www. xml. gr. jp/relax) are beginning to emerge. ]

Schemas vs. DTDs n n We highlight a few major differences between XML Schema

Schemas vs. DTDs n n We highlight a few major differences between XML Schema and DTDs. A DTD describes an XML document's structure —not its element content. For example, <quantity>5</quantity> contains character data.

n Element quantity can be validated to confirm that it does indeed contain content

n Element quantity can be validated to confirm that it does indeed contain content (e. g. , PCDATA), but its content cannot be validated to confirm that it is numeric; DTDs do not provide such a capability. So, unfortunately, markup such as <quantity>hello</quantity> is also considered valid.

n With XML Schema, element quantity's data can indeed be described as numeric. When

n With XML Schema, element quantity's data can indeed be described as numeric. When the preceding markup examples are validated against an XML Schema that specifies element quantity's data must be numeric, 5 conforms and hello fails.

n An XML document that conforms to a schema document is schema valid and

n An XML document that conforms to a schema document is schema valid and a document that does not conform is invalid.

n Unlike DTDs, schema do not use the Extended Backus-Naur Form (EBNF) grammar. Instead,

n Unlike DTDs, schema do not use the Extended Backus-Naur Form (EBNF) grammar. Instead, schema use XML syntax. Because schemas are XML documents, they can be manipulated (e. g. , elements added, elements removed, etc. ) like any other XML document. Later, we will discuss how to manipulate XML documents programmatically.

n We begin our discussion of Microsoft XML Schema. We discuss several key schema

n We begin our discussion of Microsoft XML Schema. We discuss several key schema elements and attributes, which are used in the examples. We also present our first Microsoft XML Schema document and use Microsoft's XML Validator to check it for validity. XML Validator also validates documents against DTDs as well as schema.

Microsoft XML Schema: Describing Elements n To use Microsoft XML Schema, Microsoft's XML parser

Microsoft XML Schema: Describing Elements n To use Microsoft XML Schema, Microsoft's XML parser (msxml) is required; this parser is part of Internet Explorer 5.

n Elements are the primary building blocks used to create XML documents. In Microsoft

n Elements are the primary building blocks used to create XML documents. In Microsoft XML Schema, element Element. Type defines an element. Element. Type contains attributes that describe the element's content, data type, name, etc.

n The application using the XML document containing this markup would need to test

n The application using the XML document containing this markup would need to test if quantity is numeric and take appropriate action if it is not.

The Fig. 7. 1 presents a complete schema. This schema describes the structure for

The Fig. 7. 1 presents a complete schema. This schema describes the structure for an XML document that marks up messages passed between users. We name the schema introschema. xml. In Fig 7. 2, we show, in the browser Internet Explorer, a document that conforms to this schema.

1 <? xml version = "1. 0"? > 2 3 <!-- Fig. 7. 1

1 <? xml version = "1. 0"? > 2 3 <!-- Fig. 7. 1 : intro-schema. xml --> 4 <!-- Microsoft XML Schema showing the Element. Type --> 5 <!-- element and element --> 6 7 <Schema xmlns = "urn: schemas-microsoft-com: xmldata"> 8 <Element. Type name = "message" content = "text. Only" 9 model = "closed"> 10 <description>Text messages</description> 11 </Element. Type> 12 <Element. Type name = "greeting" model = "closed" 14 content = "mixed" order = "many"> 15 <element type = "message"/> 16 </Element. Type> 17 18 <Element. Type name = "my. Message“ model = "closed" 19 content = "elt. Only" order = "seq"> 20 21 <element type = "greeting" min. Occurs = "0" 22 max. Occurs = "1"/> 23 <element type = "message" min. Occurs = "1" 24 max. Occurs = "*"/> 25 26 </Element. Type> 27 </Schema>

n Line 7 <Schema xmlns = "urn: schemas-microsoft com: xml-data"> declares the Microsoft XML

n Line 7 <Schema xmlns = "urn: schemas-microsoft com: xml-data"> declares the Microsoft XML Schema root element. Element Schema is the root element for every Microsoft XML Schema document.

n The xmlns attribute specifies the default namespace for the Schema element and the

n The xmlns attribute specifies the default namespace for the Schema element and the elements it contains. The attribute value urn: schemasmicrosoft-com: xml-data specifies the URI for this namespace.

n Microsoft Schema documents always use this URI because it is recognized by msxml.

n Microsoft Schema documents always use this URI because it is recognized by msxml. Microsoft's XML parser recognizes element Schema and this particular namespace URI and also it validates the schema.

n Element Schema can contain only elements Element. Type—for defining elements, Attribute. Type—for defining

n Element Schema can contain only elements Element. Type—for defining elements, Attribute. Type—for defining attributes and description— for describing the Schema element. We will discuss each of these elements momentarily.

n Lines 8 -11 <Element. Type name = "message" content = "text. Only" model

n Lines 8 -11 <Element. Type name = "message" content = "text. Only" model = "closed"> <description>Text messages</description> </Element. Type> n define element message, which can contain only text, because attribute content is text. Only.

n Attribute model has the value closed (line 9)—indicating that only elements declared in

n Attribute model has the value closed (line 9)—indicating that only elements declared in this schema are permitted in a conforming XML document. Any elements not defined in this schema would invalidate the document.

n We will elaborate on this when we discuss an XML document that conforms

n We will elaborate on this when we discuss an XML document that conforms to the schema ( Fig 7. 2). Element description contains text that describe this schema. In this particular case (line 10), we indicate in the description element that the message element we define is intended to contain Text messages.

n Lines 13 -16 <Element. Type name = "greeting" model = "closed " content

n Lines 13 -16 <Element. Type name = "greeting" model = "closed " content = "mixed" order = "many"> <element type = "message"/> </Element. Type>

n define element greeting. Because attribute content has the value mixed, this element can

n define element greeting. Because attribute content has the value mixed, this element can contain both elements and character data. The order attribute specifies the number and order of child elements a greeting element may contain.

n The value many indicates that any number of message elements and text can

n The value many indicates that any number of message elements and text can be contained in the greeting element in any order. The element on line 15 indicates message elements (defined on lines 8 -11) may be included in a greeting element.

n Lines 18 and 19 <Element. Type name = "my. Message" model = "clo

n Lines 18 and 19 <Element. Type name = "my. Message" model = "clo sed" content = "elt. Only" order = "seq">

n define element my. Message. The content attribute's value elt. Only specifies that the

n define element my. Message. The content attribute's value elt. Only specifies that the my. Message element can only contain elements. Attribute order has the value seq, indicating that my. Message child elements must occur in the sequence defined in the schema.

n Lines 21 -24 <element type = "greeting" min. Occurs = "0" max. Occurs

n Lines 21 -24 <element type = "greeting" min. Occurs = "0" max. Occurs = "1"/> <element type = "message" min. Occurs = "1" max. Occurs = "*"/>

n indicate that element my. Message contains child elements greeting and message. These elements

n indicate that element my. Message contains child elements greeting and message. These elements are my. Message child elements, because the elements that reference them are nested inside element my. Message. Because the element order in element my. Message is set as sequential, the greeting element (if used) must precede all message elements.

n Attributes min. Occurs and max. Occurs specify the minimum and maximum number of

n Attributes min. Occurs and max. Occurs specify the minimum and maximum number of times the element may appear in the my. Message element, respectively. The value 1 for the min. Occurs attribute (line 23) indicates that element my. Message must contain at least one message element.

n The value * for the max. Occurs attribute (line 24) indicates that there

n The value * for the max. Occurs attribute (line 24) indicates that there is no limit on the maximum number of message elements that may appear in my. Message.

n n Figure 7. 2 shows an XML document that conforms to the schema

n n Figure 7. 2 shows an XML document that conforms to the schema shown in Fig 7. 1. We use Microsoft's XML Validator to check the document's conformity. It is available as a free download at

n msdn. microsoft. com/dowloads/samples/ internet/xml_validator/sample. asp

n msdn. microsoft. com/dowloads/samples/ internet/xml_validator/sample. asp

1 <? xml version = "1. 0"? > 2 3 <!-- Fig. 7. 3

1 <? xml version = "1. 0"? > 2 3 <!-- Fig. 7. 3 : Um documento XML intro. xml --> 4 <!-- Introduction to Microsoft XML Schema --> 5 6 <my. Message 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 </my. Message>

n Line 6 <my. Message xmlns = "x-schema: introschema. xml"> references the schema (

n Line 6 <my. Message xmlns = "x-schema: introschema. xml"> references the schema ( Fig 7. 1) through the namespace declaration. A document using a Microsoft XML Schema uses attribute xmlns to reference its schema through a URI which begins with x-schema followed by a colon (: ) and the name of the schema document.

Mixed Content n Lines 8 -10 <greeting>Welcome to XML Schema! <message>This is the first

Mixed Content n Lines 8 -10 <greeting>Welcome to XML Schema! <message>This is the first message. </message> </greeting> n use element greeting to mark up text and a message element. Recall that in Fig 7. 1, element greeting (lines 13 -16) may contain mixed content.

n Line 12 <message>This is the second message. </messag e> marks up text in

n Line 12 <message>This is the second message. </messag e> marks up text in a message element. n Line 8 in Fig 7. 1 specifies that element message can contain only text.

n n In the discussion of Fig 7. 1, we mentioned that a closed

n n In the discussion of Fig 7. 1, we mentioned that a closed model allows an XML document to contain only those elements defined in its schema. For example, the markup <greeting>Welcome to XML Schema! <message>This is the first message. </m essage> <new. Element>A new element. </new. Elem ent> </greeting>

n uses element new. Element, which is not defined in the schema. With a

n uses element new. Element, which is not defined in the schema. With a closed model, the document containing new. Element is invalid. However, with an open model, the document is valid.

n Figure 7. 3 shows a well-formed document that fails to conform to the

n Figure 7. 3 shows a well-formed document that fails to conform to the schema shown in Fig 7. 1, because element message cannot contain child elements.

n n Figure 7. 4 lists the available attributes for the Element. Type element.

n n Figure 7. 4 lists the available attributes for the Element. Type element. Schema authors use these attributes to specify the properties of an element, such as its content, data type, name, etc. If the content attribute for an Element. Type element has the value elt. Only or mixed content, the Element. Type may only contain the elements listed in Fig 7. 5

Attibute Description Element. Type element

Attibute Description Element. Type element

Element. Type element attributes n Content Describes the element's content. The valid values for

Element. Type element attributes n Content Describes the element's content. The valid values for this attribute are empty (an empty element), elt. Only (may contain only elements), text. Only (may contain only text) and mixed (may contain both elements and text). The default value for this attribute is mixed.

Element. Type element attributes n n Name - The element's name. This is a

Element. Type element attributes n n 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.

Element. Type element attributes n dt: type - Defines the element's data type. Data

Element. Type element attributes n 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.

Element. Type element attributes n Order Specifies the order in which child elements must

Element. Type element attributes n 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

The default value is many if attribute content is mixed and is seq if attribute content has the value elt. Only.

Element. Type's child elements n n n description - Provides a description of the

Element. Type's child elements n n n description - Provides a description of the Element. Type. datatype - Specifies the data type for the Element. Type element. We will discuss data types. element - Specifies a child element by name.

n n n group - Groups related elements and defines their order and frequency.

n n n group - Groups related elements and defines their order and frequency. attribute. Type - Defines an attribute - Specifies an Attribute. Type for an element

n The element does not define an element, but rather refers to an element

n The element does not define an element, but rather refers to an element defined by an Element. Type. This allows the schema author to define an element once and refer to it from many places inside the schema document. The attributes of the element are listed in Fig 7. 6.

n As mentioned in Fig 7. 5, element group creates groups of elements. Groups

n As mentioned in Fig 7. 5, element group creates groups of elements. Groups define the order and frequency in which elements appear using the attributes listed in Fig 7. 7.

Element element attributes n type - A required attribute that specifies a child element's

Element element attributes n type - A required attribute that specifies a child element's name (i. e. , the name defined in the Element. Type).

Element element attributes n min. Occurs - Specifies the minimum number of occurrences an

Element element attributes n min. Occurs - 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.

Element element attributes n max. Occurs. Specifies the maximum number of occurrences an element

Element element attributes n max. Occurs. 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 Element. Type's content attribute is mixed

Element group's attributes n Order Specifies the order in which the elements occur. The

Element group's attributes n 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).

Element group's attributes n min. Occurs Specifies the minimum number of occurrences an element

Element group's attributes n min. Occurs 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.

Element group's attributes n max. Occurs Specifies the maximum number of occurrences an element

Element group's attributes n max. Occurs 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 Element. Type's content attribute is mixed.

7. 4 Microsoft XML Schema: Describing Attributes

7. 4 Microsoft XML Schema: Describing Attributes

n n XML elements can contain attributes that describe elements. In Microsoft XML Schema,

n n XML elements can contain attributes that describe elements. In Microsoft XML Schema, element Attribute. Type defines attributes. Figure 7. 8 lists Attribute. Type element attributes.

n Like element Element. Type element, element Attribute. Type may contain description elements and

n Like element Element. Type element, element Attribute. Type may contain description elements and datatype elements.

n To indicate that an element has an Attribute. Type, element attribute is used.

n To indicate that an element has an Attribute. Type, element attribute is used. The attributes of the attribute element are shown in Fig 7. 9.

1 <? xml version = "1. 0"? > 2 3 <!-- Fig. 7. 10

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 <Element. Type name = "contact" content = "elt. Only" order = "seq" 9 model = "closed"> 10 11 <Attribute. Type name = "owner" required = "yes"/> 12 <attribute type = "owner"/> 13 14 <element type = "name"/> 15 <element type = "address 1"/> 16 <element type = "address 2" min. Occurs = "0" max. Occurs = "1"/> 17 <element type = "city"/> 18 <element type = "state"/> 19 <element type = "zip"/> 20 <element type = "phone" min. Occurs = "0" max. Occurs = "*"/> 21 </Element. Type>

22 23 <Element. Type name = "name" content = "text. Only" 24 model =

22 23 <Element. Type name = "name" content = "text. Only" 24 model = "closed"/> 25 26 <Element. Type name = "address 1" content = "text. Only" 27 model = "closed"/> 28 29 <Element. Type name = "address 2" content = "text. Only" 30 model = "closed"/> 31 32 <Element. Type name = "city" content = "text. Only" 33 model = "closed"/> 34 35 <Element. Type name = "state" content = "text. Only" 36 model = "closed"/> 37 38 <Element. Type name = "zip" content = "text. Only" model = "closed"/> 39 40 <Element. Type name = "phone" content = "text. Only" model = "closed"> 41 <Attribute. Type name = "location" default = "home"/> 42 <attribute type = "location"/> 43 </Element. Type> 44 45 </Schema>