Zaphod
Zaphod

Reputation: 7290

XML Schema for attribute or subnode

Here is my problem. In a node, I wish to have either an attribute or a subnode (from a group). For instance:

<set_variable name="bob" value="3/>

or

<set_variable name="bob">
    <.../>
</set_variable>

The only way I found to do it is:

<xs:element name="set_variable" type="setVariableType"/>

<xs:complexType name="setVariableType">
    <xs:sequence minOccurs="0" maxOccurs="1">
        <xs:group ref="arithmeticGroup"/>
    </xs:sequence>
    <xs:attribute name="name" type="xs:string" use="required"/>
    <xs:attribute name="value" type="xs:integer" use="optional"/>
</xs:complexType>

<xs:group name="arithmeticGroup">
    ....
</xs:group>

The problem with that solution is that it allows two forbidden ways:

<set_variable name="bob"/> <!-- Should at least have an attribute or a subnode -->

and

<set_variable name="bob" value="18"> <!-- Should not have both attribute and a subnode -->
    <.../>
</set_variable>

Anyone has an idea?

Upvotes: 1

Views: 246

Answers (1)

Petru Gardea
Petru Gardea

Reputation: 21658

Unfortunately, this is not possible with XSD 1.0; you either have to use Schematron assertions on top of XSD 1.0, or rely on XSD 1.1 assertions or type alternatives (just forged as a W3C rec on April 5th) and a compliant processor (none free, very few available).

Upvotes: 2

Related Questions