XML Schemas - PowerPoint PPT Presentation

About This Presentation
Title:

XML Schemas

Description:

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. – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 66
Provided by: JoaoB8
Category:
Tags: xml | schemas | validator

less

Transcript and Presenter's Notes

Title: XML Schemas


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 DTDscalled
    schemasfor 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.,
    RELAXwww.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 structurenot its element content.
  • For example,
  • ltquantitygt5lt/quantitygt
  • 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
  • ltquantitygthellolt/quantitygt
  • 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

18
  • Line 7 ltSchema xmlns  "urnschemas-microsoft-

  • comxml-data"gt
  • 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
    urnschemas-microsoft-comxml-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
    ElementTypefor defining elements,
    AttributeTypefor defining attributes and
    descriptionfor describing the Schema element. We
    will discuss each of these elements momentarily.

22
  • Lines 8-11
  • ltElementType name  "message" content  "textOnly"
       model  "closed"gt   ltdescriptiongtText message
    slt/descriptiongt
  • lt/ElementTypegt
  • 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
    Text messages.

25
  • Lines 13-16
  • ltElementType name  "greeting" model  "closed"
        content  "mixed" order  "many"gt   
    ltelement type  "message"/gtlt/ElementTypegt

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
  • ltElementType name  "myMessage" model  "closed"
        content  "eltOnly" order  "seq"gt

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
  • ltelement type  "greeting" minOccurs  "0"   
    maxOccurs  "1"/gtltelement type  "message" minOcc
    urs  "1"   maxOccurs  ""/gt

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/x
    ml_validator/sample.asp

36
(No Transcript)
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 ltmyMessage xmlns  "x-schemaintro-schema.
    xml"gt

38
Mixed Content
  • Lines 8-10 ltgreetinggtWelcome to XML Schema!   
    ltmessagegtThis is the first message.lt/messagegtlt/gr
    eetinggt
  • 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.

39
  • Line 12 ltmessagegtThis is the second message.lt/mes
    sagegt
  • 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
    closed model allows an XML document to contain
    only those elements defined in its schema.
  • For example, the markup ltgreetinggtWelcome to XML 
    Schema!   ltmessagegtThis is the first message.lt/me
    ssagegt   ltnewElementgtA new element.lt/newElementgt
    lt/greetinggt

41
  • uses element newElement, which is not defined in
    the schema. With a closed model, the document
    containing newElement is invalid. However, with
    an open model, 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 mixed content,
    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 closed models.

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

51
  • group - Groups related element elements and
    defines their order and frequency.
  • attributeType - Defines an attribute.
  • attribute - Specifies an AttributeType for an
    element

52
  • The element element does not define an element,
    but rather refers to an element defined by an
    ElementType. 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 element are listed in Fig 7.6.

53
  • As mentioned in Fig 7.5, element group creates
    groups of element elements. Groups define the
    order and frequency in which elements appear
    using the attributes listed in Fig 7.7.

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

55
Element element attributes
  • 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.

56
Element element attributes
  • maxOccursSpecifies 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

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

58
Element group's attributes
  • 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.

59
Element group's attributes
  • 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.

60
7.4 Microsoft XML Schema Describing Attributes
61
  • XML elements can contain attributes that describe
    elements.
  • In Microsoft XML Schema, element AttributeType
    defines attributes. Figure 7.8 lists
    AttributeType element attributes.

62
  • Like element ElementType element, element
    AttributeType may contain description elements
    and datatype elements.

63
  • To indicate that an element has an AttributeType,
    element attribute is used. The attributes of the
    attribute element are shown in Fig 7.9.

64
1 lt?xml version "1.0"?gt2 3 lt!-- Fig. 7.10
contact-schema.xml --gt4 lt!-- Defining attributes
--gt5 6 ltSchema xmlns "urnschemas-microsoft-co
mxml-data"gt7 8 ltElementType name
"contact" content "eltOnly" order "seq"9
model "closed"gt 10 11
ltAttributeType name "owner" required
"yes"/gt12 ltattribute type
"owner"/gt13 14 ltelement type
"name"/gt15 ltelement type
"address1"/gt16 ltelement type
"address2" minOccurs "0" maxOccurs "1"/gt17
ltelement type "city"/gt18
ltelement type "state"/gt19 ltelement
type "zip"/gt20 ltelement type
"phone" minOccurs "0" maxOccurs ""/gt21
lt/ElementTypegt
65
22 23 ltElementType name "name"
content "textOnly" 24 model
"closed"/gt25 26 ltElementType name
"address1" content "textOnly"27
model "closed"/gt28 29
ltElementType name "address2" content
"textOnly"30 model
"closed"/gt31 32 ltElementType name
"city" content "textOnly" 33
model "closed"/gt34 35 ltElementType
name "state" content "textOnly"36
model "closed"/gt37 38
ltElementType name "zip" content "textOnly"
model "closed"/gt39 40 ltElementType
name "phone" content "textOnly" model
"closed"gt41 ltAttributeType
name "location" default "home"/gt42
ltattribute type "location"/gt43
lt/ElementTypegt44 45 lt/Schemagt
Write a Comment
User Comments (0)
About PowerShow.com