XML Schema Qualifications / Facets
XSD Restrictions / Facets
Restriction is used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.
Qualification of values
The following example defines an element named "age" with a qualification. 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>
Limitations on a set of values
If you need to To limit content to a set of acceptable values, we use enumeration constraints.
The following example defines an element named "car" with a qualification. 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 above example 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.
Limitations on a series of values
If you need to limit the content of an XML element to a series of usable numbers or letters, we need to use a pattern constraint.
The following example defines an element named "letter" with a qualification. 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 qualification. 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 qualification. 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 qualification. The acceptable value is the letter x , one of 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 qualification. Acceptable values are a sequence of five Arabic digits, each in the range 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 qualifications for a range of values
The following example defines With a qualified element named "letter". 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 qualification. Acceptable values are one or more pairs of letters, each consisting of a lowercase letter followed by an uppercase letter. For example, "sToP" will pass validation in this mode, but "Stop", "STOP" or "stop" will 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 qualification. 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 qualification. Acceptable values are a line of 8 characters, which must be uppercase or lowercase letters a - z or numbers 0 - 9:
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9]{8}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Limitations on whitespace characters
If you need to specify the whitespace characters ( whitespace characters), we need to use whiteSpace qualification.
The following example defines an element named "address" with a qualification. The whiteSpace qualification is set to "preserve", which means that 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 qualification. The whiteSpace qualification is set to "replace", which means that the XML processor will remove all whitespace characters (newlines, 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 qualification. The whiteSpace qualification is set to "collapse", which means that the XML processor will remove all whitespace characters (newlines, carriage returns, spaces, and tabs will be replaced with spaces, leading and trailing spaces will be removed, and Multiple consecutive spaces will be reduced to a single space):
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Limitation on length
If you need to limit the length of the value in the element , we need to use length, maxLength and minLength limits.
This example defines an element named "password" with a qualification. The 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 qualification. The minimum value is 5 characters and the maximum value is 8 characters:
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Limitations of data types
Limitations | 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 | Define the upper limit of the value. Allowed values must be less than this value. |
maxInclusive | Define the upper limit of the 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 | Define the lower limit of the value. The allowed value must be greater than this value. |
minInclusive | Define the lower limit of the value. 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 precise number of allowed Arabic digits. Must be greater than 0. |
whiteSpace | Defines how whitespace characters (line feeds, carriage returns, spaces, and tabs) are handled. |