Search code examples
xmlxsdxsd-validationxsd-1.1

XSD - How to allow elements in any order any number of times in XML?


I am trying to create a schema and have come across this problem

I am trying to create an XSD, and trying to write the definition with the following requirement:

  • Allow child element specified to appear any number of times (0 to unbounded)
  • Allow child elements to be in any order

XML:

<xml-doc-out>
   <complex sys-name="abcd" isType="sequence" minOccurs="1" maxOccurs="1">
      <property sys-name="A" isType="list" minOccurs="0" maxOccurs="1">
      <property sys-name="B" isType="list" minOccurs="0" maxOccurs="1">
      <complex sys-name="D" isType="sequence" minOccurs="0" maxOccurs="1">
      <complex sys-name="E" isType="sequence" minOccurs="0" maxOccurs="1">
      <property sys-name="F" isType="bool" minOccurs="0" maxOccurs="1">
    </complex>
 </xml-doc-out>

Current XSD:

        <s:complexType name="COMPLEX_DSC_TYPE">
            <s:sequence>
                <s:element minOccurs="0" maxOccurs="unbounded" name="property" type="s0:PROPERTY_DSC_TYPE"/>
                <s:element minOccurs="0" maxOccurs="unbounded" name="complex" type="s0:COMPLEX_DSC_TYPE"/>
            </s:sequence>
        </s:complexType>
        

ERROR:  Unecpected subelement. I guess it's giving issue as element coming after .

What is the correct XSD for the above mention XML?

XSD I Tried:

   <s:complexType name="COMPLEX_DSC_TYPE">
        <s:choice minOccurs="0" maxOccurs="unbounded">
            <s:element name="property" type="s0:PROPERTY_DSC_TYPE"/>
            <s:element name="complex" type="s0:COMPLEX_DSC_TYPE"/>
        </s:choice>
     </s:complexType>

Solution

  • Your XML isn't well-formed but I'm trying to guess your intent from the indentation. I think what you want is

    <s:complexType name="COMPLEX_DSC_TYPE">
       <s:choice minOccurs="0" maxOccurs="unbounded">
          <s:element name="property" type="s0:PROPERTY_DSC_TYPE"/>
          <s:element name="complex" type="s0:COMPLEX_DSC_TYPE"/>
       </s:choice>
    </s:complexType>