I don’t know how to title my question.
I have an XSD which contains the following elements
<xs:element name="abc">
<xs:complexType>
<xs:element maxOccurs="unbounded" ref="ele1"/>
</xs:complexType>
</xs:element>
<xs:element name="xyz">
<xs:complexType>
<xs:element maxOccurs="unbounded" ref="ele1"/>
</xs:complexType>
</xs:element>
<xs:element name="ele1">
<xs:complexType>
<xs:attribute name="ID" type="xs:integer"/>
</xs:complexType>
</xs:element>
The question is for element xyz ID is mandatory whereas for abc it is not; how can I specify this in the XSD?
Assuming ID is a unique identifier for the content repeated under the “container”, then you could set a xs:key constraint for the xyz like this:
Then, invalid XML because ID is missing:
Error message:
Invalid because it is duplicate (this is the downside, if the above assumption on uniqueness doesn’t stand true):
Error:
Working sample: