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

Specifying Element Datatype - "type" Attribute

This section describes a tutorial example on how to specify a datatype in an Element Declaration Component.

An element declared with no specific datatype is too generic to be useful, because it can have any attributes, sub (child) elements, and body contents. To declare an element with a specific datatype, you have 3 options:

  • Using an XML Scheme built-in datatype using the "type" attribute.
  • Using a named datatype using the"type" attribute. The named datatype is defined outside the declaration component.
  • Using an anonymous (inline) datatype defined inside the declaration component.

Example 1, word_builtin_datatype.xsd - Specifying the built-in datatype "string" to the root element "word".

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

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

</xs:schema>

Example 2, word_anonymous_datatype.xsd - Specifying an anonymous (inline) datatype based on the built-in "string" to the root element "word".

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

 <!-- Using an anonymous datatype -->
 <xs:element name="word">
 
 <!-- Defining the anonymous datatype based "string" -->
  <xs:simpleType>
   <xs:restriction base="xs:string">
    <xs:maxLength value="40"/>
   </xs:restriction>
  </xs:simpleType>

 </xs:element>
</xs:schema>

Example 3, word_named_datatype.xsd - Specifying a named datatype, "myString" based on the built-in "string" to the root element "word".

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

 <!-- Using a named datatype "myString" -->
 <xs:element name="word" type="myString"/>

 <!-- Defining the named datatype based on "string" -->
 <xs:simpleType name="myString">
  <xs:restriction base="xs:string">
   <xs:maxLength value="40"/>
  </xs:restriction>
 </xs:simpleType>

</xs:schema>

In all 3 examples above, the "word" element is declared to have a specific datatype, which dictates that the "word" must have a string as its content with no attribute and no sub (child) elements.

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
Specifying Element Datatype - "type" Attribute