XSD - 限制
restriction 元素用于定义 XML 元素可以接受的值。
语法
<xs:restriction base = "element-type"> restrictions </xs:restriction>
| base | 要应用限制的元素的类型。例如, <xs:restriction base = "xs:integer"> 指定此限制特定于类型为 int 的元素。 |
| restriction | restriction 通常是一系列要应用于元素值的条件。在本例中,我们对分数设置了一个限制,即分数应在 0 到 100 的范围内,这两个值都包含在内。 <xs:minInclusive value = "0"/> <xs:maxInclusive value = "100"/> |
示例
值限制。
条件 - 分数应在 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>
值集限制。
条件 - 等级只能是 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>
使用正则表达式的限制。
条件 - 名字只能包含字母。
<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 定义如何处理空白字符(换行符、制表符、空格和回车符)。 |
xsd_simple_types.htm
广告