12

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.

Abel
  • 52,738
  • 19
  • 137
  • 227
jdelagorce
  • 171
  • 9
  • Try to change `@id='Contains'` in `@name'Contains'` and add `//xs` instead of `/xs` – Xstian Sep 22 '15 at 12:37
  • Thank you for your answer, I have edited my post, `@id='Contains'` was another test, but I've yet tried with `@name='Contains'`, and I get the same results. If I add `//xs` instead of `/xs` xjc return : `[ERROR] L'évaluation XPath de "//xs:element[@name='ArchiveTransferRequest']//xs:complexType//xs:sequence//xs:element[@name='Contains']//xs:complexType//xs:sequence//xs:element[@name='Contains']" génère un trop grand nombre de noeuds cible (2) ligne 6 sur file:/C:/Users/jtvervliet/Documents/SAE/seda_actes_binding.xml`. Ihink that it don't know precisely which node I want to override. – jdelagorce Sep 22 '15 at 13:17
  • Try to see [this my answer](http://stackoverflow.com/a/29969915/3364187) may be is usefull – Xstian Sep 22 '15 at 13:25
  • Thank you again. I have tried your solution: I created a new binding.xml : ` ` With just an example for the second Contains node. If I tried with `//` I get this error : `[ERROR] L'évaluation XPath de "..." génère un trop grand nombre de noeuds cible (2) ligne 6 sur file :...` – jdelagorce Sep 22 '15 at 14:35
  • And if I try `/`I get this error: `Exception in thread "main" java.lang.IllegalArgumentException: Illegal class inheritance loop. Outer class SecondContains may not subclass from inner class: SecondContains` – jdelagorce Sep 22 '15 at 14:43

2 Answers2

20

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>
Abel
  • 52,738
  • 19
  • 137
  • 227
6

Though it's an old question but just to help others make sure to add the "/xs:complexType" at the end. So instead of below:

<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>

It should be as:

<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>
alditis
  • 3,983
  • 3
  • 37
  • 68
suraj bahl
  • 2,195
  • 4
  • 24
  • 37