I have two possible variants of an XML:
- Both 'ElementB' and 'ElementC' are mandatory.
<ElementA>
<ElementB />
<ElementC />
</ElementA>
- both 'ElementB' and 'ElementD' are mandatory, but 'ElementC' is optional.
<ElementA>
<ElementB />
<ElementC />
<ElementD />
</ElementA>
I need something like
<xs:group name="ElementsD">
<xs:sequence>
<xs:element name="ElementC " type="ElementCType" minOccurs="0" maxOccurs="1" />
<xs:element name="ElementD " type="ElementDType" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:group>
<xs:element name="ElementA">
<xs:complexType>
<xs:sequence>
<xs:element name="ElementB " type="ElementBType" minOccurs="1" maxOccurs="1" />
<xs:choice>
<xs:element name="ElementC" type="ElementCType" minOccurs="1" maxOccurs="1" />
<xs:group ref="ElementsD" />
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
This schema does not work due to
ElementC and ElementC (or elements from their substitution group) violate "Unique Particle Attribution". During validation against this schema, ambiguity would be created for those two particles.
Is there any good solution?
You can use
<xs:sequence>inside<xs:choice>. This allows you to define multiple sequences that may occur within an element (but only one of them is allowed at once in any occurrence of said element).