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

后端 未结 3 1094
小蘑菇
小蘑菇 2020-12-01 07:42

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

相关标签:
3条回答
  • 2020-12-01 07:44

    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.

    0 讨论(0)
  • 2020-12-01 07:49

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

    0 讨论(0)
  • 2020-12-01 08:00

    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.

    0 讨论(0)
提交回复
热议问题