【发布时间】:2019-04-10 20:51:06
【问题描述】:
我正在尝试根据 xsd 架构验证 xml,而不是使用从我们的 WSDL 文件派生的 Java 类型使用 WSDL 到 Java。
我们的最外层元素标签是在 WSDL 中定义的,但是我们需要针对 xsd 模式进行验证,因此我们尝试将最外层元素标签添加到 xsd。但是,当最外层的包装器包含与 xsd 文件的 targetNamespace 不同的命名空间时,xml 验证失败。
简化的 xml
<ns6:responseWrapper
xmlns="http://somewhere.com/types/2016/A"
xmlns:ns6="http://somewhere.com/operations/2016/A"
>
<user>
<id>the_id</id>
<someInfo>the_source</someInfo>
</user>
</ns6:responseWrapper>
简化架构
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://somewhere.com/types/2016/A"
targetNamespace="http://somewhere.com/types/2016/A"
elementFormDefault="qualified"
>
<xsd:element
xmlns:ns6="http://somewhere.com/operations/2016/A"
name="responseWrapper"
type="tns:ResponseWrapper"
/>
<xsd:complexType name="ResponseWrapper">
<xsd:element name="user" type="tns:User"/>
</xsd:complexType>
<xsd:complexType name="User">
<xsd:sequence>
<xsd:element name="id" type="xsd:string"/>
<xsd:element name="someInfo" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
上面的xml和schema生成Cannot find the declaration of element 'ns6:responseWrapper` errors.
我想修改架构,以便 xml 成功验证。
【问题讨论】:
-
ResponseWrapper,带有大写字母 R,应该是complexType。 -
@acelent 谢谢,我已经更新了。
-
嗯,好的,你已经更新了,但是你测试了吗?
element周围不应该有一个名为“用户”的sequence吗?另外,我看到您没有在正确的命名空间中指定responseWrapper,例如ns6:responseWrapper。尽管如此,IIRC,你应该在不同的模式中声明不同命名空间的元素,与模式的目标命名空间一致。
标签: xml xsd xsd-validation