XML Schema restriction要素

定義と用法

restriction要素はsimpleType、simpleContent、またはcomplexContentで定義された制約を定義します。

要素情報

出現回数 一度
親要素 complexContent
内容 group、all、choice、sequence、attribute、attributeGroup、anyAttribute

文法

<restriction
id=ID
base=QName
任意の属性
>
simpleTypeの内容:
(annotation?,(simpleType?,(minExclusive|minInclusive| 
maxExclusive|maxInclusive|totalDigits|fractionDigits|
length|minLength|maxLength|enumeration|whiteSpace|pattern)*))
simpleContentの内容:
(annotation?,(simpleType?,(minExclusive |minInclusive| 
maxExclusive|maxInclusive|totalDigits|fractionDigits|
(length|minLength|maxLength|enumeration|whiteSpace|pattern)*)?, 
((attribute|attributeGroup)*,anyAttribute?))
complexContentの内容:
(annotation?,(group|all|choice|sequence)?,
((attribute|attributeGroup)*,anyAttribute?))
</restriction>

(? 符号は restriction 要素内でこの要素が0回または1回出现することが許可されています。)

属性 説明
id 選択可。この要素のユニークなIDを定義します。
base 必須。このスキーマ(または指定された命名空間で示される他のスキーマ)で定義された内建データタイプ、simpleType、またはcomplexType要素の名前を定義します。
任意の属性 選択可。non-schema 命名空間を持つ任意の他の属性を定義します。

例1

以下の例では、制約付きで「age」という名前の要素を定義しています。ageの値は0未満か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>

例2

この例では、「initials」という要素を定義しています。「initials」要素は制約付きのシンプルな型です。受け入れ可能な値は、aからzの大文字または小文字の3文字です:

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

例3

この例では、「password」という要素を定義しています。「password」要素は制約付きのシンプルな型です。値は少なくとも5文字、多くとも8文字である必要があります:

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

例4

この例では、制約を使用した複雑な型定義を示しています。複雑な型「Chinese_customer」は一般的な「customer」複雑な型から派生しており、country要素の固定値は「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>