Element is not an element of the schema

1.7k Views Asked by At

I want to validate an XML file from my bank against an iso20022 XSD, but it fails claiming the first element (Document) is not an element of the scheme. I can see the 'Document' element defined in the XSD though.

I downloaded the XSD mentioned in the header from here: https://www.iso20022.org/documents/messages/camt/schemas/camt.052.001.06.zip Then I wrote a litte script to validate the XML file:

import xmlschema

schema = xmlschema.XMLSchema('camt.052.001.06.xsd')
schema.validate('minimal_example.xml')

(use 'pip install xmlschema' to install the xmlschema package)

minimal_example.xml is just the first element of my bank's XML file without any children.

<?xml version="1.0" ?>
<Document xmlns:ns2="urn:iso:std:iso:20022:tech:xsd:camt.052.001.06">
</Document>

The above script fails, claiming document was not an element of the XSD:

xmlschema.validators.exceptions.XMLSchemaValidationError: failed validating <Element 'Document' at 0x7fbda11e4138> with XMLSchema10(basename='camt.052.001.06.xsd', namespace='urn:iso:std:iso:20022:tech:xsd:camt.052.001.06'):

Reason: <Element 'Document' at 0x7fbda11e4138> is not an element of the schema

Instance:

  <Document>
  </Document>

But the document element is defined right at the top of camt.052.001.06.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<!--Generated by Standards Editor (build:R1.6.5.6) on 2016 Feb 12 18:17:13, ISO 20022 version : 2013-->
<xs:schema xmlns="urn:iso:std:iso:20022:tech:xsd:camt.052.001.06" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="urn:iso:std:iso:20022:tech:xsd:camt.052.001.06">
    <xs:element name="Document" type="Document"/>
[...]

Why does the validation fail and how can I correct this?

1

There are 1 best solutions below

0
On BEST ANSWER

The XSD has

targetNamespace="urn:iso:std:iso:20022:tech:xsd:camt.052.001.06"

on the xs:schema element, indicating that it governs that namespace.

Your XML has a root element,

<Document xmlns:ns2="urn:iso:std:iso:20022:tech:xsd:camt.052.001.06">
</Document>

which places the Document in no namespace. To place it in the namespace governed by the XSD, change it to

<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.052.001.06">
</Document>

or

<ns2:Document xmlns:ns2="urn:iso:std:iso:20022:tech:xsd:camt.052.001.06">
</ns2:Document>

See also xmlns, xmlns:xsi, xsi:schemaLocation, and targetNamespace?