如何使用jaxb2绑定架构以获取重复的元素名称

时间:2018-10-25 10:51:44

标签: java jaxb maven-jaxb2-plugin jaxb2-maven-plugin

我正在尝试从多个xsd文件生成一个Java类。 在这些架构文件中,有多个complexType或元素在不同的架构中具有相同的名称。

请注意,我无法更改架构。

我创建了一个绑定文件,但仍然出现此错误

  

EXAMPLE_QualifyRQ.xsd; ...“ myelement”已经定义

我的配置中有什么想念的吗?

所有模式都位于1个目录中,例如:

xsd:

  • EXAMPLE_QualifyRS.xsd
  • EXAMPLE_QualifyRQ.xsd
  • EXAMPLE_PurchaseRQ.xsd
  • EXAMPLE_CommonTypes.xsd
  • EXAMPLE_SimpleTypes.xsd

EXAMPLE_QualifyRS.xsd

<xs:schema xmlns="http://www.example.org/EXAMPLE/2007/00" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/EXAMPLE/2007/00"
 elementFormDefault="qualified" version="1.002" id="EXAMPLE2016.1">
     <xs:include schemaLocation="EXAMPLE_CommonTypes.xsd"/>
     <xs:element name="myelement">...</xs:element>

EXAMPLE_QualifyRQ.xsd

<xs:schema xmlns="http://www.example.org/EXAMPLE/2007/00" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/EXAMPLE/2007/00"
 elementFormDefault="qualified" version="1.002" id="EXAMPLE2016.1">
     <xs:include schemaLocation="EXAMPLE_CommonTypes.xsd"/>
     <xs:element name="myelement">...</xs:element>

EXAMPLE_PurchaseRQ.xsd

<xs:schema xmlns="http://www.example.org/EXAMPLE/2007/00" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/EXAMPLE/2007/00" elementFormDefault="qualified" version="1.001" id="EXAMPLE2016.1">
    <xs:include schemaLocation="EXAMPLE_CommonTypes.xsd"/>
    <xs:include schemaLocation="EXAMPLE_SimpleTypes.xsd"/>
    <xs:element name="myelement">...</xs:element>

这是我如何在maven中生成类

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>example-schema</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                        <configuration>
                            <xsdPathWithinArtifact>xsd</xsdPathWithinArtifact>
                            <addGeneratedAnnotation>true</addGeneratedAnnotation>
                            <laxSchemaValidation>true</laxSchemaValidation>
                            <laxSchemaValidation>true</laxSchemaValidation>
                            <readOnly>true</readOnly>
                            <verbose>true</verbose>
                            <sources>
                                <source>src/main/resources/xsd</source>
                            </sources>
                            <xjbSources>
                                <xjbSource>src/main/resources/xjb</xjbSource>
                            </xjbSources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

还有我的绑定

    <?xml version="1.0" encoding="UTF-8"?>
    <bindings version="2.1"
              xmlns="http://java.sun.com/xml/ns/jaxb"
              xmlns:xs="http://www.w3.org/2001/XMLSchema"
              xmlns:xsd="http://www.w3.org/2001/XMLSchema"
              xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
              extensionBindingPrefixes="xjc">
        <globalBindings>
            <serializable uid="1"/>
            <xjc:javaType name="java.time.LocalDateTime" xmlType="xs:dateTime" adapter="com.example.adapter.LocalDateTimeAdapter"/>
            <xjc:javaType name="java.time.LocalDate" xmlType="xsd:date" adapter="com.example.adapter.LocalDateAdapter"/>
        </globalBindings>

        <bindings schemaLocation="../xsd/EXAMPLE_QualifyRS.xsd" node="/xs:schema">
            <schemaBindings>
                <package name="com.example.qualify.response"/>
            </schemaBindings>
            <bindings node="//xs:element[@name='myelement']">
                <class name="QualifyMyElement"/>
            </bindings>
        </bindings>

<bindings schemaLocation="../xsd/EXAMPLE_QualifyRQ.xsd" node="/xs:schema">
            <schemaBindings>
                <package name="com.example.qualify.request"/>
            </schemaBindings>
            <bindings node="//xs:element[@name='myelement']">
                <class name="QualifyMyElement"/>
            </bindings>
        </bindings>

        <bindings schemaLocation="../xsd/EXAMPLE_PurchaseRQ.xsd" node="/xs:schema">
            <schemaBindings>
                <package name="com.example.purchase"/>
            </schemaBindings>
            <bindings node="//xs:element[@name='myelement']">
                <class name="PurchaceMyElement"/>
            </bindings>
        </bindings>
    </bindings>

0 个答案:

没有答案