XML Schema restriction 元素
定义和用法
restriction 元素定义对 simpleType、simpleContent 或 complexContent 定义的约束。
元素信息
出现次数 | 一次 |
父元素 | complexContent |
内容 | group、all、choice、sequence、attribute、attributeGroup、anyAttribute |
语法
<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>
(? 符号声明在 restriction 元素中该元素可出现零次或一次。)
属性 | 説明 |
---|---|
id | オプション。この要素のユニークなIDを指定します。 |
base | 必須。このschema(または指定された命名空間で示される他のschema)で定義された内建データタイプ、simpleType、またはcomplexType要素の名前を指定します。 |
any attributes | オプション。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>