XML Schema restriction 元素
❮ 完整的 XML Schema 參考
定義和用法
restriction 元素定義了 simpleType、simpleContent 或 complexContent 定義的限制。
元素資訊
- 父元素: simpleType, simpleContent, complexContent
語法
<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 元素內出現零次或一次)
Attribute | 描述 |
---|---|
id | 可選。指定元素的唯一 ID |
base |
必需。指定一個內建資料型別、simpleType 元素或 complexType 元素(在此模式或其他模式中定義)的名稱 |
任何屬性 | 可選。指定任何其他非 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”元素是一個帶有限制的簡單型別。唯一可接受的值是 3 個小寫或大寫字母(a 到 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>
示例 3
此示例定義了一個名為“password”的元素。“password”元素是一個帶有限制的簡單型別。值必須至少為五個字元,最多為八個字元
<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
此示例顯示了一個使用 restriction 定義的 complex type。complex type“Norwegian_customer”是從通用的 customer complex type 派生的,其 country 元素被固定為“Norway”
<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="Norwegian_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="Norway"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
❮ 完整的 XML Schema 參考