highsource / jaxb-tools

The most advanced JAXB2 Maven Plugin for XML Schema compilation.
Other
432 stars 99 forks source link

Duplicate class name in the same XSD #189

Open nazrul074 opened 4 years ago

nazrul074 commented 4 years ago

Hi, I am trying to generate Java stub from XSDs (received from third party so cannot change that). It is working fine but when XSD has same element name for the higher order element and nested element also then it is creating duplicate class name. Implementation from my POM file `

add-source-for_response
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <forceRegenerate>true</forceRegenerate>
                        <noFileHeader>true</noFileHeader>
                        <schemaDirectory>src/main/resources</schemaDirectory>
                        <schemaIncludes>
                            <include>ResponseXSD/RegisterResponse.xsd</include>
                        </schemaIncludes>
                        <generateDirectory>target/generated-sources/xjc</generateDirectory>
                        <generatePackage>com.abcd.response</generatePackage>
                    </configuration>
                </execution>`

Sample schema : `

**** ................... **** .................. ` All the referenced schema stub generated properly. But when the Response stub is generated **_ResponseBody_** class was generated two times. One is under top level Response object another under Purchase. How can I preserve the hierarchical information? Thanks
highsource commented 4 years ago

You can use bindings to explicitly name generated classes. Name it ResponseResponseBody and PurchaseResponseBody.

Otherwise I'd expect XJC to generate ResponseBody as inner class within Response and Purchase respectively. So the hierarchical information should be retained when generating.

nazrul074 commented 4 years ago

Hi @highsource,

Thanks for the response. I tried to implement your solution for the top level ResponseBody. Receiving an error message:

POM plugins change: `src/main/resources/

bindings/abcdBindings.xml

**abcdBindings.xml** <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance">

` **Error message:** [ERROR] Error while parsing schema(s).Location [ ......... /src/main/resources/bindings/parcelRegisterBindings.xml{7,66}]. com.sun.istack.SAXParseException2: XPath evaluation of "//xs:complexType[@name='Response']" results in empty target node at com.sun.tools.xjc.reader.internalizer.Internalizer.reportError (Internalizer.java:599) at com.sun.tools.xjc.reader.internalizer.Internalizer.reportError (Internalizer.java:593)
laurentschoelens commented 1 year ago

Hi @nazrul074 Hope you finally found a way of renaming your generated code with binding files. If not, please let us know. I think I'll add sample project for this one to show how to configure this if anyone has the same problem one day

ghost commented 9 months ago

I have run into the same problem (I think) when upgrading from 0.14.0 to 4.0.0. I ended up with only upgrading to 2.x because then the old config and xjb for 0.14.0 worked, just a shame something happened during 3.x that seems not to be documented in migration guide.

laurentschoelens commented 9 months ago

Hi @anders-payerl-mmb Could you share code sample please ? If you're using still v2, does that mean that you stick to jaxb 2 api ?

laurentschoelens commented 9 months ago

@anders-payerl-mmb : Just a reminder that is present in migration guide

ghost commented 9 months ago

Strange, I could have sworn that I followed all the steps in the migration 100% last time I tried but this time when I tried to replicate the error it of course worked… So now I'm at 3.0.1 of jaxb-maven-plugin, will try to continue to 4.x tomorrow…

Thanks for an awesome project!

laurentschoelens commented 9 months ago

Good to hear from you that it worked now. If wiki is unclear on some parts, let me know I'll try to make it clearer.