xjc错误:"两个声明导致冲突"在有效的.xsd上

时间:2015-01-14 09:30:31

标签: java validation xjc

我有一个有效的.xsd作为API描述。这是(没有冗余代码):

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:simpleType name="String">
        <xsd:restriction base="xsd:string"/>
    </xsd:simpleType>

    <xsd:complexType name="ErrorType">
          <xsd:sequence>
            <xsd:element name="Type">          
                <xsd:complexType>
                      <xsd:attribute name="Name" type="String" use="required"/>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>        
    </xsd:complexType>

    <xsd:complexType name="ErrorTypeType">
        <xsd:attribute name="Code" type="String" use="required"/>
        <xsd:attribute name="Name" type="String" use="required"/>
    </xsd:complexType>
</xsd:schema>

http://www.validome.org/grammar/validate/

验证

xjc MySchema.xsd引发了以下错误:

parsing a schema...
compiling a schema...
[ERROR] Two declarations cause a collision in the ObjectFactory class.
  line 17 of file:/C:/Users/dev05/Downloads/MySchema.xsd

[ERROR] (Related to above error) This is the other declaration.
  line 10 of file:/C:/Users/dev05/Downloads/MySchema.xsd

Failed to produce code.

我应该要求API创建者更改它(这很难)或者有一些解决方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

要解决这个问题,我添加了绑定文件:

<强> bindings.xml

<jaxb:bindings 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    jaxb:extensionBindingPrefixes="xjc"
    version="2.1">
    <jaxb:globalBindings generateIsSetMethod="true" fixedAttributeAsConstantProperty="true">
        <xjc:serializable />
    </jaxb:globalBindings>
    <jaxb:bindings schemaLocation="Common.xsd">
        <jaxb:bindings node="//xs:complexType[@name='ErrorTypeType']">
               <jaxb:class name="ErrorTypeType1"/>
        </jaxb:bindings>
   </jaxb:bindings>
</jaxb:bindings>

shell命令将是:

xjc -b bindings.xml -extension MySchema.xsd