Search code examples
javaxmljaxb2xjc

How to influence class generation of group declarations with XJC


We have the following problem. We try to generate Java code from XSD files that contain group declarations and multiple group references to these group declarations. Here a simplified version:

<xs:group name="Information">
  <xs:sequence>
    <xs:element name="Name">
      <xs:complexType>
        <xs:simpleContent>
          <xs:extension base="xs:string">
            <xs:attribute name="updated" type="xs:boolean"/>
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
    </xs:element>
 </xs:sequence>
</xs:group>

<xs:element name="Address">
  <xs:complexType>
    <xs:sequence>
      <xs:group ref="Information" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:element name="Customer">
  <xs:complexType>
    <xs:sequence>
       <xs:group ref="Information"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

The main points are:

  • Each of these elements is defined in its own file
  • The same group Information is references twice
  • There is no separate type for the group
  • There is also no separate type for the Name element inside the group
  • We cannot change the XSD file

The problem we face now is that xjc generates non-deterministically depending on the machine where we build, sometimes the type Address.Name and sometimes the type Customer.Name, because the Name element is a complex type and requires a type.

Is there any way to tell xjc to always generate the same type?


Solution

    • Customize the anonymous complex type with the jaxb:class binding and specify the class name.
    • Use <jaxb:globalBindings localScoping="toplevel"/> to generate inner classes on the top level instead.

    A combination of these two will give you a predictable class.