简体   繁体   English

使用cvc-elt.1进行xsd验证失败:无法找到元素的声明

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

I am validating my xml using my xsd in java: 我在java中使用我的xsd验证我的xml:

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: 我将我的xml缩减为以下内容:

<WrappedBodyText></WrappedBodyText>

I reduced my xsd to: 我把我的xsd减少到:

<?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. 但是我的xml实例中也存在错误。 The validator found an element WrappedBodyText with empty namespace, but was expecting an element in the defined targetNamespace. 验证器找到了一个带有空命名空间的元素WrappedBodyText,但期望在定义的targetNamespace中有一个元素。 So I changed the xml instance too: 所以我也改变了xml实例:

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

I guess the combination of those two errors was the worst part about it. 我猜这两个错误的组合是关于它的最糟糕的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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