2016-11-29 60 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)

按要求。

相關問題