XML Schema restriction element

Definition and usage

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

Element information

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

Syntax

<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 indicates that the element can appear zero or once in the restriction element.)

Attributes Description
id Optional. Specifies the unique ID of the element.
base Required. Specifies the names of the built-in data types, 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 a non-schema namespace.

Beispiel

Beispiel 1

Der folgende Beispiel definiert ein Element namens "age" mit Einschränkungen. Der Wert von "age" darf nicht kleiner als 0 oder größer als 100 sein:

<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>

Beispiel 2

Dieses Beispiel definiert ein Element namens "initials". Das "initials"-Element ist ein einfacher Typ mit Einschränkungen. Akzeptierte Werte sind drei Groß- oder Kleinbuchstaben von a bis 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>

Beispiel 3

Dieses Beispiel definiert ein Element namens "password". Das "password"-Element ist ein einfacher Typ mit Einschränkungen. Der Wert muss mindestens 5 und höchstens 8 Zeichen lang sein:

<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>

Beispiel 4

Dieses Beispiel zeigt eine komplexe Typdefinition mit Einschränkungen. Der komplexe Typ "Chinese_customer" wird aus dem regulären "customer"-komplexen Typ abgeleitet, und der festgelegte Wert des "country"-Elements ist "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>