jaxb-tools
jaxb-tools copied to clipboard
Duplicate class name in the same XSD
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
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.
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)
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
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.
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 ?
@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
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!
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.