XML Schema restriction element

Definition and usage

The restriction element defines constraints for simpleType, simpleContent, or complexContent.

Element information

Occurrence times Once
Parent element complexContent
Content group, all, choice, sequence, attribute, attributeGroup, anyAttribute

Grammar

<restriction
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 declaration is in the restriction element, which can appear zero or one times.)

Attribute Description
id Optional. Specifies a unique ID for the element.
base Required. Specifies the name of the built-in data type, simpleType, or complexType elements defined in the schema (or any other schema indicated by the specified namespace).
any attributes Optional. Specifies any other attributes with non-schema namespaces.

Instance

Example 1

The following example defines an element named "age" with constraints. The value of age cannot be less than 0 or greater than 100:

<xs:element name="age">
  <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 constrained simple type. Acceptable values are three uppercase or lowercase letters from a to z:

<xs:element name="initials">
  <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 constrained simple type. The value must be at least 5 characters and at most 8 characters long:

<xs:element name="password">
  <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 demonstrates a complex type definition using constraints. The complex type "Chinese_customer" is derived from a common customer complex type, with the fixed value of the country element being "China":

<xs:complexType name="customer">
  <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="Chinese_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="China"/>
      </xs:sequence>
    </xs:restriction>
  </xs:complexContent>
</xs:complexType>