Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this XDocument fail validation?

Tags:

xml

xsd

Given the schema (anonymised, the key points of interest are renamed and the rest omitted):

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="inspec"
    targetNamespace="the_right_namespace"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:element name="inspec">
    <xs:complexType>
      <xs:all>
        <xs:element name="a_scalar_property" type="xs:int"/>
        <xs:element name="a_collection_property">
          <xs:complexType>
            <snip>
          </xs:complexType>
        </xs:element>
        <xs:element name="another_collection_property">
          <xs:complexType>                
            <snip>
          </xs:complexType>
        </xs:element>                       
      </xs:all>
    </xs:complexType>
  </xs:element>
</xs:schema>

and the instance (declared using VB xml literals):

Dim xDocument = 
<x:inspec xmlns:x='the_right_namespace'>
<a_collection_property/>
<another_collection_property/>
</x:inspec>

validation fails with the message The element 'inspec' in namespace 'the_right_namespace' has incomplete content. List of possible elements expected: 'a_scalar_property'.

Why? The all element, according to W3Schools:

"The all element specifies that the child elements can appear in any order and that each child element can occur zero or one time."

Omitting a_scalar_property is the same as including it zero times. Why does this document fail to validate?

And don't say things like 'post the full code' - this is not my IP and I have anonymised it for a good reason. There is very little else to it, and I have tested with this minimal example, it gives the same result.

like image 240
Tom W Avatar asked Jul 01 '12 10:07

Tom W


People also ask

What is XSD validation error?

The schema is defined by the XSD. Schema errors occur where there is a problem with the structure or order of the file, or an invalid character is included. Schema errors prevent the validation being run in full because the file cannot be read. This means that errors cannot be traced to a particular record.

How to validate XML in c#?

Solution. Use the XmlValidatingReader to validate XML documents against any descriptor document, such as an XSD (XML Schema), a DTD (Document Type Definition), or an XDR (Xml-Data Reduced): public static void ValidateXML( ) { // create XSD schema collection with book.

How to validate DOM?

To validate the XML in the DOM, you can validate the XML as it is loaded into the DOM by passing a schema-validating XmlReader to the Load method of the XmlDocument class, or validate a previously unvalidated XML document in the DOM using the Validate method of the XmlDocument class.


2 Answers

You need to specify the minOccurs="0" for every optional element in xs:all:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="inspec"
    targetNamespace="the_right_namespace"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
    <xs:element name="inspec">
        <xs:complexType>
            <xs:all>
                <xs:element name="a_scalar_property" type="xs:int" minOccurs="0" />
                <xs:element name="a_collection_property" minOccurs="0">
                    <xs:complexType>
                        <!-- snip -->
                    </xs:complexType>
                </xs:element>
                <xs:element name="another_collection_property" minOccurs="0">
                    <xs:complexType>
                        <!-- snip -->
                    </xs:complexType>
                </xs:element>
            </xs:all>
        </xs:complexType>
    </xs:element>
</xs:schema>
like image 85
Filburt Avatar answered Oct 05 '22 12:10

Filburt


To make an element optional, the minOccurrs attribute should be 0, even in an <all> group. Getting that from reading the XML schema specification is really cumbersome, but relying on w3schools is not a good alternative.

like image 20
forty-two Avatar answered Oct 05 '22 12:10

forty-two