Question

I am trying to create a schema and have come across this problem, although I have found a solution that should work (XSD - how to allow elements in any order any number of times?) in my case it doesn't.

<xsd:element name="foo">
<xsd:complexType>
    <xsd:choice>
          <xsd:element ref="p" maxOccurs="unbounded"/> *--element p is complex--*
          <xsd:element ref="f" maxOccurs="unbounded"/> *--element f is complex--*
          <xsd:element ref="summary"/>
    </xsd:choice>
      <xsd:attribute ref="type"/>
</xsd:complexType>
</xsd:element>

using this to validate the xml below brings back the error 'Unexpected child element':

<foo type="###">
    <p type="###">
       <pr date="##/##/##" amount="###"/>
       <pr date="##/##/##" amount="###"/>
    </p>
    <f type="###">
       <fr date="##/##/##" factor="###"/>
       <fr date="##/##/##" factor="###"/>
    </f>
    <p type="###">
       <pr date="##/##/##" amount="###"/>
       <pr date="##/##/##" amount="###"/>
    </p>
    <f type="###">
       <fr date="##/##/##" factor="###"/>
       <fr date="##/##/##" factor="###"/>
    </f>
    <summary>
        <p_summary date="##/##/##" p="####" dis="###" ......./>   
        <p_summary date="##/##/##" p="####" dis="###" ......./>
        <p_summary date="##/##/##" p="####" dis="###" ......./>
    </summary>
</foo>

I haven't listed the definitions for p f and summary but they both contain maxOccurs="unbounded" for their respective elements (fr, pr, p_summary).

Was it helpful?

Solution

It is the <xsd:choice> that must be unbounded here. You correct schema should look like this:

<xsd:element name="foo">
<xsd:complexType>
    <xsd:choice maxOccurs="unbounded">
        <xsd:element ref="p"/>
        <xsd:element ref="f"/>
        <xsd:element ref="summary"/>
    </xsd:choice>
    <xsd:attribute ref="type"/>
</xsd:complexType>
</xsd:element>

Setting maxOccurs="unbounded" by each element (p, f, summary) won't make any difference here. It just allows you to repeat the same element many times, but not mix with others.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top