Skip to main content

XSD Restrictions

XML Restrictions are used to define acceptable values for XML elements or attributes.

Restrictions on XML elements are called facets.

Restrictions for Datatypes

The following table lists the restrictions for different datatypes:

ConstraintDescription
enumerationDefines a list of acceptable values
fractionDigitsSpecifies the maximum number of decimal places allowed. Must be equal to or greater than zero
lengthSpecifies the exact number of characters or list items allowed. Must be equal to or greater than zero
maxExclusiveSpecifies the upper bounds for numeric values (the value must be less than this value)
maxInclusiveSpecifies the upper bounds for numeric values (the value must be less than or equal to this value)
maxLengthSpecifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
minExclusiveSpecifies the lower bounds for numeric values (the value must be greater than this value)
minInclusiveSpecifies the lower bounds for numeric values (the value must be greater than or equal to this value)
minLengthSpecifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
patternDefines the exact sequence of characters that are acceptable
totalDigitsSpecifies the exact number of digits allowed. Must be greater than zero
whiteSpaceSpecifies how white space (line feeds, tabs, spaces, and carriage returns) is handled

Example of Restrictions

Restrictions on Values

Example of restrictions on values:

<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Restrictions on a Set of Values

The enumeration constraint is used to limit the content of an XML element to a set of acceptable values.

Example of enumeration constraint on car element where only acceptable values are "Audi", "Golf", "BMW":

<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

An equivalent way to write the above example is as follows:

<xs:element name="car" type="carType"/>

<xs:simpleType name="carType">
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
note

In this case the type carType can be used by other elements because it is not a part of the car element.

Restrictions on a Series of Values

The pattern constraint is used to limit the content of an XML element to define a series of numbers or letters that can be used.

Example of pattern constraint on letter element where only acceptable value is one of the lowercase letters from a to z:

<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Example of pattern constraint on initials element where only acceptable value is three of the uppercase letters from a to z:

<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Example of pattern constraint on initials element where only acceptable value is three of the lowercase or uppercase letters from a to z:

<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Example of pattern constraint on choice element where only acceptable value is one of the following letters a, b or c:

<xs:element name="choice">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[abc]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Example of pattern constraint on prodid element where only acceptable value is five digits in a sequence, and each digit must be in a range from 0 to 9:

<xs:element name="prodid">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Example of pattern constraint on letter element where only acceptable value is zero or more occurrences of lowercase letters from a to z:

<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Example of pattern constraint on letter element where only acceptable value is one or more pairs of letters, consisting of a lower case letter followed by an uppercase letter.

<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z][A-Z])+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Example of pattern constraint on gender element where only acceptable value is male or female:

<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Example of pattern constraint on password element where there must be exactly eight characters in a row and those characters must be lowercase or uppercase letters from a to z, or a number from 0 to 9:

<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9]{8}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Restrictions on Whitespace Characters

The whiteSpace constraint is used to specify how whitespace characters should be handled.

Example of whiteSpace constraint on address element where the whiteSpace constraint is set to preserve, which means that the XML processor will not remove any white space characters:

<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Example of whiteSpace constraint on address element where the whiteSpace constraint is set to replace, which means that the XML processor will replace all white space characters (line feeds, tabs, spaces, and carriage returns) with spaces:

<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Example of whiteSpace constraint on address element where the whiteSpace constraint is set to collapse, which means that the XML processor will remove all white space characters (line feeds, tabs, spaces, carriage returns are replaced with spaces, leading and trailing spaces are removed, and multiple spaces are reduced to a single space):

<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Restrictions on Length

The length, maxLength and minLength constraints are used to limit the length of a value in an element.

Example of length constraint on password element where the value must be exactly eight characters:

<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Example of maxLength and minLength constraints on password element where the value must be minimum five characters and maximum eight characters:

<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>