XSD Restriction / Facets
Restriction is used to define acceptable values for XML elements or attributes. Restrictions on XML elements are referred to as facets.
Value Restriction
The following example defines an element named "age" with a restriction. The value of age cannot be less than 0 or greater than 120:
Set of Values Restriction
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:
The above example can also be written as:
Note: In this case, the type "carType" can be used by other elements, as it is not part of the "car" element.
Sequence of Values Restriction
To restrict the content of an XML element to a sequence of usable numbers or letters, we use the pattern constraint.
The following example defines an element named "letter" with a restriction. The acceptable value is only one lowercase letter from a to z:
The next example defines an element named "initials" with a restriction. The acceptable value is three uppercase letters from A to Z:
The next example also defines an element named "initials" with a restriction. The acceptable value is three letters from a to z, either uppercase or lowercase:
The next example defines an element named "choice" with a restriction. The acceptable value is one letter from x, y, or z:
The next example defines an element named "prodid" with a restriction. The acceptable value is a sequence of five Arabic digits, with each digit ranging from 0-9:
Other Restrictions on a Sequence of Values
The following example defines an element named "letter" with a restriction. The acceptable value is zero or more letters from a to z:
The following example defines an element named "letter" with a restriction. The acceptable value is one or more pairs of letters, each pair consisting of a lowercase letter followed by an uppercase letter. For example, "sToP" would pass this pattern validation, but "Stop", "STOP", or "stop" would not:
The following example defines an element named "gender" with a restriction. The acceptable value is either male or female:
The following example defines an element named "password" with a restriction. The acceptable value is a line of 8 characters, which must be uppercase or lowercase letters from a to z or digits from 0 to 9:
Whitespace Character Restriction
To specify how whitespace characters are to be handled, we use the whiteSpace restriction.
The following example defines an element named "address" with a restriction. This whiteSpace restriction is set to "preserve", meaning the XML processor will not remove any whitespace characters:
This example also defines an element named "address" with a restriction. This whiteSpace restriction is set to "replace", meaning the XML processor will remove all whitespace characters (newline, carriage return, space, and tab):
This example also defines an element named "address" with a restriction. This whiteSpace restriction is set to "collapse", meaning the XML processor will remove all whitespace characters (newline, carriage return, space, and tab will be replaced with a space, leading and trailing spaces will be removed, and multiple consecutive spaces will be reduced to a single space):
Length Restriction
To limit the length of the value in an element, we use the length, maxLength, and minLength restrictions.
This example defines an element named "password" with a restriction. Its value must be exactly 8 characters:
This example also defines an element named "password" with a restriction. Its value must be between 5 and 8 characters:
Data Type Restrictions
Restriction | Description |
---|---|
enumeration | Defines a list of acceptable values |
fractionDigits | Defines the maximum number of decimal places. 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 limit for the value. The allowed value must be less than this value. |
maxInclusive | Defines the upper limit for the value. 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 limit for the value. The allowed value must be greater than this value. |
minInclusive | Defines the lower limit for 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 characters allowed. |
totalDigits | Defines the exact number of Arabic digits allowed. Must be greater than 0. |
whiteSpace | Defines how whitespace characters (newline, carriage return, space, and tab) are handled. |