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: