Xsd Restrictions in XML
? XSD Restrictions in XML Schema
Restrictions in XSD allow you to limit or constrain the values that an element or attribute can have. This is useful to enforce data validity rules like length, range, pattern, enumeration, etc.
? Common Restriction Types
| Restriction | Description | Example |
|---|---|---|
xs:minLength | Minimum length of string | Min 3 characters |
xs:maxLength | Maximum length of string | Max 10 characters |
xs:pattern | Regex pattern the value must match | Only digits [0-9]+ |
xs:minInclusive | Minimum numeric value (inclusive) | ? 1 |
xs:maxInclusive | Maximum numeric value (inclusive) | ? 100 |
xs:enumeration | Allowed fixed set of values | "red", "green", "blue" |
xs:totalDigits | Max number of digits in a number | Max 5 digits |
xs:fractionDigits | Max number of digits after decimal point | Max 2 decimals |
? Example: Restricting a String and Number
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- Restricted string: length 3-10, pattern letters only --> <xs:simpleType name="NameType"> <xs:restriction base="xs:string"> <xs:minLength value="3"/> <xs:maxLength value="10"/> <xs:pattern value="[A-Za-z]+"/> </xs:restriction> </xs:simpleType> <!-- Restricted integer: between 1 and 100 --> <xs:simpleType name="AgeType"> <xs:restriction base="xs:integer"> <xs:minInclusive value="1"/> <xs:maxInclusive value="100"/> </xs:restriction> </xs:simpleType> <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="name" type="NameType"/> <xs:element name="age" type="AgeType"/> </xs:sequence> </xs:complexType> </xs:element></xs:schema>? Valid XML
<person> <name>John</name> <age>25</age></person>? Invalid XML Examples
Name too short:
<name>Jo</name>Name contains numbers:
<name>John123</name>Age out of range:
<age>150</age>
Would you like me to create a full XML + XSD sample with restrictions included?