如何用XSD来限制XML某元素不能为空

2016-05-03 22:15:00
1147533288
转贴
1405

  
如果你要让包含子元素的元素不能为空,那就要在定义它的子元素时让该子元素的“minOccurs”属性不为 0。如要让一个仅具有文本内容的元素不能为空,可以声明其属于某不为空的简单类型,或者使用约束条件,限制其长度大于 0。 
如下述片段,声明了“data”元素必须具有一个类型为 dateTime 的“time”元素(元素必须具有子元素),由于“time”元素的类型为 dateTime,因此,它的内容不能为空(元素内容不能为空): 
  
<xs:element name="data"> 
     <xs:complexType> 
         <xs:sequence> 
             <xs:element name="time" type="xs:dateTime" minOccurs="1"/> 
             <xs:element name="string"> 
                 <xs:simpleType> 
                     <xs:restriction base="xs:string"> 
                         <xs:minLength value="1"/> 
                     </xs:restriction> 
                 </xs:simpleType> 
             </xs:element> 
         </xs:sequence> 
     </xs:complexType> 
</xs:element> 
  
(sequence 中出现的 element,默认只能出现一次,因此“time”和“string”必须在“data”中出现) 
(“string”元素是字符串类型,通过 restriction 限制其最短长度为 1,因此其值不能为零长度的字符串) 
文章分类
联系我
联系人: meepo
电话: *****
Email: 1147533288@qq.com
QQ: 1147533288