XML Schema Restrictions
Built-in data types like xs:string and xs:integer are broad. A string accepts any text — but what if you only want a 6-digit number, or a color that must be "red," "green," or "blue"? Restrictions in XML Schema let you narrow a base type down to exactly the range or pattern your data requires.
Think of restrictions like a visa application form. The form has a field for age — but the validator only accepts values between 18 and 65. The base type is "number," but the restriction narrows it to a specific range.
xs:restriction Syntax
<xs:simpleType name="TypeName">
<xs:restriction base="xs:baseType">
<!-- constraining facets go here -->
</xs:restriction>
</xs:simpleType>
Constraining Facets
Facets are the rules inside an xs:restriction that limit the allowed values. Different base types support different facets.
1. Enumeration — List of Allowed Values
Enumeration restricts a value to a fixed set of options.
<xs:simpleType name="TrafficLightType">
<xs:restriction base="xs:string">
<xs:enumeration value="red"/>
<xs:enumeration value="yellow"/>
<xs:enumeration value="green"/>
</xs:restriction>
</xs:simpleType>
Valid
<signal>green</signal>
Invalid
<signal>blue</signal>
2. minInclusive and maxInclusive — Range with Boundaries Included
<xs:simpleType name="AgeType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="18"/>
<xs:maxInclusive value="65"/>
</xs:restriction>
</xs:simpleType>
Both 18 and 65 are valid. Values below 18 or above 65 fail validation.
3. minExclusive and maxExclusive — Range with Boundaries Excluded
<xs:simpleType name="PositiveDecimalType">
<xs:restriction base="xs:decimal">
<xs:minExclusive value="0"/>
</xs:restriction>
</xs:simpleType>
Zero is not valid. Any number greater than zero passes. This is useful for prices — a price of 0 is not allowed.
4. minLength and maxLength — String Length Limits
<xs:simpleType name="PasswordType">
<xs:restriction base="xs:string">
<xs:minLength value="8"/>
<xs:maxLength value="20"/>
</xs:restriction>
</xs:simpleType>
Passwords must be between 8 and 20 characters. "abc" is too short. A 25-character string is too long.
5. length — Exact Length
<xs:simpleType name="IndianPINCode">
<xs:restriction base="xs:string">
<xs:length value="6"/>
</xs:restriction>
</xs:simpleType>
The postal code must be exactly 6 characters. "110001" is valid. "11000" is invalid.
6. pattern — Regular Expression Format
Pattern uses a regular expression to define the allowed format. This is the most flexible facet.
Phone Number Pattern
<xs:simpleType name="IndianPhoneType">
<xs:restriction base="xs:string">
<xs:pattern value="\+91-[0-9]{10}"/>
</xs:restriction>
</xs:simpleType>
Valid: +91-9876543210 Invalid: 9876543210 (missing +91-) or +91-98765 (too short)
Email Pattern (simplified)
<xs:simpleType name="EmailType">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}"/>
</xs:restriction>
</xs:simpleType>
7. totalDigits and fractionDigits — Decimal Precision
<xs:simpleType name="PriceType">
<xs:restriction base="xs:decimal">
<xs:totalDigits value="8"/>
<xs:fractionDigits value="2"/>
</xs:restriction>
</xs:simpleType>
This allows up to 8 total digits with exactly 2 decimal places. Valid: 12345.67 Invalid: 12345.678 (3 decimal places)
8. whiteSpace — Whitespace Handling
| Value | Behavior |
|---|---|
| preserve | Keep all whitespace as-is |
| replace | Replace tabs and line breaks with spaces |
| collapse | Collapse multiple spaces into one; remove leading/trailing |
<xs:simpleType name="CleanStringType">
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
Combining Multiple Facets
Multiple facets apply together — all conditions must be satisfied.
<xs:simpleType name="ProductCodeType">
<xs:restriction base="xs:string">
<xs:length value="7"/>
<xs:pattern value="[A-Z]{2}-[0-9]{4}"/>
</xs:restriction>
</xs:simpleType>
A product code must be exactly 7 characters and match the format: two uppercase letters, a hyphen, four digits. Valid: PC-0042 Invalid: pc-0042 (lowercase) or P-0042 (wrong length)
Using Custom Types in Element and Attribute Declarations
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="AgeType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="IndianPINType">
<xs:restriction base="xs:string">
<xs:pattern value="[1-9][0-9]{5}"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="AgeType"/>
<xs:element name="pinCode" type="IndianPINType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Facet Quick Reference
| Facet | Applies To | Purpose |
|---|---|---|
| enumeration | All types | Specific allowed values |
| minInclusive | Numbers, dates | Minimum value (included) |
| maxInclusive | Numbers, dates | Maximum value (included) |
| minExclusive | Numbers, dates | Minimum value (excluded) |
| maxExclusive | Numbers, dates | Maximum value (excluded) |
| length | Strings, binary | Exact character/byte count |
| minLength | Strings, binary | Minimum character/byte count |
| maxLength | Strings, binary | Maximum character/byte count |
| pattern | All types | Regular expression format |
| whiteSpace | Strings | Whitespace handling rule |
| totalDigits | Decimals | Maximum total digit count |
| fractionDigits | Decimals | Maximum decimal places |
Key Points to Remember
- xs:restriction narrows a base type using constraining facets.
- Enumeration limits values to a specific list.
- minInclusive and maxInclusive set a range where both boundary values are valid.
- minExclusive and maxExclusive set a range where boundary values are not valid.
- pattern uses regular expressions to enforce a specific text format.
- length, minLength, and maxLength control how long a string can be.
- Multiple facets combine — the value must satisfy all of them simultaneously.
