Suppose I have an XML element, food
, that can take on one of two forms:
You can do this using XSD 1.1's Conditional Type Assignment:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
elementFormDefault="qualified"
vc:minVersion="1.1">
<xs:element name="food">
<xs:alternative test="@kind = 'juice'" type="JuiceType"/>
<xs:alternative test="@kind = 'fruit'" type="FruitType"/>
</xs:element>
<xs:complexType name="JuiceType">
<xs:sequence>
<!-- ... -->
</xs:sequence>
</xs:complexType>
<xs:complexType name="FruitType">
<xs:sequence>
<!-- ... -->
</xs:sequence>
<xs:attribute name="sort"/>
</xs:complexType>
</xs:schema>
Do consider, however, an alternative design that's expressible in both XSD 1.1 and 1.0 that's generally preferable:
<juice/>
<fruit sort="apple"/>
That is, rather than have a kind
attribute, use the element name to convey kind.