如何将generateElementProperty设置为绑定文件中的本地属性

时间:2021-11-09 21:15:48

I'm having an issue with jaxb. One of my elements is requiring me to set a JAXBElement<String>, instead of just a String. I know that a solution to this is to change the xsd, as discussed here JAXB generating JAXBElement<String> instead of String, but the xsd was provided by the vendor, and we can't convince them to change it.

我遇到了jaxb的问题。我的一个元素是要求我设置一个JAXBElement ,而不仅仅是一个String。我知道解决这个问题的方法是更改​​xsd,这里讨论JAXB生成JAXBElement 而不是String,但xsd是由供应商提供的,我们无法说服他们更改它。

Apparently another solution is to specify a property <jaxb:globalBindings generateElementProperty="false">, which would turn off the JAXBElements globally, which is also not what we want.

显然,另一个解决方案是指定一个属性 ,它将全局关闭JAXBElements,这也不是我们想要的。

According to this page, it should be possible to set generateElementProperty="false" locally in a <jaxb:property> customization. How can I do that in a bindings file? The only examples I can find (like this) seem to be setting it directly in the xsd -- and if I could change that, I'd just remove the minOccurs.

根据此页面,应该可以在 customization中本地设置generateElementProperty =“false”。我怎么能在绑定文件中这样做?我能找到的唯一例子(像这样)似乎是直接在xsd中设置它 - 如果我可以改变它,我只需删除minOccurs。

My current bindings file looks like this:

我当前的绑定文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<jxb:bindings version="1.0" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema" jxb:extensionBindingPrefixes="xjc" xmlns:jxb="http://java.sun.com/xml/ns/jaxb">
  <jxb:bindings schemaLocation="../XXXPrivateService.xsd" node="/xs:schema">
    <jxb:globalBindings>
      <xjc:noValidator />
      <xjc:serializable />
    </jxb:globalBindings>
    <jxb:schemaBindings>
      <jxb:package name="com.XXX.YYYY.xsd.XXXprivateservice">
        <jxb:javadoc><![CDATA[<body>TODO</body>]]></jxb:javadoc>
      </jxb:package>
    </jxb:schemaBindings>
  </jxb:bindings>

  <jxb:bindings schemaLocation="../XXXPrivateService_1_2.xsd" node="/xs:schema">
    <jxb:schemaBindings>
      <jxb:package name="com.XXX.YYYY.xsd.XXXprivateservice.two">
        <jxb:javadoc><![CDATA[<body>TODO</body>]]></jxb:javadoc>
      </jxb:package>
    </jxb:schemaBindings>
  </jxb:bindings>

  <jxb:bindings schemaLocation="../XXXPrivateService_1_2_3.xsd" node="/xs:schema">
    <jxb:schemaBindings>
      <jxb:package name="com.XXX.YYYY.xsd.XXXprivateservice.three">
        <jxb:javadoc><![CDATA[<body>TODO</body>]]></jxb:javadoc>
      </jxb:package>
    </jxb:schemaBindings>
  </jxb:bindings>

    <jxb:bindings schemaLocation="../XXXPrivateService_1_2_3_4.xsd" node="/xs:schema">
    <jxb:schemaBindings>
      <jxb:package name="com.XXX.YYYY.xsd.XXXprivateservice.four">
        <jxb:javadoc><![CDATA[<body>TODO</body>]]></jxb:javadoc>
      </jxb:package>
    </jxb:schemaBindings>
  </jxb:bindings>


  <jxb:bindings schemaLocation="../XXXPrivateService_1_2_3_4_5.xsd" node="/xs:schema">
    <jxb:schemaBindings>
      <jxb:package name="com.XXX.YYYY.xsd.XXXprivateservice.five">
        <jxb:javadoc><![CDATA[<body>TODO</body>]]></jxb:javadoc>
      </jxb:package>
    </jxb:schemaBindings>
  </jxb:bindings>

  <jxb:bindings schemaLocation="../XXXPrivateService_1_2_3_4_5_6.xsd" node="/xs:schema">
    <jxb:schemaBindings>
      <jxb:package name="com.XXX.YYYY.xsd.XXXprivateservice.six">
        <jxb:javadoc><![CDATA[<body>TODO</body>]]></jxb:javadoc>
      </jxb:package>
    </jxb:schemaBindings>
  </jxb:bindings>

  <jxb:bindings schemaLocation="../XXXPrivateService_1_2_3_4_5_6_7.xsd" node="/xs:schema">
    <jxb:schemaBindings>
      <jxb:package name="com.XXX.YYYY.xsd.XXXprivateservice.seven">
        <jxb:javadoc><![CDATA[<body>TODO</body>]]></jxb:javadoc>
      </jxb:package>
    </jxb:schemaBindings>
  </jxb:bindings>

</jxb:bindings>

The offending portion of the XSD looks like this:

XSD的违规部分如下所示:

  <xs:element name="PayWithPointsRedemption">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" name="requestSourceId" type="xs:unsignedInt"/>
        <xs:element minOccurs="0" name="rewardsAccountId" type="xs:unsignedInt"/>
        <xs:element minOccurs="0" name="XXXReferenceId" nillable="true" type="xs:string"/>
        <xs:element minOccurs="0" name="externalChannelCode" nillable="true" type="xs:string"/>
        <xs:element minOccurs="0" name="payWithPointsRedemptionDetails" nillable="true" type="q4:ArrayOfPayWithPointsRedemptionDetail" xmlns:q4="http://ZZZZZ.YYYY.com/XXXRewards"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

1 个解决方案

#1


3  

You should be able to set it locally in your external binding like this

您应该能够像这样在外部绑定中本地设置它

<jxb:bindings schemaLocation="xyz.xsd"
    node="/xs:schema">
    <jxb:schemaBindings>
        <!-- ... -->
    </jxb:schemaBindings>
    <jxb:bindings
        node="/xs:schema/xs:element[@name='PayWithPointsRedemption']/xs:complexType/xs:sequence">
        <jxb:property generateElementProperty="false" />
    </jxb:bindings>
</jxb:bindings>

Make sure the XPath expression in jxb:bindings matches!

确保jxb:bindings中的XPath表达式匹配!

#1


3  

You should be able to set it locally in your external binding like this

您应该能够像这样在外部绑定中本地设置它

<jxb:bindings schemaLocation="xyz.xsd"
    node="/xs:schema">
    <jxb:schemaBindings>
        <!-- ... -->
    </jxb:schemaBindings>
    <jxb:bindings
        node="/xs:schema/xs:element[@name='PayWithPointsRedemption']/xs:complexType/xs:sequence">
        <jxb:property generateElementProperty="false" />
    </jxb:bindings>
</jxb:bindings>

Make sure the XPath expression in jxb:bindings matches!

确保jxb:bindings中的XPath表达式匹配!