1 / 65

XML Schemas

XML Schemas. Microsoft XML Schemas W3C XML Schemas. Objectives of Schemas. 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 W3C XML Schema.

gcovington
Download Presentation

XML Schemas

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XML Schemas Microsoft XML Schemas W3C XML Schemas

  2. Objectives of Schemas • 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 W3C XML Schema. • To use schema to describe elements and attributes. • To use schema data types.

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

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

  5. From DTDs to Schemas • 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.

  6. Schema Models • Two major schema models exist: W3C XML Schema and Microsoft XML Schema. Because W3C 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.]

  7. Schemas vs. DTDs • 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.

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

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

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

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

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

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

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

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

  16. 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 intro-schema.xml. In Fig 7.2, we show, in the browser Internet Explorer, a document that conforms to this schema.

  17. 1<?xml version = "1.0"?>23<!-- Fig. 7.1 : intro-schema.xml -->4<!-- Microsoft XML Schema showing the ElementType -->5<!-- element and element element -->67<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<ElementType name = "greeting" model = "closed"14content = "mixed" order = "many">15<element type = "message"/>16</ElementType>1718<ElementType name = "myMessage“ model = "closed"19content = "eltOnly" order = "seq">2021<element type = "greeting" minOccurs = "0"22maxOccurs = "1"/>23<element type = "message" minOccurs = "1"24maxOccurs = "*"/>2526</ElementType>27</Schema>

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

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

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

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

  22. Lines 8-11 <ElementType name = "message" content = "textOnly"   model = "closed">   <description>Text messages</description> </ElementType> • define element message, which can contain only text, because attribute content is textOnly.

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

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

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

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

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

  28. Lines 18 and 19 <ElementType name = "myMessage" model = "closed"    content = "eltOnly" order = "seq">

  29. define element myMessage. The content attribute's value eltOnly specifies that the myMessage element can only contain elements. Attribute order has the value seq, indicating that myMessage child elements must occur in the sequence defined in the schema.

  30. Lines 21-24 <element type = "greeting" minOccurs = "0"   maxOccurs = "1"/><element type = "message" minOccurs = "1"   maxOccurs = "*"/>

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

  32. Attributes minOccurs and maxOccurs specify the minimum and maximum number of times the element may appear in the myMessage element, respectively. The value 1 for the minOccurs attribute (line 23) indicates that element myMessage must contain at least one message element.

  33. The value * for the maxOccurs attribute (line 24) indicates that there is no limit on the maximum number of message elements that may appear in myMessage.

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

  35. msdn.microsoft.com/dowloads/samples/internet/xml/xml_validator/sample.aspmsdn.microsoft.com/dowloads/samples/internet/xml/xml_validator/sample.asp

  36. 1<?xml version = "1.0"?>23<!-- Fig. 7.3 : Um documento XML intro.xml -->4<!-- Introduction to Microsoft XML Schema -->56<myMessage xmlns = "x-schema:intro-schema.xml">78<greeting>Welcome to XML Schema!9 <message>This is the first message.</message>10</greeting>1112<message>This is the second message.</message>13</myMessage>

  37. 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. • Line 6<myMessage xmlns = "x-schema:intro-schema.xml">

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

  39. Line 12<message>This is the second message.</message> marks up text in a message element. • Line 8 in Fig 7.1 specifies that element message can contain only text.

  40. In the discussion of Fig 7.1, we mentioned that a closedmodel 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.</message>   <newElement>A new element.</newElement></greeting>

  41. uses element newElement, which is not defined in the schema. With a closedmodel, the document containing newElement is invalid. However, with an openmodel, the document is valid.

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

  43. Figure 7.4 lists the available attributes for the ElementType 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 ElementType element has the value eltOnly or mixedcontent, the ElementType may only contain the elements listed in Fig 7.5

  44. Attibute Description ElementType element

  45. ElementType element attributes • 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.

  46. ElementType element attributes • 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 closedmodels.

  47. ElementType element attributes • 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.

  48. ElementType element attributes • 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).

  49. The default value is many if attribute content is mixed and is seq if attribute content has the value eltOnly.

  50. ElementType's child elements • description - Provides a description of the ElementType. • datatype - Specifies the data type for the ElementType element. We will discuss data types. • element - Specifies a child element by name.

More Related