jaxb-tools icon indicating copy to clipboard operation
jaxb-tools copied to clipboard

Duplicate class name in the same XSD

Open nazrul074 opened this issue 5 years ago • 8 comments

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 <execution> <id>add-source-for_response</id> <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 : <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:include schemaLocation="Errors.xsd"/> <xs:include schemaLocation="StatusCode.xsd"/> <xs:include schemaLocation="Reference.xsd"/> <xs:element name="Response"> <xs:complexType> <xs:all> **<xs:element name="ResponseBody">** <xs:complexType> <xs:all> <xs:element name="ResponseData" minOccurs="0"> <xs:complexType> <xs:sequence> <xs:element name="Purchase"> <xs:complexType> <xs:sequence> ................... **<xs:element name="ResponseBody">** <xs:complexType> .................. </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:element ref="Reference"/> </xs:all> </xs:complexType> </xs:element> <xs:element ref="Errors" minOccurs="0"/> <xs:element ref="StatusCode"/> </xs:all> </xs:complexType> </xs:element> </xs: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

nazrul074 avatar Jan 08 '20 14:01 nazrul074

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.

highsource avatar Jan 08 '20 15:01 highsource

Hi @highsource,

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

POM plugins change: <bindingDirectory>src/main/resources/</bindingDirectory> <bindingIncludes>bindings/abcdBindings.xml</bindingIncludes> 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"> <jaxb:bindings schemaLocation="../responseXSD/abcdResponse.xsd" node="/xs:schema"> <jaxb:bindings node="//xs:complexType[@name='Response']"> <jaxb:bindings node=".//xs:complexType[@name='ResponseBody']"> <jaxb:class name="ResponseResponseBody"> </jaxb:class> </jaxb:bindings> </jaxb:bindings> </jaxb:bindings> </jaxb:bindings> 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)

nazrul074 avatar Jan 08 '20 16:01 nazrul074

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

laurentschoelens avatar Aug 28 '23 03:08 laurentschoelens

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.

ghost avatar Feb 08 '24 15:02 ghost

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 avatar Feb 08 '24 16:02 laurentschoelens

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

  • v2 is for jaxb 2.3
  • v3 is for jaxb 3
  • v4 is for jaxb 4 with jdk 11 baseline

laurentschoelens avatar Feb 08 '24 17:02 laurentschoelens

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!

ghost avatar Feb 11 '24 22:02 ghost

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.

laurentschoelens avatar Feb 12 '24 05:02 laurentschoelens