Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do mutually exclusive attributes in XML schema?

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" />
like image 602
Filip Frącz Avatar asked Dec 22 '08 16:12

Filip Frącz


2 Answers

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>
like image 95
Daniel Silveira Avatar answered Nov 08 '22 18:11

Daniel Silveira


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...

like image 25
Alnitak Avatar answered Nov 08 '22 19:11

Alnitak