Geoffrey
Geoffrey

Reputation: 1241

JAXB: package.package.<Type> is alrready define in package.package.<Type>.<Type>

I am trying to generate java classes from XSDs with JAXB.

The XSDs are official xsds in my company so I cannot modify them just for my project. After hundreads of try and search on thi site I decided to ask the question directly.

I have this in my XSD:

<xs:element name="Line" minOccurs="0" maxOccurs="999">
                                                    <xs:complexType>
                                                        <xs:sequence>
                                                            <xs:element name="Line" minOccurs="0" maxOccurs="999">
                                                                <xs:complexType>
                                                                    <xs:attribute name="reference" type="xs:toto" use="optional">
                                                                    </xs:attribute>
                                                                </xs:complexType>
                                                            </xs:element>
                                                        </xs:sequence>
                                                        <xs:attribute name="reference" type="xs:toto" use="optional">

                                                        </xs:attribute>
                                                    </xs:complexType>
                                                </xs:element>

It result in issue: package.Index.Line is already defined in package.Index

After looking at the generated code I can see that we have a static iner Class Line into another static inner class Line (under another Index)

I get error on the Line (at the top bottom) The nested type Line cannot hide an enclosing type

If the name change (either on xsd or on java class after generation) I don' t have issue anymore.

I tried to create a binding file:

<jaxb:bindings schemaLocation="file:../path/myschema.xsd" node="//xs:element[@name='Index']">
    <jaxb:bindings node=".//xs:element[@name='Line']" multiple="true">
        <jaxb:property name="Lines"/>
    </jaxb:bindings>
</jaxb:bindings>

I have the same issue... Because in fact under Index I have two nodes Line so I try with this:

<jaxb:bindings schemaLocation="file:path/myxsd.xsd" node="//xs:element[@name='Index']/xs:element[@name='Line']">
    <jaxb:bindings node="//xs:element[@name='Line']" multiple="true">
        <jaxb:property name="Lines"/>
    </jaxb:bindings>
</jaxb:bindings>

or

<jaxb:bindings schemaLocation="file:path/myxsd.xsd" node="//xs:element[@name='Index']">
    <jaxb:bindings node="//xs:element[@name='Line']/xs:element[@name='Line']" multiple="true">
        <jaxb:property name="Lines"/>
    </jaxb:bindings>
</jaxb:bindings>

But I get an generation issue: com.sun.istack.SAXParseException2: XPath evaluation of "//xs:element[@name='Index']/xs:element[@name='Line']" results in empty target node

I am completely lost. I am eating nutella all day long to compensate the sadness I feel T_T

If you have suggestions ;)

Thanks in advance

Geoffrey

Upvotes: 1

Views: 166

Answers (1)

ulab
ulab

Reputation: 1089

First configure bindings to generate them as individual classes.

<jaxb:globalBindings localScoping="toplevel"/>

Then, use complete XPath for the referenced elements like below.

<bindings version="2.1" xmlns="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <globalBindings localScoping="toplevel"/>

    <bindings schemaLocation="yourschema.xsd">
            <bindings node="//xs:element[@name='Index']/xs:complexType/xs:sequence/xs:element[@name='Line']/xs:complexType">
                <class name="Lines1"/>
            </bindings>
    </bindings>

        <bindings schemaLocation="yourschema.xsd">
                <bindings node="//xs:element[@name='Index']/xs:complexType/xs:sequence/xs:element[@name='Line']/xs:complexType/xs:sequence/xs:element[@name='Line']/xs:complexType">
                    <class name="Lines2"/>
                </bindings>
    </bindings>

</bindings>

Upvotes: 0

Related Questions