Deriving New Simple Datatypes - "simpleType" Element

This section describes a tutorial example on how to define a simple datatype based on a built-in datatype by adding restriction facets.

Sometime, you want to modify a built-in datatype by adding some restrictions. This can be done by using the Simple Type Definition Component.

For example, I want design a schema to validate an XML element called "age". I could use the built-in datatype "nonNegativeInteger". But I want restrict the value to be less or equal to 150. I can use the Simple Type Definition Component to modify "nonNegativeInteger" in this case.

Rule 1. The Simple Type Definition Component allows you to define a new simple datatype based on an existing simple datatype, including any built-in datatype.

Rule 2. The XML representation of a Simple Type Definition Component is an "simpleType" element, which must have one sub element of "restriction", "list", or "union". The sub element provides base datatypes and modification details.

Rule 3. A "simpleType" element must have "name" attribute, if it is used to define a named datatype.

Here is the schema document, age.xsd. It defines a derived simple datatype "ageType", which is derived from the built-in datatype "nonNegativeInteger" with a restriction of a maximum value.

<?xml version="1.0"?>
<xs:schema xmlns:xs="">
<!-- age.xsd
 - Copyright (c) 2002-2013 All Rights Reserved.

 <!-- Using a derived simple datatype -->
 <xs:element name="age" type="ageType"/>

 <!-- Defining "ageType" based on "string" -->
 <xs:simpleType name="ageType">
  <xs:restriction base="xs:nonNegativeInteger">
   <xs:maxInclusive value="150"/>


Obviously, the following XML document, age_error.xml, does not conform with the schema document, age.xsd:

<?xml version="1.0"?>

Here is the output of

herong> java XsdSchemaValidator age.xsd age_error.xml
   Line number: 2
   Column number: 15
   Message: cvc-maxInclusive-valid: Value '200' is not facet-valid
   with respect to maxInclusive '150' for type 'ageType'.

   Line number: 2
   Column number: 15
   Message: cvc-type.3.1.3: The value '200' of element 'age' is not

Failed with errors: 2

Note that the "maxInclusive" sub element is called a restriction "facet". Different built-in datatypes support different sets of facets. For more information, read the built-in datatype chapter in this book.

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 APIs

XML Schema Language - Basics

 Schema and Schema XML Representation

 Checking Schema Documents -

 Creating Schema Documents - "schema" Element

 Declaring Root Elements - "element" Element

 Specifying Element Datatype - "type" Attribute

 Using XML Schema Built-in Datatypes

 Using XML Schema Built-in Datatypes Incorrectly

 Validating XML Documents against Schema Documents

Deriving New Simple Datatypes - "simpleType" Element

 Defining Complex Datatypes - "complexType" Element

 Validation Error Examples on Complex Datatypes

 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

 Linking Multiple Schema Documents Together


 Archived Tutorials


 Full Version in PDF/EPUB