jaxb为同一类型生成的多个类包含外部绑定

时间:2016-05-23 14:00:30

标签: jaxb xjc maven-jaxb2-plugin

我有以下架构和相应的绑定文件。我使用jaxb2 maven插件生成JAXB类。

person.xsd

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

    <xs:element name="person">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="pType" type="pType" minOccurs="0" />
                <xs:element name="sex" type="xs:string"
                    minOccurs="1" />
                <xs:element name="dob" type="xs:string"
                    minOccurs="1" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
<!-- some more elements ignored for clarity.................
.......................... -->

    <xs:complexType name="pType">
        <xs:sequence>
            <xs:element name="category" type="xs:string"
                minOccurs="0" />
            <xs:element name="blahh" type="xs:string"
                minOccurs="0" />
        </xs:sequence>
    </xs:complexType>

</xs:schema>

jaxb绑定

<jxb:bindings schemaLocation="person.xsd">
    <jxb:bindings node="//xs:complexType[@name='pType']">
        <jxb:class name="PersonType" />
    </jxb:bindings>
</jxb:bindings>

<jxb:bindings schemaLocation="person.xsd">
    <jxb:bindings
        node="//xs:element[@name='person']//xs:element[@name='pType']">
        <jxb:class ref="PersonType" />
    </jxb:bindings>
</jxb:bindings>

我已定义绑定以将<xs:complexType name="pType">的名称覆盖为PersonType。在XJC生成中,它生成PersonType.class和PType.class。

如果我在元素<xs:complexType name="pType">内部定义<xs:element name="pType" >,那么它就不会生成PType.class。 但我必须在模式的根级别声明<xs:complexType name="pType">,因为此xs:complexType也被其他模式引用。

我试图在绑定中覆盖<xs:complexType name="pType"><xs:element name="pType" >,但是生成了PType.class。

我怎么能指示它不要生成PType.class?

1 个答案:

答案 0 :(得分:1)

问题是我在person2.xsd中为每个模式(依赖模式person.xsdmaven-jaxb2-plugin)执行了2次执行。所以我手动编写了episode文件来引用已创建的<xs:complexType name="pType">并解决了问题。

为了有相同问题的人的利益,这里有插件执行细节和episode文件。请注意,我没有使用namespaces因此您发现scd很简单,没有命名空间。

<强>人集的

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" if-exists="true" version="2.1">

 <jaxb:bindings scd="x-schema::">
    <jaxb:bindings scd="/type::pType">
      <jaxb:class ref="org.wipo.pct.test.PersonType"/>
      <jaxb:package name="org.wipo.pct.test" />
    </jaxb:bindings>
  </jaxb:bindings>
</jaxb:bindings>

<强>的pom.xml

<execution>
        <id>person</id>
        <phase>generate-sources</phase>
        <goals>
            <goal>generate</goal>
        </goals>
        <configuration>
            <schemaIncludes>
                <include>person.xsd</include>
            </schemaIncludes>
            <bindingIncludes>
                <include>pbindings.xjb</include>
            </bindingIncludes>
            <verbose>true</verbose>
        </configuration>
    </execution>
<execution>
    <id>person2</id>
    <phase>generate-sources</phase>
    <goals>
        <goal>generate</goal>
    </goals>
    <configuration>
        <schemaIncludes>
            <include>person2.xsd</include>
        </schemaIncludes>
        <bindingIncludes>
            <include>pbindings2.xjb</include>
        </bindingIncludes>
        <verbose>true</verbose>
        <args>
            <arg>-b</arg>
            <arg>src/main/resources/person-episode</arg>
            <arg>-Xsimplify</arg>
            <arg>-Xinheritance</arg>
        </args>
    </configuration>
</execution>