1

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.
M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
HerbertV
  • 113
  • 6
  • 1
    There's something very wrong with that XML. It's is using `http://www.w3.org/2001/XMLSchema` as its namespace, which is obviously the namespace of the XSD format (which, as far as I can tell, has no `XMLimport` element). Where did you get this XML? – JLRishe Feb 09 '13 at 15:27
  • Hi JL, The XML is from a vendor, the vendor can't/won't change it. If I just remove the "mx:" prefix from the XML it works fine. Since that's not possible in production modus I have to change my own XSD. – HerbertV Feb 09 '13 at 15:34
  • 1
    You're not the only one who is lost with regard to XML namespaces -- the source of that XML is obviously also lost. There is no XMLImport element in the namespace http://www.w3.org/2001/XMLSchema. If the vendor really can't or won't change it, I'd suggest getting a new vendor. If you can't or won't do that, fix the XML anyway: run it through a filter to clean up the namespace declarations before validating. – C. M. Sperberg-McQueen Feb 10 '13 at 19:08

1 Answers1

2

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.

Community
  • 1
  • 1
Petru Gardea
  • 21,373
  • 2
  • 50
  • 62
  • The `Leverancier` and `Bestandsversie` elements of the source XML appear to be in the null namespace. Wouldn't that mean they need to be defined in a separate XSD? – JLRishe Feb 10 '13 at 18:35
  • @JLRishe, no; this is why I pointed out `elementFormDefault` in my response. – Petru Gardea Feb 10 '13 at 21:58