René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Schema

In the XML context, a Schema is a substitute for a DTD, written in XML. It defines the elements that can appear in an XML document and their possible attributes.
Here's a little comparison between DTDs and Schemas.

Simple setup

The Schema File

In its simplest form, a schema looks like the following framgent:
<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<!--    Schema definitions go here     -->

</xsd:schema>

The XML File

<?xml version="1.0" encoding="ISO-8859-1" ?>

  <rootelem
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="file://c:/path/to/schema-file.xsd">
  >

   <!-- furher  XML here  -->

</rootelem>

Simple Type vs Complex type

With Schema, an XML Element is assigned to a type which can either be simple or complex.

Simple Type

A simple type can contain only a basic type (comparable to a variable in a programming language). It cannot contain other elements, and in cannot have attributes. If a type is not a simple type, it is automatically a complex type.
See here for an example

Complex Type

A complex type is an element that can include an attribute value and or has a content model that contains other elements.
There are four types of complex elements:
  • complex empty elements
    An element that has null content (but can have attributes)
    See here for an example
  • Complex Elements-Only Element
    Element that can contain only other elements and attributes.
    See here for an example
  • Complex mixed content
    Element that can contain Elements and Text and attributes.
    See here for an example
  • Complex text-only element
    Element that can only contain Text and attributes.
    See here for an example

Declaration of a root element

The Schema File

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:element   name='root-elem'>
  </xsd:element>

</xsd:schema>

The XML File

<?xml version="1.0" encoding="ISO-8859-1"?>

<root-elem
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="file://c:/path/to/schema-file.xsd">

</root-elem>

Nesting elements

As an element that contains another element is a complex type, the nesting element must be declared as such:

Schema

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:element   name="root-elem" >

  <xsd:complexType name="somecomplextype">
    <xsd:sequence>
      <xsd:element name="first" />
      <xsd:element name="second" />
    </xsd:sequence>
  </xsd:complexType>

  </xsd:element>

</xsd:schema>

XML

<?xml version="1.0" encoding="ISO-8859-1"?>

<root-elem
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="file://c:/path/to/schema-file.xsd">

    <first>data of first</first>
    <second>data of second</second>

</root-elem>

Attributes

<attribute name="some_number" type="integer" default="0" />
Here's a working example:
The Schema file:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:element name="foo">
  <xsd:complexType>
    <xsd:sequence>

      <xsd:element name="elem_1">
      <xsd:complexType>
        <xsd:attribute name="elem_1_attr" type="xsd:string" />
      </xsd:complexType>
      </xsd:element>

      <xsd:element name="elem_2">
      <xsd:complexType>
        <xsd:attribute name="elem_2_attr" type="xsd:string" />
      </xsd:complexType>
      </xsd:element>

    </xsd:sequence>
  </xsd:complexType>
  </xsd:element>

</xsd:schema>
The XML file:
<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:element name="foo">
  <xsd:complexType>
    <xsd:sequence>

      <xsd:element name="elem_1">
      <xsd:complexType>
        <xsd:attribute name="elem_1_attr" type="xsd:string" />
      </xsd:complexType>
      </xsd:element>

      <xsd:element name="elem_2">
      <xsd:complexType>
        <xsd:attribute name="elem_2_attr" type="xsd:string" />
      </xsd:complexType>
      </xsd:element>

    </xsd:sequence>
  </xsd:complexType>
  </xsd:element>

</xsd:schema>

Groups of attributes

The Schema file:
<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">


  <!-- First: define an attribute group, name it attr_group -->

  <xsd:attributeGroup name="attr_group">
      <xsd:attribute name="attr_one" type="xsd:string" />
      <xsd:attribute name="attr_two" type="xsd:string" />
  </xsd:attributeGroup>

  <xsd:element name="foo">
    <xsd:complexType>
      <xsd:sequence>
  
        <xsd:element name="elem_1">
        <xsd:complexType>
          <!-- Then: reference the group using ref by the groups name  -->
          <xsd:attributeGroup ref="attr_group" />
        </xsd:complexType>
        </xsd:element>
  
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

</xsd:schema>
The XML file:
<?xml version="1.0" encoding="ISO-8859-1" ?>

  <foo  xmlns:xsi                     = "http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation = "file://ag.xsd" >

      <elem_1 attr_one="bar" attr_two="baz" />

</foo>

Datatypes

Built-in datatypes

  • string
  • decimal
  • integer
  • boolean
  • date
  • time
  • ...
There are totally 44 built-in datatypes. New datatypes can be built or derived

Derived datatypes

12 facets can be used to derive new datatypes from built-in datatypes by restricting possible values:
  • length, minLength, maxLength
  • pattern (regular expressions)
  • enumeration
  • whiteSpace
  • minInclusive, maxInclusive, minExclusive, maxExclusive
  • totalDigits, fractionDigits
The Schema file:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:element name="root-elem">
    <xsd:simpleType>
        <xsd:restriction base="xsd:positiveInteger">
         <xsd:maxExclusive value="100"/>
        </xsd:restriction>
    </xsd:simpleType>
  </xsd:element>

</xsd:schema>
The XML File:
<?xml version="1.0" encoding="ISO-8859-1"?>

<root-elem
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="file://restriction.xsd">42</root-elem>
If we had some figure greater than 99 or below zero, it wouldn't work for the restriction based.

Tags (Elements) possible in Schemas

all

See also choice, sequence and group
 

annotation

any

anyAttribute

 

appInfo

attribute

Declares an attribute.

attributeGroup

choice

See also all, sequence and group

complexContent

 

complexType

Defines a new complex type.
Such a definition usually contains
  • element declarations
  • element references
  • attribute declarations
A complex type is defined like so:
<xsd:complexType name='T_person'>

  <xsd:all>
    <xsd:element name='first_name'  type='xsd:string'          />
    <xsd:element name='last_name'   type='xsd:string'          />
    <xsd:element name='birth_day'   type='xsd:date'            />
  </xsd:all>

  <xsd:attribute name='employee_id' type='xsd:positiveInteger' />

</xsd:complexType>
It can then be referenced:
<xsd:element name='person' type='T_person' />

documentation

element

Declares an element.
Attributes:

enumeration

extension

field

group

Attributes for model group definition (parent is schema or redefine:
  • name
  • targetNamespace
  • ????
Attributes for particle component:
  • name
  • minOccurs
  • maxOccurs
  • term
Content: (annotation?, ( all | choice | sequence))

import

include

key

keyref

length

list

maxInclusive

maxLength

minInclusive

minLength

pattern

redefine

restriction

Restriction allows to create a new type based on another (already existing) type.
<xsd:simpleType name='T_length_between_5_and_9'>
  <xsd:restriction base='xsd:string'>
    <xsd:minLength value='5' />
    <xsd:maxLength value='9' />
  </xsd:restriction>
</xsd:simpleType>
Now that the type has been defined, an element can be created from it:
<xsd:element name='something' type='T_length_between_5_and_9' />

schema

selector

sequence

See also all, choice and group

simpleContent

simpleType

union

unique

Misc links

See also validating XML against a schema (with MSXML).
maxOccurs can have the value "unbounded"