0

The default for minOccurs property of an <element> in an XSD file is 1 (source).

Consider you have an XSD specifying a complex type containing 2 elements: <el1> and <el2>.
If you provide an XDocument only containing <el2>, the XDocument will not validate.
Instead you'll get the message:

The element Message in namespace ___ has invalid child element el2 in namespace ___.
List of possible elements expected: el1

This basically is an error on <el2> where one would expect an error on the complex type for not containing <el1>.

My question is:
Is there a way to check if all <element>-tags which have minOccurs > 0 are present?
I would say this is a very basic check when validating XML with an XSD.

Peter
  • 114
  • 3
  • 11
  • I think it is working fine but the error message is just misleading. It is saying "this is not the element I expected to see next". If you do not like this message, your problem is with your validator, not your schema. – Nemo Jun 29 '11 at 12:00
  • Maybe you're right, but when I'm missing more elements with minOccurs=1, I only get an error for the first one missing. Feedback to the user would be inefficient reporting 1 missing element at a time. @Nemo: know any other validators? – Peter Jun 29 '11 at 12:12

1 Answers1

2

Depending on the way you defined your schema, the order of appearance of elements will matter.

In this case the validator is expecting a <el1> but is seeing the element <el2> so the error is that <el2> is appearing where it should not. I belive that means you used a "sequence" when defining your complex type. So the error you at getting is correct.

If this still bothers you, and the order of the elements does not matter to your parsing use "all" instead of "sequence" which will not enforce order. The validator should then prompt you that a required element <el1> is missing. It should look something like the following:

<xsd:complexType name="MyType">
  <xsd:all>
    <xsd:element name="el1" minOccurs="1"/>
    <xsd:element name="el2" minOccurs="1"/>
  </xsd:all>
</xsd:complexType>

I hope this helps.

Rob
  • 2,618
  • 2
  • 22
  • 29
  • you're right, it's the sequence that's the problem here. Unfortunately, the XSD is provided by a third party and we're not fond of changing it... – Peter Jun 29 '11 at 13:00
  • @peter I was affraid of that, but at least you know the validator is not broken. – Rob Jun 29 '11 at 13:23