"simpleType" Components - User-Defined Simple Datatypes

This section describes the 'simpleType' component, which can be used to define user-defined simple datatypes. There are 3 optional child components supported 'restriction', 'list', 'union'.

If you are happy with those built-in simple datatypes offered in XSD 1.1, you don't need to learn how define your own simple datatypes.

But if you want to be creative, you need to learn the "simpleType" component for creating new simple datatypes. Here is an XSD template showing you how to use the "simpleType" component:

<simpleType name="type_name" final="list|union|restriction|#all">

  <restriction>...</restriction>
  or <union>...</union>
  or <list>...</list>

</simpleType>

Some notes on using the "simpleType" component:

For example, the following XSD document uses "simpleType" components to define 2 new simple datatypes:

<?xml version="1.1"?>
<!-- simpleType_definition_test.xsd
 - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="SimpleType_Definition_Test">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="ZipList" maxOccurs="unbounded">

        <!-- Anonymous "simpleType" using "list" -->
        <xs:simpleType>
          <xs:list itemType="zipType"/>
        </xs:simpleType>

      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

  <!-- Named "simpleType" using "restriction" -->
  <xs:simpleType name="zipType" final="union">
    <xs:restriction base="xs:token">
      <xs:pattern value="\d\d\d\d\d"/>
    </xs:restriction>
  </xs:simpleType>

</xs:schema>

Here is a sample XML document to test the above XSD document:

<?xml version="1.1"?>
<!-- simpleType_definition_test.xml
 - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved.
-->
<SimpleType_Definition_Test>

   <!-- Valid "ZipList" values -->
   <ZipList>01210</ZipList>
   <ZipList>01210 12345
     67890</ZipList>

   <!-- Invalid "ZipList" values -->
   <ZipList>121</ZipList>
   <ZipList>12345 null</ZipList>

</SimpleType_Definition_Test>

When validating this XML document with my XsdSchemaValidator.java program presented earlier in the book, I get these errors:

herong> java XsdSchemaValidator
^^^ simpleType_definition_test.xsd simpleType_definition_test.xml

Error:
   Line number: 13
   Column number: 26
   Message: cvc-pattern-valid: Value '121' is not facet-valid with
   respect to pattern '\d\d\d\d\d' for type 'zipType'.

Error:
   Line number: 13
   Column number: 26
   Message: cvc-type.3.1.3: The value '121' of element 'ZipList' is
   not valid.

Error:
   Line number: 14
   Column number: 33
   Message: cvc-pattern-valid: Value 'null' is not facet-valid with
   respect to pattern '\d\d\d\d\d' for type 'zipType'.

Error:
   Line number: 14
   Column number: 33
   Message: cvc-type.3.1.3: The value '12345 null' of element
   'ZipList' is not valid.

Failed with errors: 4

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

 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

 What Is Simple Datatype?

"simpleType" Components - User-Defined Simple Datatypes

 "simpleType" Components with "restriction" Child Components

 "simpleType" Components with "union" Child Components

 "simpleType" Components with "list" Child Components

 Atomic, List, Atomic Union and List Union Datatypes

 Constraining Facets on List Datatypes

 Constraining Facets on Union Datatypes

 Nested List Datatypes - Not Allowed

 Nested Atomic and List Union 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

 Glossary

 Archived Tutorials

 References

 Full Version in PDF/EPUB