简体   繁体   中英

xsd validation fails with cvc-elt.1: Cannot find the declaration of element

I am validating my xml using my xsd in java:

javax.xml.validation.SchemaFactory
.newInstance("http://www.w3.org/2001/XMLSchema")
.newSchema(new java.io.File(schemaPath))
.newValidator()
.validate(new javax.xml.transform.stream.StreamSource(new java.io.FileInputStream(xmlPath)));

and I get the following error:

org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 18; cvc-elt.1: Deklaration des Elements "WrappedBodyText" kann nicht gefunden werden.

I reduced my xml to the following:

<WrappedBodyText></WrappedBodyText>

I reduced my xsd to:

<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://compa.ny/customer/schema/Wrapper"
        xmlns="http://www.w3.org/2001/XMLSchema"
        xmlns:Wrapper="http://compa.ny/customer/schema/Wrapper"
        xmlns:WrapperType="http://compa.ny/customer/schema/WrapperType"
        xmlns:standardservice="http://compa.ny/standard/service/schema">

    <complexType name="WrappedBodyText">
    </complexType>
</schema>

I looked through several posts here or on other forums, but none of the errors I found seem to apply. Please help

As far as I have understood it a single definition of a complexType is not enough. I had to define an element too:

<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://compa.ny/customer/schema/Wrapper"
        xmlns="http://www.w3.org/2001/XMLSchema"
        xmlns:Wrapper="http://compa.ny/customer/schema/Wrapper"
        xmlns:WrapperType="http://compa.ny/customer/schema/WrapperType"
        xmlns:standardservice="http://compa.ny/standard/service/schema">

    <complexType name="WrappedBodyText">
    </complexType>

    <element name="WrappedBodyText" type="Wrapper:WrappedBodyText"/>
</schema>

But there was also an error in my xml instance. The validator found an element WrappedBodyText with empty namespace, but was expecting an element in the defined targetNamespace. So I changed the xml instance too:

<WrappedBodyText xmlns="http://compa.ny/customer/schema/Wrapper"></WrappedBodyText>

I guess the combination of those two errors was the worst part about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM