Page 1 of 1

XSD validation with minOccurs="1"

Posted: Tue May 04, 2010 7:42 pm
by pdcbob
I have a problem validating an XML document against its XSD. Specifically, I have set minOccurs="1" for an element in the XSD. The element does not exist in the XML file. The XML file validates against the XSD without errors! How can this happen? Example code:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="">
<xs:element name="c1">
<xs:choice maxOccurs="unbounded">
<xs:element name="candidate" minOccurs="1" maxOccurs="1">
<xs:element name="party" minOccurs="1" maxOccurs="1">

<candidate ...>

Note that there is no party element in the XML, and that it has minOccurs="1" in the XSD. The XML file validates without errors against the XSD, both in oXygen and in my C# validation against the same XSD.

What am I doing wrong? Doesn't minOccurs="1" mean that an element MUST exist at least one time in the XML file?

Re: XSD validation with minOccurs="1"

Posted: Wed May 05, 2010 8:34 am
by Radu

You have a choice which contains both candidate and party.
An <xs:choice> allows only one of the contained elements to be present within the containing element. So if candidate appears, there is no party. If you would choose party, then the constraints for it would be used.


Re: XSD validation with minOccurs="1"

Posted: Wed May 05, 2010 5:55 pm
by pdcbob
Thanks, Radu! I now understand the problem, and I will find a way to modify the XSD to make the elements actually required.