xmlxsdxml-parsingxsd-validationxml-validation

XSD to define complex type OR array of elements


How can define OR logic for elements in my XSD file? Unfortunately I can't make changes in "OLD STYLE BLOCK" structure, I am just extending existing XML structure.

The only idea I have is something like this example XSD below but I have parser error I hoped I can include xsd:choice and xsd:sequence to xs:complexType

Can I use Version="2.0" somehow to add extra logic to xsd ?

<?xml version="1.0"?>
<MyStruct Version="2.0">

   <!-- xs should allow "NEW STYLE BLOCK" OR "OLD STYLE BLOCK" with array of id -->
   <!-- one of them should present !!!! -->

  <!-- NEW STYLE BLOCK my complex type-->
  <IDArray> <
    <IDSet>
        <ID>89</ID>
        <ID>1</ID>
        <ID>2</ID>
    </IDSet>
    <IDSet>
        <ID>3</ID>
        <ID>4</ID>
    </IDSet>
  </IDArray>
  <!-- end of NEW STYLE BLOCK -->

  <!-- OLD STYLE BLOCK  array of elements  -->
  <ID>5</ID>
  <ID>7</ID>
  <!-- end of OLD STYLE BLOCK -->

  <Type>Some type</Type>  <!-- mandatory element -->
  <ReadableName>Human readable name</ReadableName>  <!-- mandatory element -->
</MyStruct>

Proposed XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.1">

  <xs:element name="MyStruct">
    <xs:complexType>

        <xs:choice>
            <xs:element name="IDArray" minOccurs="0" maxOccurs="1">
                <xs:complexType>
                      <xs:choice maxOccurs="unbounded">
                        <xs:element name="IDSet" minOccurs="1" maxOccurs="unbounded">
                            <xs:complexType>
                                  <xs:choice maxOccurs="unbounded">
                                      <xs:element name="ID" minOccurs="1" maxOccurs="unbounded" />
                                  </xs:choice>
                            </xs:complexType>
                        </xs:element>
                      </xs:choice>
                </xs:complexType>
            </xs:element>

            <xs:element name="ID" minOccurs="0" maxOccurs="unbounded" />
        </xs:choice>

        <xs:sequence>
            <xs:element name="Type" type="xs:string" />
            <xs:element name="ReadableName" type="xs:string" />
        </xs:sequence>  <!--  error in this line  -->

      <xs:attribute name="Version" type="xs:string" />

    </xs:complexType>
  </xs:element>
</xs:schema>

Error message , I assume xsd:choice and xs:sequence are not allowed simultaneously

./MyStruct.xsd:: element sequence: Schemas parser error : Element '{http://www.w3.org/2001/XMLSchema}complexType': The content is not valid. Expected is (annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))).

Solution

  • Try

      <xs:element name="MyStruct">
        <xs:complexType>
          <xs:sequence>
    
            <xs:choice>
                <xs:element name="IDArray" minOccurs="0" maxOccurs="1">
                    <xs:complexType>
                          <xs:choice maxOccurs="unbounded">
                            <xs:element name="IDSet" minOccurs="1" maxOccurs="unbounded">
                                <xs:complexType>
                                      <xs:choice maxOccurs="unbounded">
                                          <xs:element name="ID" minOccurs="1" maxOccurs="unbounded" />
                                      </xs:choice>
                                </xs:complexType>
                            </xs:element>
                          </xs:choice>
                    </xs:complexType>
                </xs:element>
    
                <xs:element name="ID" minOccurs="0" maxOccurs="unbounded" />
            </xs:choice>
    
            <xs:element name="Type" type="xs:string" />
            <xs:element name="ReadableName" type="xs:string" />
    
         </xs:sequence> 
    
         <xs:attribute name="Version" type="xs:string" />
    
    
        </xs:complexType>
      </xs:element>