JAXB和匹配的属性名称

时间:2015-02-26 23:09:23

标签: java xml xsd jaxb code-generation

我正在尝试使用XJC实用程序从XSD文件生成类。它工作正常,除非在查看我得到的生成的类时:

* You are getting this "catch-all" property because of the following reason: 
* The field name "Products" is used by two different parts of a schema. See: 
* LINE 16 of FILENAME.xsd
* line 15 of FILENAME.xsd

查看xml:

编辑 - 添加名称空间定义

...
xmlns:def="http://www.host.com/DEFResponse" 
xmlns:abc="http://www.host.com/ABCResponse"
...
<xsd:import namespace="http://www.host.com/ABCResponse" schemaLocation="ABCXMLResponse.xsd"/>
<xsd:import namespace="http://www.host.com/DEFResponse" schemaLocation="DEFXMLResponse.xsd"/>
...
<xsd:choice minOccurs="0">
    <xsd:element name="HostResponse" type="xsd:string"/>
    <xsd:element ref="abc:Products"/>
    <xsd:element ref="def:Products"/>
</xsd:choice>

如何使用绑定可以告诉它创建两个属性,一个叫做ABCProducts,一个叫做DEFProducts?

我下面的尝试不起作用:

<jaxb:bindings schemaLocation="FILENAME.xsd">
    <jaxb:bindings node="//xs:choice">
        <jaxb:bindings node=".//xs:attribute[@ref='abc:Products']">
           <jaxb:property name="ABCProducts"/>
        </jaxb:bindings>
        <jaxb:bindings node=".//xs:attribute[@ref='def:Products']">
            <jaxb:property name="DEFProducts"/>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

我可能做错了什么?

2 个答案:

答案 0 :(得分:0)

<xsd:element ref="abc:Products"/>
<xsd:element ref="def:Products"/>

abc:Productsdef:Products是两个xsd元素,在你的xsd中你定义了xsd:前缀而不是xs: ...所以更改绑定文件如下。

<jaxb:bindings schemaLocation="FILENAME.xsd">
    <jaxb:bindings node="//xsd:choice">
        <jaxb:bindings node="//xsd:element[@ref='abc:Products']">
           <jaxb:property name="ABCProducts"/>
        </jaxb:bindings>
        <jaxb:bindings node="//xsd:element[@ref='def:Products']">
            <jaxb:property name="DEFProducts"/>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

答案 1 :(得分:0)

事实证明,绑定xml也需要:

xmlns:def="http://www.host.com/DEFResponse" 
xmlns:abc="http://www.host.com/ABCResponse"

定义选择工作。

<jaxb:bindings node=".//xs:attribute[@ref='abc:Products']">
       <jaxb:property name="ABCProducts"/>
    </jaxb:bindings>
    <jaxb:bindings node=".//xs:attribute[@ref='def:Products']">
        <jaxb:property name="DEFProducts"/>
    </jaxb:bindings>