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

Defining New Datatypes with Built-in Datatypes

This section describes how to define or contruct new datatypes using built-in datatypes with a simple XSD document example.

When built-in datatypes are used to define (or construct) new datatypes, we need to remember these rules:

  • All built-in datatype names must be qualified with namespace of xmlns:xs="http://www.w3.org/2001/XMLSchema", where "xs" can be replace with any short names.
  • Complex built-in datatype (only 1 in XSD 1.1: "anyType") is alowed only to to be used to define new complex datatypes.
  • "anyType" (the only complex built-in datatype in XSD 1.1) is alowed to be used as the base datatype for "restriction" construction components.
  • "anyType" is not alowed to be used as the base datatype for "extension" construction components, since "anyType" is the largest/biggest complex datatype.
  • Simple built-in datatypes (49 of them in XSD 1.1) are alowed to be used to define new simple and complex datatypes.
  • Simple built-in datatypes are allowed to be used as the base type for "restriction" construction components, member datatypes for "union" construction structions, or the item datatype for "list" construction components to define new simple datatypes.
  • Simple built-in datatypes are allowed to be used as the base type for "extension" construction components to define new complex datatypes.

Here is a simple XSD document example on how to construct or define new datatypes using built-in datatypes:

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

 <!-- Using simple built-in datatype to define new simpleType -->
 <xs:simpleType name="zipType">
  <xs:restriction base="xs:string">
   <xs:maxLength value="5"/>
  </xs:restriction>
 </xs:simpleType>

 <!-- Using complex built-in datatype to construct new complexType -->
 <xs:complexType name="tableType">
  <xs:complexContent>
   <xs:restriction base="xs:anyType">
    <xs:sequence>
    <xs:element name="tr" type="xs:anyType" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="border" type="xs:string"/>
   </xs:restriction>
  </xs:complexContent>
 </xs:complexType>

<!-- Using simple built-in datatype to construct new complexType -->
 <xs:complexType name="length">
  <xs:simpleContent>
   <xs:extension base="xs:integer">
    <xs:attribute name="unit" type="xs:string"/>
   </xs:extension>
  </xs:simpleContent>
 </xs:complexType>

</xs:schema>

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

 Overview of XSD 1.1 Built-in Datatypes

 List of Built-in Datatypes

 Datatypes, Values and Representations

 Datatypes, Values and Representations - Example

 Built-in Datatypes Lexical Representation Examples

 Declaring Elements and Attributes with Built-in Datatypes

Defining New Datatypes with Built-in Datatypes

 "anyType" Built-in Datatype Values and Representations

 "anySimpleType" Built-in Datatype Values and Representations

 "anyAtomicType" Built-in Datatype Values and Representations

 "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

 Glossary

 References

 PDF Printing Version

Defining New Datatypes with Built-in Datatypes - Updated in 2014, by Dr. Herong Yang