SQL Server XML schema validation; invalid content error; expecting element with minoccurs set to 0?

574 views Asked by At

I have an XSD from a third party that I'm trying to use to create an XML schema collection in SQL Server and validate XML received from that third party.

Upon validation, I receive an error:

XML Validation: Invalid content. Expected element(s): 'element1','element2'. Found: element 'element3' instead. Location: /DOCUMENT[1]/:reference_data[1]/:element3[1].

The relevant XSD and XML look correct to me and I can't determine why this error is occurring. Here is the relevant XSD trimmed back to the relevant elements (the whole document is huge so I won't post here):

<xs:element name="reference_data">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="element1" minOccurs="0" maxOccurs="256">
                <xs:complexType>
                    <xs:simpleContent>
                        ... some other stuff
                    </xs:simpleContent>
                </xs:complexType>
           </xs:element>
           <xs:element name="element2" minOccurs="0" maxOccurs="50">
               <xs:complexType>
                   <xs:simpleContent>
                       ... some other stuff
                   </xs:simpleContent>
               </xs:complexType>
           </xs:element>
           <xs:choice minOccurs="0" maxOccurs="1000">
               <xs:element name="element3" minOccurs="0" maxOccurs="1000">
               </xs:element>
           </xs:choice>
       </xs:sequence>
   </xs:complexType>
</xs:element>

Here is the relevant XML being validated:

<reference_data>
    <element1>ABCD</element1>
    <element3>
        <associated_detail1>whatever</associated_detail1>
        <associated_detail2>whatever</associated_detail2>
    </element3>
</reference_data>

As you can see, element1 is there, so I don't understand why it found element3 and not element1. Element2 is not there, but it's also defined in the XSD as minoccurs=0, so why is it "expected"?

1

There are 1 answers

0
kimbert On

It is unusual for an XSD validator to issue incorrect validation errors - especially if the XML processor is one of the heavily-used ones (such as the one shipped with a Java VM).

However, if your XSD and XML are exactly as posted then I agree with your assessment. After seeing the first occurrence of /reference_data/element1, the allowed set of 'next tag names' is (element1,element2,element3).

Filburt's comment about the unexpected child elements under element3 is correct, but that should produce a different XSD validation error (one that relates to the content of element3)

I recommend that you re-test with the exact XSD and XML that you posted.