Schema Restrictions
Restrictions define acceptable values for elements and attributes. If a Schema
restricts the value of an element to a certain length or certain value, the
element cannot contain text that violates the restriction.
The most common types of restrictions include value restrictions, sets of values
restrictions, and length restrictions. Restrictions are defined within the
element definition. The code block below shows the general format for applying
a restriction to an existing element.
Element Restrictions
<xsd:element name="element name">
<xsd:simpleType>
<xsd:restriction base="xsd: Integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="10"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
Listing 3-14. Element restrictions.
Restrictions are defined within a <xsd:simpleType>
block.
Here the element is restricted to an integer value between 0 and 10 inclusive.
minInclusive
and maxInclusive
are contstraints
that define the possible values that can be assigned to the element. Other
common contsraints are listed in the table below.
Schema Datatype Restrictions
Constraint |
Description |
enumeration |
Defines a list of acceptable values |
length |
Specifies the exact number of characters or list items
allowed. Must be equal to or greater than zero |
maxExclusive |
Specifies the upper bounds for numeric values (the value
must be less than this value) |
maxInclusive |
Specifies the upper bounds for numeric values (the value
must be less than or equal to this value) |
maxLength |
Specifies the maximum number of characters or list items
allowed. Must be equal to or greater than zero |
minExclusive |
Specifies the lower bounds for numeric values (the value
must be greater than this value) |
minInclusive |
Specifies the lower bounds for numeric values (the value
must be greater than or equal to this value) |
minLength |
Specifies the minimum number of characters or list items
allowed. Must be equal to or greater than zero |
totalDigits |
Specifies the exact number of digits allowed. Must be
greater than zero |
pattern |
Defines the exact sequence of characters that are acceptable |
In the next example, the enumeration
constraint is used to limit
the values for the Size
element. The values for the
Size
element are limited to small, medium, or large.
<xsd:element name="Size">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="small"/>
<xsd:enumeration value="medium"/
<xsd:enumeration value="large"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
Listing 3-15. enumeration constraint.
The example below uses the pattern
constraint to place
restrictions on the id
element. The restriction specifies
that the value of the id element must be an integer and four digits in length.
The first digit can be any number between 1 and 9, the remaining digits can
be any number between 0 and 9.
<xsd:element name="id">
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:pattern value="[1-9][0-9][0-9][0-9]"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
Listing 3-16. pattern constraint.