Schema约束

Schema系列文章

1、XML对Schema文档的引用123

2、Schema文档概述123

3、Schema之简单元素、复合元素和属性

4、Schema约束

5、Schema指示器

6、如何创建一份XMLSchema And XML Elements

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:

 <xs:element  name ="price">  
  <xs:simpleType  >     
 <xs:restriction  base ="xs:double">      
  <xs:minExclusive  value ="20"></xs:minExclusive>        
<xs:maxExclusive  value ="200"></xs:maxExclusive>      
</xs:restriction>   
 </xs:simpleType>
  </xs:element>

1.2、为了限制XML元素的内容得到一组符合条件的值,我们会用到“列举约束:

<xs:element  name ="publishdate" >    
<xs:simpleType  >      
<xs:restriction  base ="xs:date">      
  <xs:enumeration  value ="2009-01-04"></xs:enumeration>      
  <xs:enumeration  value ="2008-01-15"></xs:enumeration>   
   </xs:restriction>    
</xs:simpleType>  
</xs:element>

1.3、式样约束:pattern;

    1.3.1、限制属性lang的值为a到z中的任意一个字母:

<xs:attribute  name ="lang" >    
       <xs:simpleType >     
                  <xs:restriction  base ="xs:string"> 
       <xs:pattern  value ="[a-z]"></xs:pattern>    
  </xs:restriction>    </xs:simpleType>  
</xs:attribute>

    1.3.2、限制属性lang的值为a到z中三个任意字母:

<xs:attribute  name ="lang" > 
   <xs:simpleType >  
    <xs:restriction  base ="xs:string">  
      <xs:pattern  value ="[a-z][a-z][a-z]"></xs:pattern>
      </xs:restriction>    </xs:simpleType> 
 </xs:attribute>

    1.3.3、限制属性lang的值为 a到z之间的三个大写或小写字母:

<xs:attribute  name ="lang" >
    <xs:simpleType > 
     <xs:restriction  base ="xs:string"> 
       <xs:pattern  value ="[a-zA-Z][a-zA-Z]"></xs:pattern> 
     </xs:restriction> 
   </xs:simpleType>
  </xs:attribute>

   1.3.4、限制属性lang的值为为xyz中的任意一个:

 <xs:attribute  name ="lang" > 
   <xs:simpleType > 
     <xs:restriction  base ="xs:string"> 
       <xs:pattern  value ="[xyz]"></xs:pattern>
      </xs:restriction>  
  </xs:simpleType>
  </xs:attribute>

其他:

  • "([a-z])*":a 到z的小写字母(可以有多个)或0;
  •   "([a-z][A-Z])+":每对都是一个小写字母后跟一个大写字母组成;
  • "male|female":male (男性)或female(女性);
  • "[a-zA-Z0-9]{8}":一行里必须有8个字符,字符必须是a到z大或小写字母,或者是0到9的数字;

2、对空白符的约束:

     <xs:attribute  name ="lang" > 
   <xs:simpleType > 
     <xs:restriction  base ="xs:string">   
     <xs:whiteSpace  value ="preserve"></xs:whiteSpace>
      </xs:restriction>    </xs:simpleType>
  </xs:attribute>

preserve:意味着XML处理器不会删除任何空白字符;

replace:意味着XML处理器会用空格替代所有的空白字符(换行符, 制表符, 空格符, 回车符);

collapse:意味着XML处理器会清除所有的空白字符(换行符, 制表符, 空格符以及回车符都被空格符代替。头尾空格会被清除,多个空格也会减少为一个);

更多参考http://www.cnblogs.com/caoxch/archive/2006/11/17/563856.html

原文地址:https://www.cnblogs.com/LittleFeiHu/p/1894512.html