Search code examples
xmlxsd

XML Schema: element A, element B or both


I'm trying to write an XML schema construct that would allow to have either element A or element B, or both A and B.

I've tried this:

<xs:complexType name="RoleMapType">
    <xs:sequence>
        <xs:element minOccurs="0" name="Description" type="xs:string"/>
        <xs:element minOccurs="0" name="Schedule" type="xs:string"/>
        <xs:choice>
            <xs:element name="Loader" type="RoleMapLoaderType"/>
            <xs:element name="Dump" type="RoleMapDumpType"/>
            <xs:sequence>
                <xs:element name="Loader" type="RoleMapLoaderType"/>
                <xs:element name="Dump" type="RoleMapDumpType"/>
            </xs:sequence>
        </xs:choice>
    </xs:sequence>
</xs:complexType>

but I'm getting an error saying that Loader and Loader would be ambiguous.

How can I enforce such a constraint?


Solution

  • I think the following is not considered ambiguous:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      
    <xs:complexType name="RoleMapType">
        <xs:sequence>
            <xs:element minOccurs="0" name="Description" type="xs:string"/>
            <xs:element minOccurs="0" name="Schedule" type="xs:string"/>
            <xs:choice>
              <xs:sequence>
                <xs:element name="Loader" type="RoleMapLoaderType"/>
                <xs:element name="Dump" type="RoleMapDumpType" minOccurs="0"/>            
              </xs:sequence>
              <xs:element name="Dump" type="RoleMapDumpType"/>
            </xs:choice>
        </xs:sequence>
    </xs:complexType>
    
    <xs:complexType name="RoleMapLoaderType"/>
    
    <xs:complexType name="RoleMapDumpType"/>
    
    </xs:schema>