I'm trying to make two XML attributes to be mutually exclusive. How can one create an XSD schema to capture this kind of scenario?
I would like to have one of these
<elem value="1" />
<elem ref="something else" />
but not
<elem value="1" ref="something else" />
You can't do with attributes, but you can with child elements...
<element name="elem">
<complexType>
<choice>
<element name="value"/>
<element name="ref"/>
</choice>
</complexType>
</element>
This way you can have...
<elem>
<value>1</value>
</elem>
or...
<elem>
<rel>something else</rel>
</elem>
Unfortunately AFAIK you can't do that with XML Schema, I've had the same problem myself.
I've seen it suggested that if you need both of:
<elem type="xxx">
<elem ref="yyy">
then <elem>
itself should be split into two types, since they've clearly got different attributes...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With