XML won't validate against certain XSD

863 Views Asked by At

The XML (simplified):

<?xml version="1.0" encoding="UTF-8"?>
<mx:XMLimport xmlns:mx="http://www.w3.org/2001/XMLSchema" 
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Leverancier>nn</Leverancier>
    <Bestandsversie>1.1.0.0</Bestandsversie>
</mx:XMLimport>

The XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="qualified">
    <xsd:element name="XMLimport">
        <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="Leverancier" type="xsd:string" minOccurs="1"/>
              <xsd:element name="Bestandsversie" type="xsd:string" minOccurs="1"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

If I remove the mx: prefix from the root element of the XML it validates just fine. I'm lost in name spaces and I'm searching the internet for several hours now. I can't change the XML so the XSD have to be adapted to allow the mx: prefix. The validator gives this error:

ERROR: Element '{http://www.w3.org/2001/XMLSchema}XMLimport':
       No matching global declaration available for the validation root.
1

There are 1 best solutions below

2
On BEST ANSWER

I assume your question is what should the XSD look like to match your XML. This is the corrected XSD:

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema targetNamespace="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="XMLimport">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Leverancier" type="xsd:string"/>
                <xsd:element name="Bestandsversie" type="xsd:string"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>  
</xsd:schema>

Your XSD doesn't define a targetNamespace attribute. Hence, your XMLimport must show without a namespace in instance documents; this is why when you remove the namespace from your XMLimport in the XML document, it becomes valid.

If you add targetNamespace="http://www.w3.org/2001/XMLSchema" you'll now have an XSD that will validate your XML; in general, the value of the targetNamespace must match the namespace of your document element (chameleon XSDs are a special case).

Another thing at play here is elementFormDefault which by default is unqualified. This setting is that which makes the inner elements Leverancier and Bestandsversie without a namespace.

Another thing to notice is that while it is highly unusual to see user defined content targeting the http://www.w3.org/2001/XMLSchema namespace, there's no provision in the XSD 1.0 spec which would explicitly prohibit users to target this namespace. This is unlike the other namespace you see in your sample XML, http://www.w3.org/2001/XMLSchema-instance: it is explicitly prohibited to target this namespace in user defined XSDs.

This SO post might help, too.