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

5

49

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>
Wallen answered 9/11, 2012 at 15:12 Comment(0)
B
47

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>.

Buryat answered 10/11, 2012 at 10:20 Comment(0)
E
8

After making the change suggested above by Martin, I was still getting the same error. I had to make an additional change to my parsing code. I was parsing the XML file via a DocumentBuilder as shown in the oracle docs: https://docs.oracle.com/javase/7/docs/api/javax/xml/validation/package-summary.html

// parse an XML document into a DOM tree
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new File("example.xml"));

The problem was that DocumentBuilder is not namespace aware by default. The following additional change resolved the issue:

// parse an XML document into a DOM tree
DocumentBuilderFactory dmfactory = DocumentBuilderFactory.newInstance();
dmfactory.setNamespaceAware(true);

DocumentBuilder parser = dmfactory.newDocumentBuilder();
Document document = parser.parse(new File("example.xml"));
Exosmosis answered 15/12, 2020 at 14:59 Comment(1)
dmfactory.setNamespaceAware(true); Was misssed out when we were migrating old project from Java 8 to JAVA 11 + . Thankyou this works.Greataunt
S
2

I had this error for my XXX element and it was because my XSD was wrongly formatted according to javax.xml.bind v2.2.11 . I think it's using an older XSD format but I didn't bother to confirm.

My initial wrong XSD was alike the following:

<xs:element name="Document" type="Document"/>
...
<xs:complexType name="Document">
    <xs:sequence>
        <xs:element name="XXX" type="XXX_TYPE"/>
    </xs:sequence>
</xs:complexType>

The good XSD format for my migration to succeed was the following:

<xs:element name="Document">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="XXX"/>
        </xs:sequence>
    </xs:complexType>        
</xs:element>
...
<xs:element name="XXX" type="XXX_TYPE"/>

And so on for every similar XSD nodes.

Stoneham answered 25/5, 2018 at 19:15 Comment(0)
H
1

I got this same error working in Eclipse with Maven with the additional information

schema_reference.4: Failed to read schema document 'https://maven.apache.org/xsd/maven-4.0.0.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

This was after copying in a new controller and it's interface from a Thymeleaf example. Honestly, no matter how careful I am I still am at a loss to understand how one is expected to figure this out. On a (lucky) guess I right clicked the project, clicked Maven and Update Project which cleared up the issue.

Heins answered 16/2, 2020 at 20:29 Comment(0)
P
0

To expand upon the top answer. If you're using Java Web Services (JAX-WS) annotations to define your services, like in this example:

@WebService(..., targetNamespace = "http://bar.foo.com/")

Then make sure that your SOAP request has exactly the same namespace as defined in your annotation:

<soapenv:Envelope 
   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
   xmlns:foo="http://bar.foo.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <foo:someRequest>
          ...
      </foo:someRequest>
   </soapenv:Body>
</soapenv:Envelope>

The targetNamespace in your annotation and the xmlns:foo property in the XML request must match! Literally every character (including whitespace) must match. Also don't forget to put the / at the end as well (it's a very common mistake).

Pipistrelle answered 22/4, 2022 at 7:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.