Charlie
Charlie

Reputation: 2263

XSD Validation Error when mandatory element is missing from sequence complex type

I have an XSD and incorrect XML.

The XSD has a complex type with a sequence of elements. All those elements are mandatory. (The XSD is maintained by a 3rd party and can't be changed)

The incorrect XML is missing one element.

When I validate the XML against the XSD using C#, the expected error is "the 'XXX' element is expected". But actually, it also tells me "the element has invalid child element". I'm not sure what should I do.

To help you understand my question, I'll show you an example:

<!-- Incorrect XML -->
<class>
  <el1>222</el1>
  <el3>222</el3>
</class>

<!-- XSD -->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="class">
    <xs:complexType>
      <xs:sequence>
        <xs:element type="xs:short" name="el1" />
        <xs:element type="xs:short" name="el2" />
        <xs:element type="xs:short" name="el3"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

The real but unexpected validation errors are something like below:

The element 'class' has invalid child element 'el3'. List of possible elements expected: 'el2'.

The expected errors are:

List of possible elements expected: 'el2'.

Upvotes: 3

Views: 1528

Answers (1)

kjhughes
kjhughes

Reputation: 111561

You're misunderstanding the message. It's not saying that el3 cannot ever be a child of class. It's saying that el3 is invalid at the point at which it was encountered it in the process of parsing.

In other words, as stated,

The element 'class' has invalid child element 'el3'.

is correct.

On the other hand, the way you're interpreting the message,

The element 'class' can never have child element 'el3'.

would indeed be incorrect. However, since it doesn't actually say that, the diagnostic message is fine as is.

Upvotes: 1

Related Questions