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

What Is Simple Datatype?

This section describes simple datatypes. What is a simple datatype? How many built-in simple datatypes provided in XSD 1.1? Can I define my own simple datatypes?

What Is Simple Datatype? Simple Datatype is a datatype who's values are simple and have no XML structure. Simple datatypes can be used to declare:

  • XML Attributes.
  • XML Elements that have no attributes and no sub elements.

There are 2 sources of simple datatypes:

  • Built-in Simple Datatypes - XSD 1.1 offers 47 built-in simple datatypes, including "string", "decimal", "integer", "byte", "float", "date", "time", "hexBinary", etc..
  • User-defined Simple Datatypes - XSD 1.1 supports type definition components for you to define your own simple datatypes.

For example, the following XSD document uses a built-in simple datatype and a user-defined simple datatype to declare XML elements:

<?xml version="1.1"?>
<!-- simple_datatype_test.xsd
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Simple_Datatype_Test">
  <xs:complexType>
    <xs:sequence>
    
      <!-- Using built-in simple datatype -->   
      <xs:element name="Street" type="xs:string" 
         maxOccurs="unbounded"/>
        
      <!-- Using user-defined simple datatype -->   
      <xs:element name="ZIP" maxOccurs="unbounded">
        <xs:simpleType>
          <xs:restriction base="xs:token">
            <xs:pattern value="\d\d\d\d\d"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>

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

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

<?xml version="1.1"?>
<!-- simple_datatype_test.xml
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<Simple_Datatype_Test>
   
   <!-- Valid "Street" values -->
   <Street>500 Main Street</Street>

   <!-- Invalid "Street" values -->
   <Street>500 <b>Main</b> Street</Street>

   <!-- Valid "ZIP" values -->
   <ZIP>01210</ZIP>

   <!-- Invalid "ZIP" values -->
   <ZIP>01210<br/></ZIP>
   
</Simple_Datatype_Test>

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

c:\Progra~1\Java\jdk1.7.0_07\bin\java XsdSchemaValidator 
   simple_datatype_test.xsd simple_datatype_test.xml

Error:
   Line number: 11
   Column number: 43
   Message: cvc-type.3.1.2: Element 'Street' is a simple type, so it 
   must have no element information item [children]. 
   (Child element is not allowed)

Error:
   Line number: 17
   Column number: 25
   Message: cvc-type.3.1.2: Element 'ZIP' is a simple type, so it must
   have no element information item [children].
   (Child element is not allowed)

Error:
   Line number: 17
   Column number: 25
   Message: cvc-pattern-valid: Value '' is not facet-valid with 
   respect to pattern '\d\d\d\d\d' for type 
   '#AnonType_ZIPSimple_Datatype_Test'.

Error:
   Line number: 17
   Column number: 25
   Message: cvc-type.3.1.3: The value '' of element 'ZIP' is not 
   valid.

Failed with errors: 4

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

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

 References

 PDF Printing Version

What Is Simple Datatype? - Updated in 2014, by Dr. Herong Yang