Python XSD Restrictions, Facets & Pattern Matching (Regex)
XSD restrictions (facets) define acceptable boundaries for element and attribute values. You can restrict numeric ranges (minInclusive, maxInclusive), define allowed string enumerations (enumeration), constrain lengths, and enforce regular expression patterns (pattern).
"Restrictions are the velvet rope bouncers of your data schema: they verify that an age is between 18 and 120, a password matches a regex pattern, or a currency code is strictly USD, EUR, or GBP."
Deep Dive: How It Works
<xs:restriction base="xs:string">: Base type being constrained.
<xs:enumeration value="..."/>: Constrains value to an explicit set of allowable choices.
<xs:pattern value="[0-9]{3}-[A-Z]{2}"/>: Validates against a regular expression pattern.
Numeric Facets: <xs:minInclusive value="0"/> and <xs:maxInclusive value="100"/>.
Syntax Blueprint
<xs:simpleType name="AgeType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="18" />
</xs:restriction>
</xs:simpleType>Creates a custom simple type requiring integer values >= 18.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Applying string length facets to integer types.Why it happens: <xs:length> applies to strings; numeric types must use <xs:totalDigits> or <xs:maxInclusive>.
How to fix: Use the correct facet for the base data type.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Define an integer score restriction
Create a <xs:restriction base="xs:integer"> with <xs:minInclusive value="0"/> and <xs:maxInclusive value="100"/>.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.