With XSD 1.0, you can use xs:key
element.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="tag">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="abc" type="xs:integer"/>
<xs:attribute name="def" type="xs:integer"/>
</xs:complexType>
<xs:key name="attributeKey">
<xs:selector xpath="."/>
<xs:field xpath="@abc|@def"/>
</xs:key>
</xs:element>
Edit:
If both attributes are present (even with different values), this creates two keys, so the XML validation will fail. On the other hand, the <xs: key>
requires that a key is defined for the element, and therefore one of the two attributes must be present.
the following XML doc is not valid using the above XSD. (I'am using oXygen 17.0):
<tag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="stack3.xsd" name="" abc="12" def="13"/>
Error:
cvc-identity-constraint.3: Field "./@abc|./@def" of identity constraint "attributeKey" matches more than one value within the scope of its selector; fields must match unique values
<tag name="default3"/>
should be valid (ie when neither attribute is present). – Whipstitch