4

I need a required attribute or element only if a specific value of an enumeration is chosen. Example below:

  <xs:element name="TYPE" type="TestEnum" />
   <!-- // This Element should only required when TYPE = INTERNATIONAL -->
   <xs:element name="IBAN"/>

 </xs:complexType>
<xs:simpleType name="TestEnum">
    <xs:restriction base="xs:string">
        <xs:enumeration  value="NATIONAL"/>
        <xs:enumeration value="INTERNATIONAL"/>
    </xs:restriction>
</xs:simpleType>

kjhughes
  • 106,133
  • 27
  • 181
  • 240
elnapo
  • 55
  • 2
  • 7
  • Aside from some structural issues with your XSD fragment, the type of the constraint you request typically requires XSD 1.1's conditional type assignment or assertion constructs. Is XSD 1.1 an option for you? – kjhughes May 06 '16 at 15:25
  • Thanks for the answer. Yes it is an Option for me. How does this constraint look like? – elnapo May 06 '16 at 16:48

1 Answers1

5

XSD 1.1

Here's how use xs:assert to make IBAN be mandatory when TYPE = 'INTERNATIONAL':

<?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"
           vc:minVersion="1.1">

  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="TYPE" type="TestEnum" />
        <!-- // This Element should only required when TYPE = INTERNATIONAL -->
        <xs:element name="IBAN" minOccurs="0"/>        
      </xs:sequence>
      <xs:assert test="not(TYPE = 'INTERNATIONAL') or IBAN"/>
    </xs:complexType>
  </xs:element>

  <xs:simpleType name="TestEnum">
    <xs:restriction base="xs:string">
      <xs:enumeration  value="NATIONAL"/>
      <xs:enumeration value="INTERNATIONAL"/>
    </xs:restriction>
  </xs:simpleType>

</xs:schema>
kjhughes
  • 106,133
  • 27
  • 181
  • 240