Optional XML element not recogniced

You try to validate your XML against a schema and get the message:
The element 'ExternAuszahl' has invalid child element 'ExternAusbezahlterBetrag'. List of possible elements expected: 'ExternerBezuegerName'
You wonder since you know that the first child of ‚ExternAuszahl‘ is the optional ‚ExternerBezuegerName‘ and therefore can be left out. For a fuller understanding, here is the XML portion:

<externauszahl>
  <externausbezahlterbetrag>1548.25</externausbezahlterbetrag>
  <externerbetragfueranzahlmonate>12</externerbetragfueranzahlmonate>
  <startdatum>2008-05-04</startdatum>
  <stoppdatum>2009-12-31</stoppdatum>
</externauszahl>

And the corresponding schma definition:

  <xs :complexType name="ExternAuszahlType">
    </xs><xs :sequence>
      </xs><xs :choice>
        <xs :element name="ExternerBezuegerName" type="PersonenName" />
      </xs>
      <xs :choice>
        </xs><xs :element name="ExterneAuszahlstelle">
          </xs><xs :simpleType>
            </xs><xs :restriction base="xs:string">
              <xs :maxLength value="80" />
              <xs :whiteSpace value="preserve" />
            </xs>
      <xs :sequence>
        </xs><xs :element name="ExternAusbezahlterBetrag">
          </xs><xs :simpleType>
            </xs><xs :restriction base="xs:decimal">
              <xs :fractionDigits value="2" />
            </xs>
        <xs :element name="ExternerBetragFuerAnzahlMonate">
          </xs><xs :simpleType>
            </xs><xs :restriction base="xs:int">
              <xs :minInclusive value="1" />
              <xs :maxInclusive value="12" />
            </xs>
        <xs :choice>
          </xs><xs :element name="WaehrungsCode">
            </xs><xs :simpleType>
            </xs>
      <xs :sequence>
        <xs :element name="StartDatum" type="xs:date" />
        <xs :element name="StoppDatum" type="xs:date" />
      </xs>

To make this work simply add minOccurs="0" and maxOccurs="1" to the choice element in the definition. If the XML still does not validate with the same error, make sure you changed the schema definition you validate against before you go on a wild goose chase.

Schreibe einen Kommentar