XSD Restrictions / Facets
Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.
Restrictions on Values
The following example defines an element named "age" with a restriction. The value of age cannot be lower than 0 or higher than 120:
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restriction on a Set of Values
To restrict the content of an XML element to a set of acceptable values, we use the enumeration constraint.
The following example defines an element named "car" with a restriction. The acceptable values are only: Audi, Golf, BMW:
<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>
The example above can also be written as:
<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 part of the "car" element.
Restriction on a Series of Values
To restrict the content of an XML element to a series of usable numbers or letters, we use the pattern constraint.
The following example defines an element named "letter" with a restriction. The acceptable values are only one of the lowercase letters a - z:
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The next example defines an element named "initials" with a restriction. The acceptable values are three of the uppercase letters A - Z:
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The next example also defines an element named "initials" with a restriction. The acceptable values are three of the uppercase or lowercase letters a - z:
<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>
The next example defines an element named "choice" with a restriction. The acceptable values are one of the letters x, y, or z:
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[xyz]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The next example defines an element named "prodid" with a restriction. The acceptable values are a sequence of five Arabic digits, with each digit ranging from 0-9:
<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>
Other Restrictions on a Series of Values
The following example defines an element named "letter" with a restriction. The acceptable values are zero or more letters from a - z:
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The following example defines an element named "letter" with a restriction. The acceptable values are one or more pairs of letters, each pair consisting of a lowercase letter followed by an uppercase letter. For example, "sToP" would pass validation of this pattern, but "Stop", "STOP", or "stop" would not pass validation:
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z][A-Z])+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The following example defines an element named "gender" with a restriction. The acceptable values are male or female:
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The following example defines an element named "password" with a restriction. The acceptable values are a line of characters consisting of 8 characters, which must be uppercase or lowercase letters a - z or digits 0 - 9:
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9]{8}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restriction on Whitespace Characters
To specify how whitespace characters are handled, we need to use the whiteSpace restriction.
The following example defines an element named "address" with a restriction. This whiteSpace restriction is set to "preserve", which means the XML processor will not remove any whitespace characters:
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
This example also defines an element named "address" with a restriction. This whiteSpace restriction is set to "replace", which means the XML processor will remove all whitespace characters (line feeds, carriage returns, spaces, and tabs):
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
This example also defines an element named "address" with a restriction. This whiteSpace restriction is set to "collapse", which means the XML processor will remove all whitespace characters (line feeds, carriage returns, spaces, and tabs are replaced with spaces, leading and trailing spaces are removed, and multiple consecutive spaces are reduced to a single space):
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restrictions on Length
To restrict the length of a value in an element, we need to use the length, maxLength, and minLength restrictions.
This example defines an element named "password" with a restriction. Its value must be exactly 8 characters:
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
This example also defines an element named "password" with a restriction. Its value must be at least 5 characters and at most 8 characters:
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Data Type Restrictions
| Restriction | Description |
|---|---|
| enumeration | Defines a list of acceptable values |
| fractionDigits | Defines the maximum number of decimal places allowed. Must be greater than or equal to 0. |
| length | Defines the exact number of characters or list items allowed. Must be greater than or equal to 0. |
| maxExclusive | Defines the upper bound of a value. Allowed values must be less than this value. |
| maxInclusive | Defines the upper bound of a value. Allowed values must be less than or equal to this value. |
| maxLength | Defines the maximum number of characters or list items allowed. Must be greater than or equal to 0. |
| minExclusive | Defines the lower bound of a value. Allowed values must be greater than this value. |
| minInclusive | Defines the lower bound of a value. Allowed values must be greater than or equal to this value. |
| minLength | Defines the minimum number of characters or list items allowed. Must be greater than or equal to 0. |
| pattern | Defines the exact sequence of acceptable characters. |
| totalDigits | Defines the exact number of Arabic digits allowed. Must be greater than 0. |
| whiteSpace | Defines how whitespace characters (line feeds, carriage returns, spaces, and tabs) are handled. |