cvc-elt.1: Cannot find the declaration of element 'MyElement'

Mitko Mitev picture Mitko Mitev · Nov 9, 2012 · Viewed 135.9k times · Source

I'm trying to validate a really simple xml using xsd, but for some reason I get this error. I'll really appreciate if someone can explain me why.

XML File

<?xml version="1.0" encoding="utf-8"?> 
<MyElement>A</MyElement>

XSD File

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.example.org/Test"
        xmlns:tns="http://www.example.org/Test"
        elementFormDefault="qualified">

    <simpleType name="MyType">
        <restriction base="string"></restriction>
    </simpleType>

    <element name="MyElement" type="tns:MyType"></element>
</schema>

Answer

Martin Honnen picture Martin Honnen · Nov 10, 2012

Your schema is for its target namespace http://www.example.org/Test so it defines an element with name MyElement in that target namespace http://www.example.org/Test. Your instance document however has an element with name MyElement in no namespace. That is why the validating parser tells you it can't find a declaration for that element, you haven't provided a schema for elements in no namespace.

You either need to change the schema to not use a target namespace at all or you need to change the instance to use e.g. <MyElement xmlns="http://www.example.org/Test">A</MyElement>.