🚫 XSD Restrictions – Enforce Data Rules in XML Schema
🧲 Introduction – Why Learn XSD Restrictions?
In XML Schema (XSD), restrictions allow you to control the allowed values of elements and attributes. With restrictions, you can limit lengths, set patterns, define min/max values, and enumerate acceptable options. This ensures your XML documents meet strict business and formatting rules—essential for APIs, forms, and standardized data exchange.
🎯 In this guide, you’ll learn:
- What
<xs:restriction>
is and how it works - How to apply restrictions to strings, numbers, and dates
- Examples of
enumeration
,pattern
,minInclusive
,length
, etc. - Use cases and best practices for strict XML validation
📘 What Is <xs:restriction>
?
<xs:restriction>
is used inside <xs:simpleType>
to constrain values based on a base data type like xs:string
, xs:integer
, or xs:date
.
🔧 Syntax
<xs:simpleType>
<xs:restriction base="xs:dataType">
<!-- constraint facets here -->
</xs:restriction>
</xs:simpleType>
🔠 String Restrictions
🔹 Example 1 – Limit String Length
<xs:element name="username">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="4"/>
<xs:maxLength value="12"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
✅ Allows values between 4 and 12 characters long.
🔹 Example 2 – Enforce a Regex Pattern
<xs:element name="email">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value=".+@.+\..+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
✅ Ensures the string looks like an email address.
🔢 Numeric Restrictions
🔹 Example 3 – Range of Integers
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="18"/>
<xs:maxInclusive value="99"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
✅ Allows values from 18 to 99 inclusive.
📅 Date Restrictions
🔹 Example 4 – Minimum Date
<xs:element name="eventDate">
<xs:simpleType>
<xs:restriction base="xs:date">
<xs:minInclusive value="2025-01-01"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
✅ Requires dates on or after January 1, 2025.
🎯 Enumerations
🔹 Example 5 – Restrict to Fixed Values
<xs:element name="status">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="active"/>
<xs:enumeration value="inactive"/>
<xs:enumeration value="pending"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
✅ Only allows "active"
, "inactive"
, or "pending"
.
📚 All String Restriction Facets
Facet | Use Case |
---|---|
length | Exact string length |
minLength | Minimum string length |
maxLength | Maximum string length |
pattern | Regex-like format enforcement |
enumeration | Fixed list of valid values |
whiteSpace | Controls how whitespace is handled (preserve , replace , collapse ) |
📐 All Numeric Restriction Facets
Facet | Use Case |
---|---|
minInclusive | Minimum allowed value (inclusive) |
maxInclusive | Maximum allowed value (inclusive) |
minExclusive | Minimum value, excluding the value itself |
maxExclusive | Maximum value, excluding the value itself |
totalDigits | Total number of digits allowed |
fractionDigits | Digits allowed after decimal point |
✅ Best Practices for XSD Restrictions
- ✔️ Use
enumeration
for status flags, categories, or options - ✔️ Use
pattern
to validate formats like postal codes, emails, or IDs - ✔️ Combine
minLength
andmaxLength
to control field sizes - ✔️ For date or age validation, always use
minInclusive
ormaxInclusive
- ❌ Don’t use overly complex regex—keep
pattern
readable and test it - ❌ Avoid conflicting restrictions (e.g.
minLength
>maxLength
)
📌 Summary – Recap & Next Steps
XSD restrictions allow you to enforce business rules and data validation at the schema level. With just a few lines, you can catch invalid inputs early and ensure clean, structured data flows.
🔍 Key Takeaways:
- Use
<xs:restriction>
inside<xs:simpleType>
for custom constraints - Restrict string formats, numeric ranges, and allowable values
- Combine multiple facets for strong validation logic
⚙️ Real-world relevance: Used in XML forms, APIs, e-commerce feeds, digital documents, and any system where data integrity is critical.
❓ FAQs – XSD Restrictions
❓ Can I restrict element and attribute values the same way?
✅ Yes. Use <xs:simpleType>
with <xs:restriction>
for both.
❓ Can I use multiple restrictions together?
✅ Yes. For example, combine minLength
, maxLength
, and pattern
.
❓ What happens if the value doesn’t match the restriction?
✅ The XML document fails validation.
❓ Are restrictions only for strings?
✅ No. You can restrict numbers, dates, booleans, and even custom types.
❓ Can I use restrictions with global types?
✅ Yes. You can define a restricted type globally and reuse it across elements or attributes.
Share Now :