I'm writing a Python script that creates XML data, and then validates it using an XSD file. At one particular place in the XML data, I get a validation error even though things look correct.
First, here's an extract from the XSD file
<xs:complexType name="address">
<xs:all>
<xs:element name="city" type="string255Length" minOccurs="0">
<xs:element name="postal_code" type="string255Length" minOccurs="0">
I interpret this as the "address" tag can have zero or one "city" and "postal_code" tags. As simplified version of my xml data is this:
<address>
<city>London</city>
</address>
I'd thought that this was legal xml data according to the XSD. This is the code:
schema_doc = etree.parse(xsd_file)
schema = etree.XMLSchema(schema_doc)
schema.assertValid(xml_data)
When I run that code, I get
Missing child element(s). Expected is one of ( postal_code )
To me, it looks like the XML validator ignores the minOccurs="0"
statement, thus requiring all the elements to be present.
Does anyone know what's causing this seemingly unexpected error?
lxml
seems to not respectminOccurs=0
. Did you ever come to a resolution @kenneho? I will update if I find a fix. – henrycjc May 31 '19 at 0:39