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 元素中該元素可出現零次或一次。)
屬性 | 描述 |
---|---|
id | 可选。規定該元素的唯一的 ID。 |
基礎 | 必需。規定在此 schema(或由指定的命名空間指示的其他 schema)中定義的內建數據類型、simpleType 或 complexType 元素的名稱。 |
任何屬性 | 可选。規定帶有 non-schema 命名空间的任何其他屬性。 |
Halimbawa
Mga halimbawa 1
Ang halimbawa na ito ay naglilinang ng isang elemento na may pangalan na "age" na may pagbabawal. Ang halaga ng "age" ay hindi dapat na mas mababa sa 0 o mas mataas sa 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>
Mga halimbawa 2
Ang paglilinang nito ay naglalagay ng isang elemento na may pangalan na "initials". Ang elemento na "initials" ay isang simple type na may pagbabawal. Ang mga halalang halaga ay tatlong may a hanggang z na malalaking o maliit na titik:
<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>
Mga halimbawa 3
Ang paglilinang nito ay naglalagay ng isang elemento na may pangalan na "password". Ang elemento na "password" ay isang simple type na may pagbabawal. Ang halaga ay dapat na may pinakamaliit na 5 na mga karakter at pinakamataas na 8 na mga karakter:
<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>
Mga halimbawa 4
Ang pagpapakita nito ay naglalarawan ng isang kompleksong paglilinang ng uri na may pagbabawal. Ang kompleksong uri na "Chinese_customer" ay nagmula sa isang pangkaraniwang kompleksong uri na "customer", ang alinmang halaga ng elemento na "country" ay "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>