XML Schema Restrictions / Facets
XSD Restrictions / Facets
*
A restriction is used to define acceptable values for XML elements or attributes. A restriction on an XML element is called a facet.
*
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: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
To limit 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 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>
The above example can also be written as:
<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 part of the "car" element itself.
*
Restrictions on a Series of Values
To limit the content of an XML element to a series of permissible numbers or letters, we use the pattern constraint.
The following example defines an element named "letter" with a restriction. The only acceptable values are lowercase letters aβz:
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value=""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The next example defines an element named "initials" with a restriction. The only acceptable values are three uppercase letters AβZ:
<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value=""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The next example also defines an element named "initials" with a restriction. The only acceptable values are three letters, each being either uppercase or lowercase aβz:
<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value=""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The next example defines an element named "choice" with a restriction. The only acceptable values are the letters x, y, or z:
<xs:element name="choice">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value=""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The next example defines an element named "prodid" with a restriction. The only acceptable values are sequences of five Arabic numerals, each ranging from 0β9:
<xs:element name="prodid">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value=""/>
</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:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="()*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The following example defines an element named "letter" with a restriction. The acceptable values consist of one or more pairs of letters, where each pair comprises a lowercase letter followed by an uppercase letter. For example, "sToP" would pass validation against this pattern, but "Stop", "STOP", or "stop" would not:
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="()+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The following example defines an element named "gender" with a restriction. The only acceptable values are "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>
The following example defines an element named "password" with a restriction. The only acceptable values are strings of exactly eight characters, each being either an uppercase or lowercase letter aβz or a digit 0β9:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="{8}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
*
Restrictions on Whitespace Characters
To specify how whitespace characters should be handled, we use the whiteSpace facet.
The following example defines an element named "address" with a restriction. The whiteSpace facet is set to "preserve", meaning that the XML processor will not remove any whitespace characters:
<xs:element name="address">
<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. The whiteSpace facet is set to "replace", meaning that the XML processor will replace all whitespace characters (line feeds, carriage returns, spaces, and tabs) with spaces:
<xs:element name="address">
<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. The whiteSpace facet is set to "collapse", meaning that the XML processor will replace all whitespace characters (line feeds, carriage returns, spaces, and tabs) with single spaces, strip leading and trailing spaces, and collapse multiple consecutive spaces into 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
To restrict the length of the value in an element, we use the length, maxLength, and minLength facets.
This example defines an element named "password" with a restriction. Its value must be exactly eight characters long:
<xs:element name="password">
<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 five characters and at most eight characters long:
<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>
*
Facets for Data Types
| Facet | 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 for numeric values. The allowed value must be less than this value. |
| maxInclusive | Defines the upper bound for numeric values. The allowed value 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 for numeric values. The allowed value must be greater than this value. |
| minInclusive | Defines the lower bound for numeric values. The allowed value 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 numerals allowed. Must be greater than 0. |
| whiteSpace | Defines how whitespace characters (line feeds, carriage returns, spaces, and tabs) are handled. |
YouTip