1、关于约束:restriction
概述、
enumeration | 定义了一系列的有效值 |
fractionDigits | 指定了允许的小数位数的最多位数。必须大于等于0 |
length | 指定了允许的字符或列表项的个数。必须大于等于0 |
maxExclusive | 指定了数值的上限(数值要比这个值小) |
maxInclusive | 指定了数值上限(数值必须小于等于这个值) |
maxLength | 指定了所允许的字符或列表项的最多个数。必须大于等于0 |
minExclusive | 指定了数值的下限 (数值要比这个值大) |
minInclusive | 指定了数值的下限(数值必须大于等于这个值) |
minLength | 指定了所允许的字符或列表的最少个数。必须等于大于0个 |
pattern | 定义了符合要求的字符的确切排列顺序 |
totalDigits | 指定了所允许的字符的确切个数。必须大于0 |
whiteSpace | 指定了空白该怎样被处理(换行符,制表符,空格符和回车符) |
1.1、对单个值得约束:minInclusive、minExclusive、maxInclusive、maxExclusive:
1 | < xs:element name = "price" > |
3 | < xs:restriction base = "xs:double" > |
4 | < xs:minExclusive value = "20" ></ xs:minExclusive > |
5 | < xs:maxExclusive value = "200" ></ xs:maxExclusive > |
1.2、为了限制XML元素的内容得到一组符合条件的值,我们会用到“列举约束:
1 | < xs:element name = "publishdate" > |
3 | < xs:restriction base = "xs:date" > |
4 | < xs:enumeration value = "2009-01-04" ></ xs:enumeration > |
5 | < xs:enumeration value = "2008-01-15" ></ xs:enumeration > |
1.3、式样约束:pattern;
1.3.1、限制属性lang的值为a到z中的任意一个字母:
1 | < xs:attribute name = "lang" > |
3 | < xs:restriction base = "xs:string" > |
4 | < xs:pattern value = "[a-z]" ></ xs:pattern > |
5 | </ xs:restriction > </ xs:simpleType > |
1.3.2、限制属性lang的值为a到z中三个任意字母:
1 | < xs:attribute name = "lang" > |
3 | < xs:restriction base = "xs:string" > |
4 | < xs:pattern value = "[a-z][a-z][a-z]" ></ xs:pattern > |
5 | </ xs:restriction > </ xs:simpleType > |
1.3.3、限制属性lang的值为 a到z之间的三个大写或小写字母:
1 | < xs:attribute name = "lang" > |
3 | < xs:restriction base = "xs:string" > |
4 | < xs:pattern value = "[a-zA-Z][a-zA-Z]" ></ xs:pattern > |
1.3.4、限制属性lang的值为为xyz中的任意一个:
1 | < xs:attribute name = "lang" > |
3 | < xs:restriction base = "xs:string" > |
4 | < xs:pattern value = "[xyz]" ></ xs:pattern > |
其他:
- "([a-z])*":a 到z的小写字母(可以有多个)或0;
- "([a-z][A-Z])+":每对都是一个小写字母后跟一个大写字母组成;
- "male|female":male (男性)或female(女性);
- "[a-zA-Z0-9]{8}":一行里必须有8个字符,字符必须是a到z大或小写字母,或者是0到9的数字;
2、对空白符的约束:
1 | < xs:attribute name = "lang" > |
3 | < xs:restriction base = "xs:string" > |
4 | < xs:whiteSpace value = "preserve" ></ xs:whiteSpace > |
5 | </ xs:restriction > </ xs:simpleType > |
preserve:意味着XML处理器不会删除任何空白字符;
replace:意味着XML处理器会用空格替代所有的空白字符(换行符, 制表符, 空格符, 回车符);
collapse:意味着XML处理器会清除所有的空白字符(换行符, 制表符, 空格符以及回车符都被空格符代替。头尾空格会被清除,多个空格也会减少为一个);