XSD Tutorials - Herong's Tutorial Examples - Version 5.10, by Dr. Herong Yang

Defining a Derived Datatype from a Base Datatype

This section describes a tutorial example on how to define complex base datatype and derived datatypes.

Sometimes when you declaring an element in a schema, you want to declare it with a default datatype and offer some optional derived datatypes. The conforming XML documents can then use the special attribute "type="derived-datatype"" to override the base datatype with an optional derived datatype.

For example, a "contact" element may be declared using a base datatype "contactType" that captures only a "name" child element.

But you may want to create two derived datatypes:

  • "phoneContactType" - A "contactType" with an extra child element "phone".
  • "emailContactType" - A "contactType" with an extra child element "email".

With the base datatype and two derived datatypes, a "contact" element can be constructed in three ways now:

  • Staying with the base datatype "contactType" - "name" only.
  • Overriding the base datatype with "phoneContactType" - "name" and "phone"
  • Overriding the base datatype with "emailContactType" - "name" and "email"

Rule 1. A derived datatype must be defined with a "restriction" or "extension" component on the base datatype.

Here is a good sample schema, directory.xsd, that defines a base datatype and two derived datatypes:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- order.xsd
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->

 <xsd:element name="directory">
  <xsd:complexType>
   <xsd:sequence>
    <xsd:element name="contact" type="contactType"
       minOccurs="0" maxOccurs="unbounded"/>
   </xsd:sequence>
  </xsd:complexType>
 </xsd:element>

<!-- Defining the base datatype -->
 <xsd:complexType name="contactType">
   <xsd:sequence>
    <xsd:element name="name" type="xsd:string"/>
   </xsd:sequence>
 </xsd:complexType>

<!-- Defining a derived datatype -->
 <xsd:complexType name="phoneContactType">
  <xsd:complexContent>
   <xsd:extension base="contactType">
    <xsd:sequence>
     <xsd:element name="phone" type="xsd:string"/>
    </xsd:sequence>
   </xsd:extension>
  </xsd:complexContent>
 </xsd:complexType>

<!-- Defining a derived datatype -->
 <xsd:complexType name="emailContactType">
  <xsd:complexContent>
   <xsd:extension base="contactType">
    <xsd:sequence>
     <xsd:element name="email" type="xsd:string"/>
    </xsd:sequence>
   </xsd:extension>
  </xsd:complexContent>
 </xsd:complexType>

<!-- Defining a new datatype -->
 <xsd:complexType name="webContactType">
   <xsd:sequence>
    <xsd:element name="name" type="xsd:string"/>
    <xsd:element name="url" type="xsd:string"/>
   </xsd:sequence>
 </xsd:complexType>

</xsd:schema>

Notice that an extra datatype "webContactType" is defined in the schema to test an error condition described in the next section. Tutorials and example XML documents on how to override base datatypes with derived datatypes are given in the next section.

Last update: 2013.

Table of Contents

 About This Book

 Introduction to XML Schema

 XML Editor and Schema Processor - XMLPad

 Java API for XML Processing - JAXP

 JAXP - XML Schema (XSD) Validation

 Xerces2 Java Parser - Java API of XML Parsers

 Using Xerces2 Java API

 XML Schema Language - Basics

 Introduction of XSD Built-in Datatypes

 "string" and Its Derived Datatypes

 "decimal" and Its Derived Datatypes

 "dateTime" and Its Related Datatypes

 Miscellaneous Built-in Datatypes

 Facets, Constraining Facets and Restriction Datatypes

 "simpleType" - Defining Your Own Simple Datatypes

 Complex Element Declaration

 Identity-Constraints: unique, key and keyref

 Assertion as Custom Validation Rules

 XML Schema Location and Namespace in XML Documents

Overriding Element Types in XML Documents

 Overriding Element Type to Empty Content - nillable

 Overriding Element Type to Empty Content - xsi:nil

Defining a Derived Datatype from a Base Datatype

 Overriding the Base Datatype - xsi:type

 Overriding the Base Datatype - Errors

 Linking Multiple Schema Documents Together

 Glossary

 References

 PDF Printing Version

Defining a Derived Datatype from a Base Datatype - Updated in 2014, by Dr. Herong Yang