Search code examples
xsd

XML Schema with a combination of choice and all


I want to do the validations like:

<A>
   <B> or <C>
   <D>
</A>

In the , the first element should be one of the B and C. The second element is D. And at the same time, the first element B or C and the second element do not in sequence. It could become and . Anybody know how to do it?


Solution

  • I get the best practice. It should be as below:

    <xs:element name="A">
       <xs:complexType>
          <xs:all>
            <xs:element ref="Choice" minOccurs="1" maxOccurs="1"/>
            <xs:element ref="D" minOccurs="0" maxOccurs="1"/>
          </xs:all>
       </xs:complexType>
     </xs:element>
    
     <xs:element name="D" type="xs:string"/>
    
     <xs:element name="Choice" abstract="true"/> 
    
     <xs:element name="B" substitutionGroup="Choice"> 
     </xs:element>
    
     <xs:element name="C" substitutionGroup="Choice"> 
     </xs:element>