javaxpathxsdjaxbjaxb2

Nested elements in XSD cause Illegal class inheritance loop exception in JAXB, how can I properly override the bindings?


I have got a problem, I'm trying to bind an XML where there are the name 'Contains' three times.

So I read that it's possible to override node names with a binding file. At the moment it doesn't work.

I think mistake come from Xpath in the binding file, but I not sure. I have tried lot of way, but I have never succeed.

There is my XSD File :

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="fr:gouv:ae:archive:draft:standard_echange_v0.2" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1">

  <xs:element name="ArchiveTransferRequest">
    <xs:complexType>
      <xs:sequence>

        <xs:element type="xs:string" name="Comment"/>
        <xs:element type="xs:dateTime" name="Date"/>
        <xs:element name="TransferRequestIdentifier">....</xs:element>
        <xs:element name="TransferringAgency">...</xs:element>
        <xs:element name="ArchivalAgency">...</xs:element>

        <xs:element name="Contains">
          <xs:complexType>
            <xs:sequence>

              <xs:element type="xs:string" name="ArchivalAgencyArchiveIdentifier"/>
              <xs:element type="xs:string" name="ArchivalAgreement"/>
              <xs:element type="xs:string" name="ArchivalProfile"/>
              <xs:element name="DescriptionLanguage">...</xs:element>
              <xs:element name="DescriptionLevel">...</xs:element>
              <xs:element type="xs:string" name="Name"/>
              <xs:element name="ContentDescription">...</xs:element>

              <xs:element name="Contains" id="contains" >
                <xs:complexType>
                  <xs:sequence>

                    <xs:element name="DescriptionLevel">...</xs:element>
                    <xs:element type="xs:string" name="Name"/>
                    <xs:element name="ContentDescription">...<xs:element>

                    <xs:element name="Contains" maxOccurs="unbounded" minOccurs="0">
                      <xs:annotation>
                        <xs:documentation>a new contains for a new  XML</xs:documentation>
                      </xs:annotation>
                      <xs:complexType>
                        <xs:sequence>
                             ...
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

And the binding file to modify the second and the thirs 'Contains' node.

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               version="2.1">
    <jaxb:bindings schemaLocation="seda_actes.xsd">
        <jaxb:bindings node="//xs:element[@name='ArchiveTransferRequest']/xs:complexType/xs:sequence/xs:element[@name='Contains']/xs:complexType/xs:sequence/xs:element[@name='Contains']">
            <jaxb:class name="SecondContains"/>
        </jaxb:bindings>
         <jaxb:bindings node="//xs:element[@name='ArchiveTransferRequest']/xs:complexType/xs:sequence/xs:element[@name='Contains']/xs:complexType/xs:sequence/xs:element[@name='Contains']/xs:complexType/xs:sequence/xs:element[@name='Contains']">
            <jaxb:class name="ThirdContains"/>
        </jaxb:bindings>
    </jaxb:bindings>

</jaxb:bindings>

But when I execute the following xjc command :

xjc -b C:\Users\jtvervliet\Documents\SAE\seda_actes_binding.xml -d C:\Users\jtvervliet\workspace\poc_bordereau\src -p test4.impnat.xml C:\Users\jtvervliet\Documents\SAE\seda_actes.xsd

I have got this error :

analyse dun schéma... compilation dun schéma... Exception in thread "main" java.lang.IllegalArgumentException: Illegal class inheritance loop. Outer class SecondContains may not subclass from inner class: SecondContains at com.sun.codemodel.internal.JDefinedClass._extends(JDefinedClass.java:258) at com.sun.tools.internal.xjc.generator.bean.ImplStructureStrategy$1._extends(ImplStructureStrategy.java:104) at com.sun.tools.internal.xjc.generator.bean.BeanGenerator.(BeanGenerator.java:200) at com.sun.tools.internal.xjc.generator.bean.BeanGenerator.generate(BeanGenerator.java:154) at com.sun.tools.internal.xjc.model.Model.generateCode(Model.java:275) at com.sun.tools.internal.xjc.Driver.run(Driver.java:348) at com.sun.tools.internal.xjc.Driver.run(Driver.java:185) at com.sun.tools.internal.xjc.Driver._main(Driver.java:108) at com.sun.tools.internal.xjc.Driver.access$000(Driver.java:65) at com.sun.tools.internal.xjc.Driver$1.run(Driver.java:88)

Any ideas will be appreciated :)

Thank you.


Solution

  • You wrote:

    <jaxb:bindings node="//xs:element[@name='ArchiveTransferRequest']
        /xs:complexType/xs:sequence/xs:element[@name='Contains']
        /xs:complexType/xs:sequence/xs:element[@name='Contains']">
    

    I took your XSD and fixed it to make it a legal document. Then I took your XPath expressions, they selected the xs:element correctly.

    There's only one more thing you need to do to make this working: select the xs:complexType, not the xs:element, because that is what JAXB uses to create the classes, not the elements.

    The following should work:

    <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
                   xmlns:xs="http://www.w3.org/2001/XMLSchema"
                   version="2.1">
        <jaxb:bindings schemaLocation="seda_actes.xsd">
            <jaxb:bindings node="//xs:element[@name='ArchiveTransferRequest']
                /xs:complexType/xs:sequence/xs:element[@name='Contains']
                /xs:complexType/xs:sequence/xs:element[@name='Contains']
                /xs:complexType">
                <jaxb:class name="SecondContains"/>
            </jaxb:bindings>
             <jaxb:bindings node="//xs:element[@name='ArchiveTransferRequest']
                 /xs:complexType/xs:sequence/xs:element[@name='Contains']
                 /xs:complexType/xs:sequence/xs:element[@name='Contains']
                 /xs:complexType/xs:sequence/xs:element[@name='Contains']
                 /xs:complexType">
                <jaxb:class name="ThirdContains"/>
            </jaxb:bindings>
        </jaxb:bindings>
    
    </jaxb:bindings>