为本地complexType指定xsi:type是否有效? [英] Is it valid to specify xsi:type for an local complexType?

查看:94
本文介绍了为本地complexType指定xsi:type是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们遇到了xsi:type的问题,因为我们的服务器升级到jaxb 2.当服务器运行jaxb 1时,有问题的xml客户端请求被解析了,但是由于升级到jaxb 2,我们看到了一个错误。以下行:

We are having a problem with xsi:type since upgrading our server to jaxb 2. The xml client request in question was parsed ok when the server was running jaxb 1, but since upgrading to jaxb 2 we are seeing an error along the lines of:

错误:: cvc-elt.4.2:无法解析'er-request'到元素'er-request的类型定义'

客户端指定 xsi:type 作为xml标记中的属性,但我不认为这是有效的,因为有问题的复杂类型没有这样的名称。

A client is specifying xsi:type as an attribute in a xml tag, but I don't think it's valid since the complex type in question has no such name.

<?xml version="1.0" encoding="UTF-8"?>
<er-request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="100007" xsi:type="er-request">
    <payload>
     <!--some content-->
    </payload>
</er-request>

这是xsd:

<xs:element name="er-request">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="payload" type="payloadType" minOccurs="0"/>
        </xs:sequence>
        <xs:attribute name="id" use="required">
            <xs:simpleType>
                <xs:restriction base="xs:int"/>
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>
</xs:element>

<xs:complexType name="payloadType">
    <xs:sequence>
        <xs:any processContents="skip" minOccurs="0"/>
    </xs:sequence>
</xs:complexType>

因为你可以看到没有名称 complexType中的>属性。我们可以通过添加它来解决错误,但之后我们看到验证错误,因为显然是本地complexType上的name属性:

So as you can see there is no name attribute in the complexType. We can work around the error by adding it, but then we see a validation error since apparently a name attribute on a local complexType:

Attribute' name'不能出现在元素'complexType'

我认为这在jaxb1中起作用的原因是它不如jaxb 2严格。

I presume the reason this worked in jaxb1 was that it was less strict than jaxb 2.

所以问题是:


  1. 客户端xml是否有效?

  2. 我们可以尝试使jaxb2更宽松吗?


推荐答案

您的XML文档是否有效?



不,因为您声明了 xsi:type 属性与XML模式中的命名复杂类型不对应。

Is Your XML Document Valid?

No, as you state the value of the xsi:type attribute does not correspond to a named complex type in your XML schema.

JAXB实现旨在非常容错。我无法让您的示例在最新的JDK 1.7安装或EclipseLink JAXB(MOXy)中使用默认的JAXB impl失败:

JAXB implementations aim to be very fault tolerant. I couldn't get your example to fail using the default JAXB impl in the latest JDK 1.7 install or EclipseLink JAXB (MOXy):

ErRequest

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="er-request")
public class ErRequest {

}

演示

import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(ErRequest.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StreamSource xml = new StreamSource("src/forum22497112/input.xml");
        ErRequest erRequest = (ErRequest) unmarshaller.unmarshal(xml);

    }

}



错误来自



您获得的错误似乎是架构验证错误:

Where is the Error Coming From

The error you are getting appears to be a schema validation error:


错误:: cvc-elt.4.2:无法解析'er-request'到类型定义
元素'er-request'

Error::cvc-elt.4.2: Cannot resolve 'er-request' to a type definition for element 'er-request'

您是否要求JAXB对输入执行架构验证?默认情况下,JAXB不会执行此操作。由于您的文档无效,您必须修复它或注册错误监听器才能忽略它。

Are you asking JAXB to perform schema validation on the input? JAXB does not do this by default. Since your document is not valid you will have to fix it or register an error listener to ignore it.

这篇关于为本地complexType指定xsi:type是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆