XSD - one of 2 attributes is required?

Xsd

Xsd Problem Overview


Is there a way to specify that one of 2 attributes is required in XSD?

for example, I have a definition like this:

<xs:attribute name="Name" type="xs:string" use="optional" />
<xs:attribute name="Id" type="xs:string" use="optional" />

I want to be able to define that at least one of these is required. Is that possible?

Xsd Solutions


Solution 1 - Xsd

No, I don't think you can do that with attributes. You could wrap two <xs:element> into a <xs:choice> - but for attributes, there's no equivalent construct, I'm afraid.

Solution 2 - Xsd

XSD 1.1 will let you do this using asserts.

<xsd:element name="remove">
    <xsd:complexType>                        
        <xsd:attribute name="ref" use="optional"/>
        <xsd:attribute name="uri" use="optional"/>
        <xsd:assert test="(@ref and not(@uri)) or (not(@ref) and @uri)"/>            
    </xsd:complexType>
</xsd:element>

Solution 3 - Xsd

Marc is quite right... You cannot have xs:attribute child elements inside a xs:choice parent element in XSD.

The logic seems to be that if two instances of an element have a mutually exclusive set of attributes then they are logically two different elements.

A workaround for this has been presented by Jeni Tennison here.

Solution 4 - Xsd

You should look at this pages on W3C wiki: Simple attribute implication and Attribute muttex

Solution 5 - Xsd

The example defines an element named "person" which must contain either a "employee" element or a "member" element.

<xs:element name="person">
  <xs:complexType>
    <xs:choice>
      <xs:element name="employee" type="employee"/>
      <xs:element name="member" type="member"/>
    </xs:choice>
  </xs:complexType>
</xs:element>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser92454View Question on Stackoverflow
Solution 1 - Xsdmarc_sView Answer on Stackoverflow
Solution 2 - Xsdjeremiah jahnView Answer on Stackoverflow
Solution 3 - XsdCerebrusView Answer on Stackoverflow
Solution 4 - XsdOndřej DoněkView Answer on Stackoverflow
Solution 5 - XsdNIthinView Answer on Stackoverflow