XML - (XML Schema Definition|XSD) (Schema With Capital S)

> Markup Language (ML) > Extensible Markup Language (XML)

1 - About

The Data Structure - (Schema|Data Model) in XML.

XML Schema definition language (Xsd) is a language that define an XML schema (usually written in the W3C XML Schema Language). It provides functionality above and beyond what is provided by DTD.

The XML Schema language provides the elementary data types for numbers, booleans, strings, URIs, dates and times, and for references and other XML constructs. User-defined data types can be derived from any elementary type by adding one or more restrictions. This is done by adding so-called facets, which are available to set lower or upper bounds for values or string lengths, to limit the precision, to enumerate all legal values, and to define a pattern for a string type.

Advertising

3 - Form

The tags of schema elements are presented in the qualified form, with xsd as the namespace identifier. This (or some other name, e.g., xs) identifier must be bound to the URI identifying the XML Schema language.

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

Each of the elements in the schema has a prefix xsd: which is associated with the XML Schema namespace through the declaration, xmlns:xsd=“http://www.w3.org/2001/XMLSchema”, that appears in the schema element. The prefix xsd: is used by convention to denote the XML Schema namespace, although any prefix can be used

4 - Declaration

A schema consists of:

They determine the appearance of elements and their content in instance documents.

4.1 - Element

The xsd:element defines the XML tag.

Elements are declared using the element element. Example:

<xsd:element name="name"     type="xsd:string"/>
<xsd:element name="zip"      type="xsd:decimal"/>
<xsd:element name="Quantity" type="xsd:int"/>

It will result for instance in the following XML instance:

<name>Nico</name>
<zip>3234</zip>
<Quantity>1</Quantity>
Advertising

4.2 - complexType

complexType elements are elements that:

  • contain sub-elements (allow elements in their content)
  • may carry attributes

New complex types are defined using the complexType element which contain a set of:

  • element declarations,
  • element references,
  • and attribute declarations.

For example, USAddress is defined as a complex type, and within the definition of USAddress we see five element declarations and one attribute declaration:

<xsd:complexType name="USAddress" >
  <xsd:sequence>
    <xsd:element name="name"   type="xsd:string"/>
    <xsd:element name="street" type="xsd:string"/>
    <xsd:element name="city"   type="xsd:string"/>
    <xsd:element name="state"  type="xsd:string"/>
    <xsd:element name="zip"    type="xsd:decimal"/>
  </xsd:sequence>
  <xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/>
</xsd:complexType>

Example of Instance for the USAddress complextype:

<shipTo country="US">
    <name>Alice Smith</name>
    <street>123 Maple Street</street>
    <city>Mill Valley</city>
    <state>CA</state>
    <zip>90952</zip>
</shipTo>

4.3 - simpleType

simpleType elements are elements that:

  • do not contain any sub-elements (cannot have element content)
  • cannot carry attributes
  • contain numbers (and strings, and dates, etc.)

Some elements have attributes; attributes always have simple types.

Simple types are defined as part of XML Schema's repertoire of built-in simple types such as:

  • string,
  • decimal
  • NMTOKEN
Advertising

4.4 - attribute

attributes are declared using the attribute element.

<xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/>

5 - Grouping facilities

For child elements, the schema's element grouping facilities <xsd:all>, <xsd:union>, <xsd:choice> and <xsd:sequence> in combination with repetition limit attributes let you define XML structures that are the equivalent of the classic concepts of array, list, structure (or record) and union.

6 - Association Instance and Schema

An instance is not required to reference a schema.

7 - Snippet

7.1 - Integer Range Type

The XML Schema snippet shown below defines an integer type, limiting permissible values to the range 1 to 255.

<xsd:simpleType name="GroupType">
  <xsd:restriction base="xsd:int">
    <xsd:minInclusive value="1"/>
    <xsd:maxInclusive value="255"/>
  </xsd:restriction>
</xsd:simpleType>

7.2 - String with Length Limits

The XML Schema snippet shown below defines a string type, limiting string lengths to the range 1 to 3.

<xsd:simpleType name="ShortNameType">
  <xsd:restriction base="xsd:string">
    <xsd:minLength  value="1"/>
    <xsd:maxLength  value="3"/>
  </xsd:restriction>
</xsd:simpleType>

7.3 - Defining a String Type Restricted By Pattern

A string type may also be restricted by a pattern facet. The example below defines a type for strings of arbitrary length consisting of 'L' and 'R' only.

<xsd:simpleType name="DirType">
  <xsd:restriction base="xsd:string">
    <xsd:pattern value="[LR]*"/>
  </xsd:restriction>
</xsd:simpleType>

The syntax for regular expressions in pattern facets provides the basics for repetition, alternatives and grouping. It should be noted that a regular expression is always matched against the entire string value.

7.4 - Defines an element containing sub-elements

The XML schema snippet shown below defines an element containing sub-elements with xsd:date and xsd:time.

<xsd:complexType name="DateTimeType">
  <xsd:sequence>
    <xsd:element name="Date" type="xsd:date"/>
    <xsd:element name="Time" type="xsd:time"/>
  </xsd:sequence>
 </xsd:complexType>

7.5 - Defines an element containing binary data

The XML Schema datatype to use for binary data is xsd:hexBinary. A sample schema declaration is shown below.

<xsd:complexType name="BinaryType">
  <xsd:sequence>
    <xsd:element name="data" type="xsd:hexBinary"/>
  </xsd:sequence>
</xsd:complexType>

7.6 - Defining a List of Integers

To obtain a simple type that can be used for XML element values as well as for attribute values consisting of a simple series of values, use an xsd:list

<xsd:simpleType name="NumberListType">
  <xsd:list itemType="xsd:int"/>
</xsd:simpleType>

7.7 - Defining a List of String

The XML representation of a xsd:list is a list of values of the defined type, separated by white space.

For string unless you can be very sure that your strings are free from spaces or any other white space, don't do anything like this:

<xsd:simpleType name="StringListType">
  <xsd:list itemType="xsd:string"/>   <!-- dangerous! -->
</xsd:simpleType>

To be on the safe side, use a complex type that contains a sequence of string elements.

7.8 - Defining an Enumeration

If you want a data type that enumerates discrete values you should use a restriction of the schema type xsd:string, enumerating all the values as you would in a Java enum type.

<xsd:simpleType name="IXLType">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="eStwA"/>
    <xsd:enumeration value="eStwS"/>
    <xsd:enumeration value="SpDrL"/>
    <xsd:enumeration value="SpDrS"/>
    <xsd:enumeration value="VGS80"/>
  </xsd:restriction>
</xsd:simpleType>

7.9 - Defining Types for XML Elements Without Content

Types for XML elements are constructed using xsd:complexType, even if they do not have content. The snippet below defines a simple element with two attributes and no sub-elements.

<xsd:complexType name="RouteType">
  <xsd:attribute name="Pos" type="xsd:int" use="optional" default="1"/>
  <xsd:attribute name="Dir" type="DirType" use="required"/>
</xsd:complexType>

7.10 - Defining an Ordered Set of Elements (xsd:sequence)

The schema element xsd:sequence defines that the enclosed set of elements should occur in the given order and according to the specified minimum and maximum repetition counts. (The default for both is 1.) The following complex type defines a set of two coordinates.

<xsd:complexType name="PointType">
  <xsd:sequence>
    <xsd:element name="X" type="xsd:int"/>
    <xsd:element name="Y" type="xsd:int"/>
  </xsd:sequence>
</xsd:complexType>

7.11 - Defining an Unordered Set of Elements (xsd:all)

Content consisting of a set of elements that may occur in any order within its parent XML element can be defined by using the schema element xsd:all. There is, however, a severe restriction: the maxOccurs may not have a value greater than 1. Here is the definition for an XML element describing the courses of a dinner which does not permit repetitions of any course, but you may omit all courses except for the main dish.

<xsd:complexType name="DinnerType">
  <xsd:all>
    <xsd:element name="Starter" type="xsd:string" minOccurs="0"/>
    <xsd:element name="Soup"    type="xsd:string" minOccurs="0"/>
    <xsd:element name="Entree"  type="xsd:string"/>
    <xsd:element name="Dessert" type="xsd:string" minOccurs="0"/>
  </xsd:all>
</xsd:complexType>

7.12 - Defining Alternative Elements (xsd:choice)

The schema element xsd:choice lets you define a type for an XML element which has a content of exactly one element from a given set of alternatives.

<xsd:complexType name="CommType">
  <xsd:choice>
    <xsd:element name="SMS"   type="xsd:string"/>
    <xsd:element name="MMS"   type="xsd:string"/>
    <xsd:element name="Email" type="xsd:string"/>
  </xsd:choice>
</xsd:complexType>

7.13 - Defining a Homogeneous List of Elements

To define an element where some sub-element occurs repeatedly, we make use of the optional attributes minOccurs and maxOccurs. Various combinations are possible, permitting the definition of a list that may be empty, may contain any number of elements, or a fixed number. The definition of an unbounded list with at least two elements is given below. (PointType is shown in subsection Content: An Ordered Set of Elements.)

<xsd:complexType name="PolygonType">
  <xsd:sequence>
    <xsd:element name="Points" type="PointType"
                 minOccurs="2" maxOccurs="unbounded"/>
  </xsd:sequence>
</xsd:complexType>

8 - Documentation / Reference