3

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'.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Charlie
  • 2,141
  • 3
  • 19
  • 35
  • I don't view this error as necessarily being unexpected. The error output is telling you that it hit an `` element when it really expected an `` element instead. And from the point of view of validation, it is still failing when it should, just with an error message which you didn't expect. – Tim Biegeleisen Jul 05 '16 at 05:04
  • Thanks Tim, I think you're right. – Charlie Jul 06 '16 at 05:33

1 Answers1

1

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.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thanks @kjhughes. However, I didn't interpreted the message as you said. I know the message _The element 'class' has invalid child element 'el3'._ is correct, I just hope if there's any chance I only get a single message said _List of possible elements expected: 'el2'._ – Charlie Jul 11 '16 at 09:21
  • Perhaps you meant *desired*, not *expected* then. The answer to your question then is, yes, if you wish to override the default error handler behavior, you can write whatever message you'd like; frankly, however, I don't see it being worth the effort here. – kjhughes Jul 11 '16 at 12:09
  • Yes, I have persuaded my colleagues to just leave as it is. BTW, as you mentioned _"you can write whatever message you'd like"_, it'd be great if you could give me some hints or links that I can look at? – Charlie Jul 12 '16 at 00:43
  • Hint: Set a custom `ValidationEventHandler`: `doc.Validate(xmlSchemaSet, new ValidationEventHandler(CustomHandler), true);` – kjhughes Jul 12 '16 at 01:07