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:

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) 2002-2013 HerongYang.com. 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) 2002-2013 HerongYang.com. 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) 2002-2013 HerongYang.com. 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.

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

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

 Deriving New Simple Datatypes - "simpleType" Element

 Defining Complex Datatypes - "complexType" Element

 Validation Error Examples on Complex Datatypes

 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

 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