- Forward


Imposing Structure on XML Documents Using a Schema
An Introduction to Schemas


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • An Observation:
    • DTDs have a very different syntax from XML
  • Problems that Arise as a Result:
    • Creators of XML documents and DTDs must learn two syntaxes
    • Two different parsers are required to process XML documents
Resolving these Problems
Back SMYC Forward
  • The Idea:
    • Use XML to specify structure
  • The Result:
    • Use schemas to specify structure
  • The History:
    • Microsoft and the University of Edinburgh wer one of the first to use this approach (called XDR)
    • In 2001, the W3C published a different recommendation (which is now called both XSD and WXS); version 1.1 became a recommendation in 2012
Schemas
Back SMYC Forward
  • Associating a Schema with an XML Document:
    • Include attributes in the root element of the XML document
  • Syntax:
    • < root_tag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type=" schema ">
    • where type can be either schemaLocation or noNamespaceSchemaLocation depending on whether or not the schema has a target namespace
    • schema denotes the name of the schema
The Schema Element
Back SMYC Forward
  • Role:
    • The same role as the DOCTYPE in a DTD
  • Attributes:
    • The xmlns - specifies the default namespace for the elements and attributes
    • The targetNamespace - An optional namespace for the schema
  • Contains:
    • simpleType or complexType
    • element
    • attribute
The Schema Element (cont.)
Back SMYC Forward

An Example

xmlexamples/timetable/schema/timetable.xsd (Fragment: schema)
 
Different Approaches to Specifying Schemas
Back SMYC Forward
  • Local Types:
    • Define types, as needed, within elements and attributes
  • General Types:
    • Define types outside of the elements and attributes (the approach that we will use for consistency and simplicity)
The attribute Element
Back SMYC Forward
  • Role:
    • An attribute in an XML document
  • Attributes:
    • name
    • type
    • fixed or default - an optional constant or default value
    • use - either optional, prohibited, or required
  • Contains:
    • simpleType
The element Element
Back SMYC Forward
  • Role:
    • An element in a document
  • Attributes:
    • name
    • type
    • minOccurs - optional minimum number of occurrences
    • maxOccurs - optional maximum number of occurrences
  • Contains:
    • Nothing (since we are not using local types)
The simpleType Element
Back SMYC Forward
  • Role:
    • Similar to ELEMENT in a DTD (i.e., defines a type)
  • Attributes:
    • name
  • Contains:
    • restriction or list or union
The simpleType Element (cont.)
Back SMYC Forward

An Example

xmlexamples/timetable/schema/timetable.xsd (Fragment: simpleType)
 
The complexType Element
Back SMYC Forward
  • Role:
    • Similar to ELEMENT in a DTD (i.e., defines a type)
  • Attributes:
    • name
  • Contains:
    • simpleContent or complexContent
    • group or all or choice or sequence
    • attribute
The complexType Element (cont.)
Back SMYC Forward

An Example

xmlexamples/timetable/schema/timetable.xsd (Fragment: complexType)
 
The restriction Element
Back SMYC Forward
  • Role:
    • Restrictions on the definition of an element
  • Contains:
    • Diffrent elements depending on the thing being defined
The restriction Element (cont.)
Back SMYC Forward

An Example

xmlexamples/timetable/schema/timetable.xsd (Fragment: restriction)
 
The extension Element
Back SMYC Forward
  • Role:
    • Extend an existing simple or complex type
  • Attributes:
    • base
  • Contains:
    • group or all or choice or sequence
    • attribute
The extension Element (cont.)
Back SMYC Forward

An Example

xmlexamples/transcript/schema/transcript.xsd (Fragment: extension)
 
The simpleContent Element
Back SMYC Forward
  • Role:
    • Extensions or restrictions on text-only complext types or simple types
  • Contains:
    • restriction or extension
The simpleContent Element (cont.)
Back SMYC Forward

An Example

xmlexamples/timetable/schema/timetable.xsd (Fragment: simpleContent)
 
The complexContent Element
Back SMYC Forward
  • Role:
    • Extensions or restrictions on complext types that contain elements
  • Contains:
    • restriction or extension
The alternative Element
Back SMYC Forward
  • Role:
    • Provides for type alternatives
  • Attributes:
    • test - the test to perform
    • type - the type to use when the test passes
The alternative Element (cont.)
Back SMYC Forward

An Example

xmlexamples/transcript/schema/transcript.xsd (Fragment: alternative)
 
The assert Element
Back SMYC Forward
  • Role:
    • Constrain the existence and values of related elements and attributes
  • Attributes:
    • test - the test to perform
The aseert Element (cont.)
Back SMYC Forward

An Example

xmlexamples/transcript/schema/transcript.xsd (Fragment: assert)
 
The Complete Timetable Example
Back SMYC Forward
xmlexamples/timetable/schema/timetable.xsd
 
The Complete Transcript Example
Back SMYC Forward
xmlexamples/transcript/schema/transcript.xsd
 
There's Always More to Learn
Back -