New Perspectives on XML, 2nd Edition - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

New Perspectives on XML, 2nd Edition

Description:

The element contains child elements but not attributes. ... The code to create a reference to an element or attribute declaration is ... – PowerPoint PPT presentation

Number of Views:86
Avg rating:3.0/5.0
Slides: 25
Provided by: willa47
Category:

less

Transcript and Presenter's Notes

Title: New Perspectives on XML, 2nd Edition


1


TUTORIAL 4
  • WORKING WITH SCHEMAS
  • Section 4.1 (p. 145)

2
SCHEMAS
  • A schema is an XML document that defines the
    content and structure of one or more XML
    documents.
  • The XML document containing the content is called
    the instance document.

3
COMPARING SCHEMAS AND DTDS
  • This figure compares schemas and DTDs

4
SCHEMA VOCABULARIES
  • There is no single schema form. Several schema
    vocabularies have been developed in the XML
    language.
  • Support for a particular schema depends on the
    XML parser being used for validation.

5
SCHEMA VOCABULARIES
  • This figure shows a few schema vocabularies

6
STARTING A SCHEMA FILE
  • A schema is always placed in a separate XML
    document that is referenced by the instance
    document.

7
ELEMENTS AND ATTRIBUTES OF THE PATIENTS DOCUMENT
  • This figure shows the elements and attributes of
    the patients.xml document

8
SCHEMA TYPES
  • XML Schema recognize two categories of element
    types complex and simple.
  • A complex type element has one or more
    attributes, or is the parent to one or more child
    elements.
  • A simple type element contains only character
    data and has no attributes.

9
SCHEMA TYPES
  • This figure shows types of elements

10
SIMPLE TYPE ELEMENTS
  • Use the following syntax to declare a simple type
    element in XML Schema
  • ltelement namename type type/gt
  • Here, name is the name of the element in the
    instance document and type is the data type of
    the element.
  • If a namespace prefix is used with the XML Schema
    namespace, any XML Schema tags must be qualified
    with the namespace prefix.

11
UNDERSTANDING DATA TYPES
  • XML Schema supports two data types built-in and
    user-derived.
  • A built-in data type is part of the XML Schema
    specifications and is available to all XML Schema
    authors.
  • A user-derived data type is created by the XML
    Schema author for specific data values in the
    instance document.

12
DECLARING AN ATTRIBUTE
  • An attribute is another example of a simple type.
    The syntax to define an attribute is
  • ltxsattribute name"name" type"type
    default"default fixed"fixed" /gt
  • Where name is the name of the attribute, type is
    the data type, default is the attributes default
    value, and fixed is a fixed value for the
    attribute.

13
ASSOCIATING ATTRIBUTES AND ELEMENTS
  • The basic structure for defining a complex type
    element with XML Schema is
  • ltxselement name"name"gt
  • ltxscomplexTypegt
  • declarations
  • lt/xscomplexTypegt
  • lt/xselementgt
  • Where name is the name of the element and
    declarations is schema commands specific to the
    type of complex element being defined.

14
ASSOCIATING ATTRIBUTES AND ELEMENTS
  • Four complex type elements that usually appear in
    an instance document are the following
  • The element is an empty element and contains only
    attributes.
  • The element contains textual content and
    attributes but no child elements.
  • The element contains child elements but not
    attributes.
  • The element contains both child elements and
    attributes.

15
EMPTY ELEMENTS AND ATTRIBUTES
  • The code to declare the attributes of an empty
    element is
  • ltxselement name"name"gt
  • ltxscomplexTypegt
  • attributes
  • lt/xscomplexTypegt
  • lt/xselementgt
  • Where attributes is the set of declarations that
    define the attributes associated with the
    element. For example, the empty element

16
SIMPLE CONTENT AND ATTRIBUTES
  • If an element is not empty and contains textual
    content (but no child elements), the structure of
    the complex type element is slightly different.
  • ltxselement name"name"gt
  • ltxscomplexTypegt
  • ltxssimpleContentgt
  • ltxsextension base"type"gt
  • attributes
  • lt/xsextensiongt
  • lt/xssimpleContentgt
  • lt/xscomplexTypegt
  • lt/xselementgt

17
SPECIFYING THE USE OF AN ATTRIBUTE
  • An attribute may or may not be required with a
    particular element. To indicate whether an
    attribute is required, you add the use attribute
    to the element declaration or reference. The use
    attribute has the following values
  • requiredThe attribute must always appear with
    the element
  • optionalThe use of the attribute is optional
    with the element
  • prohibitedThe attribute cannot be used with the
    element

18
REFERENCING AN ELEMENT OR ATTRIBUTE
  • XML Schema allows for a great deal of flexibility
    in designing complex types. Rather than nesting
    the attribute declaration within the element, you
    can create a reference to it. The code to create
    a reference to an element or attribute
    declaration is
  • ltxselement ref"elemName" /gt
  • ltxsattribute ref"attName" /gt
  • Where elemName is the name used in an element
    declaration and attName is the name used in an
    attribute declaration

19
WORKING WITH CHILD ELEMENTS
  • Another kind of complex type element contains
    child elements, but no attributes. To define
    these child elements, use the code structure
  • ltxselement name"name"gt
  • ltxscomplexTypegt
  • ltxscompositorgt
  • elements
  • lt/xscompositorgt
  • lt/xscomplexTypegt
  • lt/xselementgt
  • Where elements is the list of simple type element
    declarations for each child element, and
    compositor defines how the child elements are
    organized.

20
USING COMPOSITORS
  • XML Schema supports the following compositors
  • sequence defines a specific order for the child
    elements
  • choice allows any one of the child elements to
    appear in the instance document
  • all allows any of the child elements to appear in
    any order in the instance document however, they
    must appear either only once or not all.

21
WORKING WITH CHILD ELEMENTS AND ATTRIBUTES
  • The code for a complex type element that contains
    both attributes and child elements is
  • ltxselement name"name"gt
  • ltxscomplexTypegt
  • ltxscompositorgt
  • elements
  • lt/xscompositorgt
  • lt/xscomplexTypegt
  • attributes
  • lt/xselementgt

22
SPECIFYING MIXED CONTENT
  • When the mixed attribute is set to the value
    true, XML Schema assumes that the element
    contains both text and child elements. The
    structure of the child elements can then be
    defined with the conventional method. For
    example, the XML content
  • ltSummarygt
  • Patient ltNamegtCynthia Davislt/Namegt was enrolled
    in
  • the ltStudygtTamoxifen Studylt/Studygt on 8/15/2003.
  • lt/Summarygt
  • can be declared in the schema file using the
    following complex type
  • ltelement name"Summary"gt
  • ltcomplexType mixed"true"gt
  • ltsequencegt
  • ltelement name"Name" type"string"/gt
  • ltelement name"Study" type"string"/gt
  • lt/sequencegt
  • lt/complexTypegt
  • lt/elementgt

23
APPLYING A SCHEMA
  • To attach a schema to the document, you must do
    the following
  • Declare a namespace for XML Schema in the
    instance document.
  • Indicate the location of the schema file.
  • To declare the XML Schema namespace in the
    instance document, you add the following
    attribute to the documents root element
  • xmlnsxsi"http//www.w3.org/2001/XMLSchema-instan
    ce"

24
APPLYING A SCHEMA
  • If there is no namespace for the contents of the
    instance document, add the following attribute to
    the root element
  • xsinoNamespaceSchemaLocation"schema"
Write a Comment
User Comments (0)
About PowerShow.com