声明导致ObjectFactory中的冲突

时间:2019-03-04 12:13:11

标签: java xml jaxb

我有两个xsd导入到主xsd中。两者具有相同的元素,但名称空间不同。因此,我为第二个xsd(B.xsd)创建了一个自定义绑定。我仍然收到“两个声明在ObjectFactory类中导致冲突”。据我了解,自定义绑定实际上将为默认XML名称到Java标识符映射无法自动处理的情况提供有意义的自定义名称,因此将避免冲突。

A.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="urn:abc:def:ghi" xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="lotNumber" type="xs:string"/>
    <xs:element name="itemExpirationDate" type="xs:date" />

</xs:schema>

B.xsd

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns="http://abc.def.org/kj/ns"
     targetNamespace="http://abc.def.org/kj/ns"
     elementFormDefault="qualified"
     attributeFormDefault="unqualified">
    <xs:element name="ilmd">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="lotNumber" minOccurs="0"/>
                <xs:element ref="itemExpirationDate" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="lotNumber" type="xs:string"/>
    <xs:element name="itemExpirationDate" type="xs:string"/>
</xs:schema>

binding.xml

<jaxb:bindings
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.0">

    <jaxb:bindings
        schemaLocation="./xsd/B.xsd" node="/xs:schema">

        <jaxb:bindings node="//*/xs:element[@name='lotNumber']">
            <jaxb:property name="ucbLotNumber" />
        </jaxb:bindings>

        <jaxb:bindings node="//*/xs:element[@name='itemExpirationDate']">
            <jaxb:property name="ucbItemExpiratonDate" />
        </jaxb:bindings>

    </jaxb:bindings>

</jaxb:bindings>

pom.xml

<plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-xjc-plugin</artifactId>
                <configuration>
                    <extensions>
                        <extension>org.apache.cxf.xjcplugins:cxf-xjc-ts:3.0.3</extension>
                    </extensions>
                </configuration>
                <executions>
                    <execution>
                        <id>Xsd2Java</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>xsdtojava</goal>
                        </goals>
                        <configuration>
                            <sourceRoot>${basedir}/target/generated-sources/cxf</sourceRoot>
                            <xsdOptions>
                                <xsdOption>
                                    <xsd>${basedir}/src/main/resources/xsd/Main.xsd</xsd>
                                    <packagename>abc.def</packagename>
                                    <bindingFile>src/main/resources/binding.xml</bindingFile>
                                    <extension>true</extension>
                                    <extensionArgs>
                                        <arg>-encoding</arg>
                                        <arg>UTF-8</arg>
                                    </extensionArgs>
                                </xsdOption>
                            </xsdOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

1 个答案:

答案 0 :(得分:0)

如果元素内部包含复杂类型,则应将名称属性添加到复杂类型

<xs:element name="ilmd">
        <xs:complexType **name="ilmdArray"**>
            <xs:sequence>
                <xs:element ref="lotNumber" minOccurs="0"/>
                <xs:element ref="itemExpirationDate" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>