限制元素用于定义XML元素可以接受的接受值。
语法
<xs:restriction base = "element-type"> restrictions </xs:restriction>
其中,
base
- 要应用限制的元素的类型。 例如,<xs:restriction base = "xs:integer">
。指定此限制特定于int
类型的元素。restriction
- 限制通常是对元素值应用的一系列条件。 在这个例子中,设置了一个标记限制,<mark>
应该在0
到100
的范围内,同时包含两个值。<xs:minInclusive value = "0"/> <xs:maxInclusive value = "100"/>
示例
对值的限制。
条件 - marks
应在0
到100
的范围内。
<xs:element name = "marks">
<xs:simpleType>
<xs:restriction base = "xs:integer">
<xs:minInclusive value = "0"/>
<xs:maxInclusive value = "100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
对值集的限制。
条件 - marks
应该只是A
,B
或C
。
<xs:element name = "grades">
<xs:simpleType>
<xs:restriction base = "xs:string">
<xs:enumeration value = "A"/>
<xs:enumeration value = "B"/>
<xs:enumeration value = "C"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
使用常规模式限制。
条件 - firstname
只能是字母。
<xs:element name = "firstname">
<xs:simpleType>
<xs:restriction base = "xs:string">
<xs:pattern value = "[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
限制类型
序号 | 限制 | 描述 |
---|---|---|
1 | enumeration |
定义可接受的值列表。 |
2 | fractionDigits |
定义允许的最大小数位数(零或多个)。 |
3 | length |
根据字符串中的字符或列表中的项目(零个或多个)定义长度。 |
4 | maxExclusive |
定义除此数字之外的数值的上限。 |
5 | maxInclusive |
定义包含此数字的数值的上限。 |
6 | maxLength |
根据列表中的字符或项目的字符(零或更多)定义最大长度。 |
7 | minExclusive |
定义除此数字之外的数值的下限。 |
8 | minInclusive |
定义数值的下限,包括此数字。 |
9 | minLength |
根据字符串或列表中的项目(零或更多)的字符定义最小长度。 |
10 | pattern |
定义可接受的模式标识的确切字符序列 |
11 | totalDigits |
定义数字中允许的确切位数(始终大于零) |
12 | whiteSpace |
定义处理空格字符(换行符,制表符,空格和回车符)的方式 |