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