XML Schema Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.11

Using XML Schema Built-in Datatypes

This section describes what is a built-in datatype and how to use a built-in datatype.

XML Schema 1.0 specification provides about 46 built-in datatypes. Here are some examples of built-in datatypes:

  • float - Floating numbers, like "1267.43233E12", "12.78e-2", "12" , "0", ...
  • integer - Integer numbers, like "-1", "0", "12678967543233", "+100000", ...
  • date - Date only, like "2002-12-23".
  • time - Time only, like "14:19:00.000-05:00".
  • string - A sequence of any Unicode characters

Rule 1. All built-in datatypes are simple datatypes, which do not allow any attributes or sub (child) elements if it is used in an element declaration component.

Rule 2. Build-in datatype names have the namespace name of "http://www.w3.org/2001/XMLSchema".

Here is a sample schema, holiday.xsd, that uses the built-in datatype "date":

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- holidat.xsd
 - Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
-->

 <!-- Using XML Schema built-in datatype "date" -->
 <xs:element name="holiday" type="xs:date"/>

</xs:schema>

The following XML document, holiday.xml, conforms to holiday.xsd:

<?xml version="1.0"?>
<holiday>2007-01-01</holiday>

The following XML document, holiday_error.xml, does not conform to holiday.xsd, because the content of "holiday" element is not a "date" value:

<?xml version="1.0"?>
<holiday>New Year Day</holiday>

Sections in This Chapter

Schema and Schema XML Representation

Checking Schema Documents - XsdSchemaChecker.java

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 again Schema Documents

Deriving New Simple Datatypes - "simpleType" Element

Defining Complex Datatypes - "complexType" Element

Validation Error Examples on Complex Datatypes

Dr. Herong Yang, updated in 2009
Using XML Schema Built-in Datatypes