Defining XML Elements and Their Types in XML Schema

This material is prepared based on the book "XML", second edition, by Kevin Howard Goldberg.

XML Schema defines two types of elements:
  1. Simple type elements: can only contain a value and not child elements or attributes.
  2. Complex type elements: can contain child elements, attributes, or some combination of the two. Complex elements are further divided into simple content and complex content elements. Both can have attributes, but simple content only allows string content, whereas complex content allows child elements.

Simple type elements use simple types . Please see Table 1 for details of simple types and their usage in defining simple type elements.

Complex type elements use complex types . Please see Table 2 for details of complex types and their usage in defining complex type elements.

Table 1. Simple Types
Simple Types

Built-in Simple Types

XML schema includes a collection of built-in simple types for text. These include string, boolean values, URLs, various date and time format, and numbers.

Names of built-in simple types:

  • xs:string
  • xs:decimal
  • xs:boolean
  • xs:time
  • xs:date
  • xs:anyURI

Syntax for defining an element using the built-in simple type:
       <xs:element name="label" type="xs:string|xs:decimal|xs:boolean|xs:time|xs:date|xs:anyURL"/>
     

| - means "OR".

Derived Custom Simple Types

By applying restrictions, or facets, on the built-in simple types, users can derive their own custom simple types.

Anonymous Custom Type

An Anonymous Custom Type is defined inside an element definition. It can only be used for the element in which it is contained.

Syntax for deriving an anonymous custom type:
     <xs:element name="label"> 
        <xs:simpleType>
          <xs:restriction base="foundation">
              <!--Specify as many restrictions (or facets) as necessary -->
             <!-- For example: -->
             <xs:minInclusive value="0"/>
             <xs:maxExclusive value="100"/> 

           </xs:restriction>
        </xs:simpleType>
     </xs:element>
     
  • "label" is the name of XML element
  • "foundation" is any of the built-in simple types
Named Custom Type

If a custom type is used to define more than one element in an XML Schema, the custom type should be named and placed separately.

Syntax for deriving a named custom type:
        <xs:simpleType name="custom_type_name">
          <xs:restriction base="foundation">
              <!--Specify as many restrictions (or facets) as necessary -->
             <!-- For example: -->
             <xs:minInclusive value="0"/>
             <xs:maxExclusive value="100"/>

           </xs:restriction>
        </xs:simpleType>
     
  • "custom_type_name" identifies the new custom simple type
  • "foundation" is any of the built-in simple types
Syntax for defining an element using the named custom type:
       <xs:element name="label" type="custom_type_name"/>
     

Tips:

Table 2. Complex Types
Complex Types

Four complex types define complex type elements. "Text only" complex type defines the structure of complex type elements with simple content. The other three complex types define the structure of complex type elements with complex content.

The default derivation for complex type is:

complex content that restricts anyType

Based on the default condition, XML schema elements xs:complexContent and xs:anyType can be omitted from the XML Schema definitions of complex types .

Complex Type 1 - "text only" Complex Type

This kind of complex type supports adding attribute types to simple types.

Syntax for deriving a "text only" complex type:

        <xs:complexType name="complex_type_name">
         <xs:simpleContent>

          <xs:extension base="foundation">
              <!--Specify as many attributes as necessary -->
             <!-- For example: -->
             <xs:attribute name="semester" type = "xs:string"/>
            
           </xs:extension>

                OR 

          <xs:restriction base="foundation">
              <!--Specify as many restrictions (or facets) as necessary -->
             <!-- For example: -->
             <xs:minInclusive value="0"/>
             <xs:maxExclusive value="100"/> 

           </xs:restriction>

        </xs:simpleContenT>
       </xs:complexType>
     

The above is for named type. Defining an anonymous type is similar to the syntax in simple types. The extension element extends an existing simpleType or complexType element.

Example from W3C.

Complex Type 2 - "element only" Complex Type

Allows children and attributes.
The children can be specified to appear in order , or to appear in in any order .
It is also possible to specify that the complex type element contain only one child element (or a group of elements).

Syntax for deriving an "elements only" complex type with ordered elements:
        <xs:complexType name="complex_type_name">
          <xs:sequence> 
            
            <!- Declare the simple or complex type elements in the order in which they should appear. -->
            
            <xs:element name="child_element_name" type="child_element_type" />
               .
               .
               .
          </xs:sequence> 

       </xs:complexType>
     

Tips:
  • A sequence can contain one element.
  • A sequence can contain other sequences or choices.
  • The <xs:sequence> element is equal the the comma (,) in DTDs.

Syntax for deriving an "elements only" complex type with unordered elements:
        <xs:complexType name="complex_type_name">
          <xs:all> 
            
            <!- Declare the simple or complex type elements. Order does not matter. -->
            
            <xs:element name="child_element_name" type="child_element_type" />
               .
               .
               .
          </xs:all> 

       </xs:complexType>
     

Tips:
  • No element may appear more than once.
  • The <xs:all> element must be the sole child of an "element only" complex type. It is not used anywhere else.

Syntax for deriving an "elements only" complex type that can contain one child element or another:
        <xs:complexType name="complex_type_name">
          <xs:choice> 
            
            <!- Declare the simple or complex type elements. -->
            
            <xs:element name="child_element_name" type="child_element_type" />
               .
               .
               .
          </xs:choice> 

       </xs:complexType>
     

Tips:
  • A set of choices can contain sequences or additional choice sets.
  • The <xs:choice> element is equal the the vertical bars in DTDs.
Complex Type 3 - "empty element" Complex Type

Contains only attributes.

Syntax for deriving an "empty element" complex type:

        <xs:complexType name="complex_type_name">
           
           <!- Declare attributes -->
           
       </xs:complexType>
     

An anonymous empty complex type that does not contain any attribute:

       <xs:complexType/>
     

XML schema elements xs:complexContent and xs:anyType can be omitted because they are the default for the complex types. Example.

Complex Type 4 - "mixed content" Complex Type

This type defines a complex element with both simple content and complex content. It allows text, child elements, and attributes.

Syntax for deriving a "mixed content" complex type:
        <xs:complexType name="complex_type_name" mixed="true">
            <!- Declare a sequence, an unordered list, or a choice to specify the child elements and structure
             within the complex type. -->
           <!-- For example:
          <xs:choice> 
            
            
            <xs:element name="child_element_name" type="child_element_type" />
               .
               .
               .
          </xs:choice> 
           -->


            <!- Declare the attributes that should appear in this complex type element, if any. -->

       </xs:complexType>
     

See an example here.

Tips:
  • Mixed content elements are ideal for descriptive, text-based information.
  • Not suitable for database-type applications.

© Aynur Abdurazik, 2009, all rights reserved. This document is made available for use by GMU graduate students of SWE 642. Copying, distribution or other use of this document without express permission of the author is forbidden. You may create links to pages in this web site, but may not copy all or part of the text without permission of the author.