XML Schema restrictionElement
Complete XML Schema Reference Manual
Definition and Usage
The restriction element defines constraints on a simpleType, simpleContent, or complexContent definition.
Element Information
- Parent elements: simpleType, simpleContent, complexContent
Syntax
id=ID
base=QName
any attributes
>
Content for simpleType:
(annotation?,(simpleType?,(minExclusive|minInclusive|
maxExclusive|maxInclusive|totalDigits|fractionDigits|
length|minLength|maxLength|enumeration|whiteSpace|pattern)*))
Content for simpleContent:
(annotation?,(simpleType?,(minExclusive |minInclusive|
maxExclusive|maxInclusive|totalDigits|fractionDigits|
length|minLength|maxLength|enumeration|whiteSpace|pattern)*)?,
((attribute|attributeGroup)*,anyAttribute?))
Content for complexContent:
(annotation?,(group|all|choice|sequence)?,
((attribute|attributeGroup)*,anyAttribute?))
</restriction>
(The ? symbol declares that the element can occur zero or one time inside the restriction element.)
| Attributes | Description |
|---|---|
| id | Optional. Specifies a unique ID for this element. |
| base | Required. Specifies the name of a built-in data type, simpleType, or complexType element defined in this schema (or in another schema indicated by the specified namespace). |
| any attributes | Optional. Specifies any other attributes with a non-schema namespace. |
Example 1
The following example defines an element named "age" with a restriction. The value of age cannot be less than 0 or greater than 100:
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Example 2
This example defines an element named "initials". The "initials" element is a simple type with a restriction. The acceptable values are three uppercase or lowercase letters from a to z:
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Example 3
This example defines an element named "password". The "password" element is a simple type with a restriction. The value must be a minimum of 5 characters and a maximum of 8 characters:
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Example 4
This example shows a complex type definition using a restriction. The complex type "Chinese_customer" is derived from a normal customer complex type, whose country element has a fixed value of "China":
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Norwegian_customer">
<xs:complexContent>
<xs:restriction base="customer">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="country" type="xs:string" fixed="Norway"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
Complete XML Schema Reference Manual Other Extensions