XML Schema restriction element

Definition and usage

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

Element information

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

Grammar

<restriction
id=ID
base=QName
Dowolne atrybuty
>
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>

(Symbol declaration is in the restriction element; this element may occur zero or once.)

Atrybut Opis
Id Opcjonalnie. Określa unikalny ID elementu.
Baza Wymagane. Określa nazwę wbudowanego typu danych, simpleType lub złożonego typu elementu zdefiniowanego w tym schemacie (lub innym schemacie wskazanym przez przestrzeń nazw).
Dowolne atrybuty Opcjonalnie. Określa dowolne inne atrybuty z non-schematyczną przestrzenią nazw.

Przykład

Przykład 1

Poniższy przykład definiuje element o nazwie "age" z ograniczeniami. Wartość "age" nie może być mniejsza niż 0 ani większa niż 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>

Przykład 2

Ten przykład definiuje element o nazwie "initials". Element "initials" jest typem prostym z ograniczeniami. Akceptowane wartości to trzy duże lub małe litery z zakresu a do 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>

Przykład 3

Ten przykład definiuje element o nazwie "password". Element "password" jest typem prostym z ograniczeniami. Wartość musi składać się z co najmniej 5 znaków i co najwyżej 8 znaków:

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

Przykład 4

Ten przykład pokazuje złożony typ definicji używający ograniczeń. Złożony typ "Chinese_customer" odziedzicza się od typy złożonej "customer", gdzie stała wartość elementu "country" to "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>