【问题标题】:JAXB and matching property namesJAXB 和匹配的属性名称
【发布时间】:2015-04-29 13:06:47
【问题描述】:

我正在尝试使用 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>

我可能做错了什么?

【问题讨论】:

  • 您应该使用绑定文件来指定这些碰撞元素的类名。另外,您是否在 xsds 中指定了命名空间?
  • 我在最后一个代码块中引用了我尝试对绑定文件执行的操作。命名空间定义: xmlns:def="host.com/DEFResponse" xmlns:abc="host.com/ABCResponse"
  • 但是你在 abc 和 def 模式中有 targetNamespace 吗?如果它们有不同的命名空间,那么它们不应该冲突
  • 是的,额外包含的两个响应 XSD 都有 targetNamespace,例如:host.com/ABCResponse" xmlns:xsd="w3.org/2001/XMLSchema" xmlns="host.com/ABCResponse" elementFormDefault ="qualified" attributeFormDefault="unqualified" version="2.609">

标签: java xml xsd jaxb code-generation


【解决方案1】:
<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>

【讨论】:

    【解决方案2】:

    原来绑定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> 
    

    【讨论】:

      猜你喜欢
      • 2013-02-13
      • 2013-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-04
      • 1970-01-01
      相关资源
      最近更新 更多