XSD限定/Facets
什么是限定:
限定(restriction)用于为 XML 元素或者属性定义可接受的值。对 XML 元素的限定被称为 facet
本章内容
对值的限定
对一组值的限定
对一系列值的限定
对一系列值的其他限定
对空白字符的限定
对长度的限定
数据类型的限定
对值的限定
实例:
下面的例子定义了带有一个限定且名为 "age" 的元素。age 的值不能低于 0 或者高于 120
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<!--restriction关键字 n.限制、约束-->
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
对一组值的限定
把 XML 元素的内容限制为一组可接受的值,我们要使用枚举约束(enumeration constraint)
实例:
下面的例子定义了带有一个限定的名为 "car" 的元素。可接受的值只有:Audi, Golf, BMW
<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
or
<xs:element name="car" type="carType"/>
<xs:simpleType name="carType">
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
<!--
在这种情况下,类型 "carType" 可被其他元素使用,因为它不是 "car" 元素的组成部分
-->
对一系列值的限定
把 XML 元素的内容限制定义为一系列可使用的数字或字母,我们要使用模式约束(pattern constraint)---有点类似正则表达式
实例:
定义一个限定的名为 "letter" 的元素。可接受的值是 a - z 中零个或多个字母
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
<!--partern关键字的意思是n.模式
意思是partern模式后的值按照" "中的内容来写-->
</xs:restriction>
</xs:simpleType>
</xs:element>
可接受的值是大写字母 A - Z 其中的三个
<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z][A-Z]"/>
<!--不需要占位符,只需要再value后面表示即可。
位置数表示可接受几个数,[A-Z]表示A-Z字母的任意字母,必须是大写-->
</xs:restriction>
</xs:simpleType>
</xs:element>
接受的值是大写或小写字母 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>
接受的值是字母 x, y 或 z 中的一个
<xs:element name="choice">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[xyz]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
接受的值是五个阿拉伯数字的一个序列,且每个数字的范围是 0-9
<xs:element name="prodid">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
对一系列值得其他限定
实例:
定义了带有一个限定的名为 "letter" 的元素。可接受的值是 a - z 中零个或多个字母
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
<!--注意比较和上诉对一系列值的限定
运算符+占位符的理解-->
</xs:restriction>
</xs:simpleType>
</xs:element>
定义了可接受的值是一对或多对字母,每对字母由一个小写字母后跟一个大写字母组成
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z][A-Z])+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
定义了可接受的值是 male 或者 female
<xs:element name="gender">
<!--元素是gender-->
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
定义了:
-
可接受的值是由 8 个字符组成的一行字符
-
这些字符必须是大写或小写字母 a - z 亦或数字 0 - 9
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9]{8}"/>
<!--注意这个"{}"中的数字8-->
</xs:restriction>
</xs:simpleType>
</xs:element>
对空白字符的限定
规定对空白字符(whitespace characters)的处理方式,我们需要使用 whiteSpace 限定
实例:
下面的例子定义了带有一个限定的名为 "address" 的元素。这个 whiteSpace 限定被设置为 "preserve"XML 处理器不会移除任何空白字符
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
<!--关键字:preserve vt.保存-->
</xs:restriction>
</xs:simpleType>
</xs:element>
whiteSpace 限定被设置为 "replace"XML 处理器将移除所有空白字符(换行、回车、空格以及制表符)
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
<!--关键字:replace vt.取代-->
</xs:restriction>
</xs:simpleType>
</xs:element>
whiteSpace 限定被设置为 "collapse"XML 处理器将移除所有空白字符(换行、回车、空格以及制表符会被替换为空格,开头和结尾的空格会被移除,而多个连续的空格会被缩减为一个单一的空格)
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
对长度的限定
限制元素中值的长度,我们需要使用 length、maxLength 以及 minLength 限定
实例:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<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>
数据类型的限定
| 限定 | 描述 |
|---|---|
| enumeration | 定义可接受值的一个列表 |
| fractionDigits | 定义所允许的最大的小数位数。必须大于等于0。 |
| length | 定义所允许的字符或者列表项目的精确数目。必须大于或等于0。 |
| maxExclusive | 定义数值的上限。所允许的值必须小于此值。 |
| maxInclusive | 定义数值的上限。所允许的值必须小于或等于此值。 |
| maxLength | 定义所允许的字符或者列表项目的最大数目。必须大于或等于0。 |
| minExclusive | 定义数值的下限。所允许的值必需大于此值。 |
| minInclusive | 定义数值的下限。所允许的值必需大于或等于此值。 |
| minLength | 定义所允许的字符或者列表项目的最小数目。必须大于或等于0。 |
| pattern | 定义可接受的字符的精确序列。 |
| totalDigits | 定义所允许的阿拉伯数字的精确位数。必须大于0。 |
| whiteSpace |

浙公网安备 33010602011771号