I have the following XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Sea">
<xs:complexType>
<xs:sequence>
<xs:element ref="FishSubGroup"
minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="FishSubGroup" abstract="true"/>
<xs:element name="Tuna" type="FishType" substitutionGroup="FishSubGroup"/>
<xs:element name="Carp" type="FishType" substitutionGroup="FishSubGroup"/>
<xs:element name="Salmon" type="FishType" substitutionGroup="FishSubGroup"/>
<xs:complexType name="FishType">
<xs:attribute name="name" type="xs:string"/>
</xs:complexType>
</xs:schema>
This XML represents a valid instance:
<?xml version="1.0" encoding="UTF-8"?>
<Sea name="Atlantic Ocean">
<Tuna name="tuna1"/>
<Carp name="carp1"/>
<Carp name="carp2"/>
<Tuna name="tuna2"/>
<Salmon name="salmon1"/>
</Sea>
Note: I have taken the nice aquatic example from this post.
Question:
Is there a way in XSD to define a constraint to have only a single substituted element of its category? In other words, I would like to have a maximum of 1 Tuna, 1 Carp, 1 Salmon etc. as shown next:
<?xml version="1.0" encoding="UTF-8"?>
<Sea name="Atlantic Ocean">
<Tuna name="tuna1"/>
<Carp name="carp1"/>
<Carp name="carp2"/> <!-- <-- Invalid Carp Already Defined -->
<Tuna name="tuna2"/> <!-- <-- Invalid Tuna Already Defined -->
<Salmon name="salmon1"/>
</Sea>