绑定嵌套 classes 生成一个新的嵌套 class 称为 'Type',如何防止这种情况

Binding nested classes generating a new nested class called 'Type', how can this be prevented

我有一个 XSD(我无法更改),其中包含在 complexType 的序列元素中定义的这些元素:

<xsd:element minOccurs="0" name="precision" nillable="true">
      <xsd:complexType>
                   ...
      </xsd:complexType>
 </xsd:element>
 <xsd:element minOccurs="0" name="Precision" nillable="true">
      <xsd:complexType>
                    ...
      </xsd:complexType>
 </xsd:element>          

当我对架构进行 运行 xjc 时,由于 ObjectFactory 和嵌套的 classes.

中的名称冲突出现异常

为了尝试解决这个问题,我添加了这个外部绑定,用于将字段映射到唯一名称并将 classes 映射到唯一名称:

<jaxb:bindings node="//*[local-name()='element' and @name='precision']" schemaLocation="schema.xsd">
    <jaxb:property name="precision1"/>
    <jaxb:class name="precision1"/>
</jaxb:bindings>
<jaxb:bindings node="//*[local-name()='element' and @name='Precision']" schemaLocation="schema.xsd">
    <jaxb:property name="Precision2"/>
    <jaxb:class name="Precision2"/>
</jaxb:bindings>

这启用了 JAXB classes 的生成,但是 classes 是这样生成的:

 @XmlElementRef(name = "precision1", namespace = "namespace.CustomerInvoice2", type = JAXBElement.class, required = false)
protected JAXBElement<CustomerOrderDate.Precision1> precision1;

    protected final static QName NAME = new QName("namespace.CustomerInvoice3", "precision");

    public Precision11(CustomerOrderDate.Precision11 .Type value) {
        super(NAME, ((Class) CustomerOrderDate.Precision11 .Type.class), CustomerOrderDate.class, value);
    }

    public Precision11() {
        super(NAME, ((Class) CustomerOrderDate.Precision11 .Type.class), CustomerOrderDate.class, null);
    }


    /**
     * whole bunch of comments...
     */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "precision",
        "use"
    })
    public static class Type {
         //The actual fields

但是,如果我手动将字段名称更改为 precision1 和 Precision2,则 classes 生成为:

 public static class Precision1 {

    @XmlElementRef(name = "precision", namespace = "namespace.CustomerInvoice2", type = JAXBElement.class, required = false)
    protected JAXBElement<Integer> precision;
    @XmlElementRef(name = "use", namespace = "namespace.CustomerInvoice2", type = JAXBElement.class, required = false)
    protected JAXBElement<String> use;
    @XmlAttribute(name = "mark")
    protected String mark;
    @XmlAttribute(name = "mode")
    protected String mode;
    @XmlAttribute(name = "invalid")
    protected Boolean invalid;
    // blah, blah, blah...

我很好奇是否有我可以使用的绑定将 QName 值保留在根 class 的字段声明中并将类型信息保留在生成的 class 中。

基于this answer and the tutorial JAXB - Java Architecture for XML Binding.

您只需要:

<jaxb:bindings node="//xs:element[@name='precision']/xs:complexType" schemaLocation="schema.xsd">
    <jaxb:class name="Precision1"/>
</jaxb:bindings>
<jaxb:bindings node="//xs:element[@name='Precision']/xs:complexType" schemaLocation="schema.xsd">
    <jaxb:class name="Precision2"/>
</jaxb:bindings>