2016-11-29 62 views
1

如何确保在location元素包含在XML中时至少指定了location的子元素(locality,wkt)中的一个?如何确保至少有一个子元素通过XSD存在?

<xs:element name="location" nillable="true" minOccurs="0"> 
    <xs:complexType> 
    <xs:group ref="cs:locationGroup"></xs:group> 
    </xs:complexType> 
</xs:element> 

locationGroup定义:

<xs:group name="locationGroup"> 
    <xs:all> 
    <xs:element name="locality" minOccurs="0"/> 
    <xs:element name="wkt" minOccurs="0"/> 
    </xs:all> 
</xs:group> 

我的XSD的版本是1.0。

回答

1

对于这样一个小数量的可能的子元素,简单地定义所允许的组合的xs:choice

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <xs:element name="location"> 
    <xs:complexType> 
     <xs:group ref="locationGroup"></xs:group> 
    </xs:complexType> 
    </xs:element> 

    <xs:group name="locationGroup"> 
    <xs:choice> 
     <xs:sequence> 
     <xs:element name="locality"/> 
     <xs:element name="wkt" minOccurs="0"/> 
     </xs:sequence> 
     <xs:sequence> 
     <xs:element name="wkt"/> 
     <xs:element name="locality" minOccurs="0"/> 
     </xs:sequence> 
    </xs:choice> 
    </xs:group> 
</xs:schema> 

注意,这种方法

  • 要求之一或两者localitywkt是目前
  • 允许任何订单时,两者都存在
  • 在两个XSD 1 .0(和1.1)

按要求。

相关问题