I'm using JAXB and intellij webservices plugin to create java files from XSDs. I have two XSDs that defining the same object but when I create them using the "generate java code from XML schema" the object is created twice with his own package. I already tried with import xsd and using the ref attribute and I get the same result.
Here is an example:
This is the first XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.msp-gs.com/workflow"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:wc="http://www.example.com/workflow"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
jaxb:version="1.0">
<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings enableJavaNamingConventions="true">
</jaxb:globalBindings>
</xs:appinfo>
</xs:annotation>
<xs:element name="WC">
<xs:complexType>
<xs:sequence>
<xs:element name="Example"
type="wc:Restriction"
minOccurs="1"
maxOccurs="unbounded">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Restriction">
<xs:attribute type="xs:string"
name="authorizationTreeId"/>
</xs:complexType>
</xs:schema>
This is the second XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.msp-gs.com/workflow"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:fd="http://www.example.com/workflow"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
jaxb:version="1.0">
<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings enableJavaNamingConventions="true">
</jaxb:globalBindings>
</xs:appinfo>
</xs:annotation>
<xs:element name="FD">
<xs:complexType>
<xs:sequence>
<xs:element name="Example"
type="fd:Restriction"
minOccurs="1"
maxOccurs="unbounded">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Restriction">
<xs:attribute type="xs:string"
name="authorizationTreeId"/>
</xs:complexType>
</xs:schema>
I want that Restriction will be the same object.
Thank you.
You can tell JAXB to use an existing Java class instead of generating one using an external binding file like the following. In the example below we are telling JAXB to use the existing Product
class:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bindings version="2.1" xmlns="http://java.sun.com/xml/ns/jaxb">
<bindings scd="x-schema::tns"
xmlns:tns="http://www.example.org/Product">
<schemaBindings map="false"/>
<bindings scd="tns:product">
<class ref="org.example.product.Product"/>
</bindings>
</bindings>
</bindings>
If you are using the XJC tool to generate classes from an XML schema, you can use the -episode
flag to have XJC generate a binding file pointing to all the classes that it generated. This will allow you to reuse previously generated classes.
xjc -d out -episode product.episode Product.xsd
For More Information
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With