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

Built-in Datatype - "string"

This section describes what is built-in datatype string and its restriction facets.

The most commonly used built-in datatype is the "string" datatype, which represents a sequence of characters defined in the specified character set.

The "string" datatype can be used as is to accept any generic string value. But it can also be restricted to derive new datatypes using the following constraining facets:

  • length - Specifies a fixed length of the string datatype.
  • minLength - Specifies a minimum length of the string datatype.
  • maxLength - Specifies a maximum length of the string datatype.
  • pattern - Specifies a pattern for the string datatype.
  • enumeration - Specifies a list of string values for the string datatype.
  • whiteSpace - Specifies how white spaces to be handled based on 3 options: "preserve", "replace", or "collapse".

Here is a sample schema, string_datatype.xsd, that uses "string" datatype with different restriction facets:

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

 <xs:element name="canadaAddress">
  <xs:complexType>

   <!-- Using "minLength" to restrict the string -->
   <xs:attribute name="street">
    <xs:simpleType>
     <xs:restriction base="xs:string">
      <xs:minLength value="5"/>
     </xs:restriction>
    </xs:simpleType>
   </xs:attribute>

   <!-- Using "whiteSpace" to restrict the string -->
   <xs:attribute name="city">
    <xs:simpleType>
     <xs:restriction base="xs:string">
      <xs:whiteSpace value="collapse"/>
     </xs:restriction>
    </xs:simpleType>
   </xs:attribute>

   <!-- Using "length" to restrict the string -->
   <xs:attribute name="provinceCode">
    <xs:simpleType>
     <xs:restriction base="xs:string">
      <xs:length value="2"/>
     </xs:restriction>
    </xs:simpleType>
   </xs:attribute>

   <!-- Using "pattern" to restrict the string -->
   <xs:attribute name="zip">
    <xs:simpleType>
     <xs:restriction base="xs:string">
      <xs:pattern value="[A-Z][0-9][A-Z] [0-9][A-Z][0-9]"/>
     </xs:restriction>
    </xs:simpleType>
   </xs:attribute>

  </xs:complexType>

 </xs:element>

</xs:schema>

Here is a sample XML document, string_datatype.xml, that conforms to string_datatype.xsd:

<?xml version="1.0"?>
<canadaAddress
 street="770 Don Mills"
 city="Toronto"
 provinceCode="ON"
 zip="M3C 1T3"
/>

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

 XML Parser API - Xerces2 Java Parser

 XML Schema Language - Basics

XML Schema Built-in Datatypes

 Overview of XML Schema Built-in Datatypes

 List of Built-in Datatypes

 Deriving from Built-in Datatypes - simpleType

Built-in Datatype - "string"

 Built-in Datatype - "string" Errors

 Built-in Datatype - "dateTime"

 Built-in Datatype - "dateTime" Errors

 Built-in Datatype - "decimal"

 Built-in Datatype - "decimal" Errors

 Complex Element Declaration

 XML Schema Location and Namespace in XML Documents

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together

 Glossary

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2009
Built-in Datatype - "string"