Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make two XSDs to use the same custom object

Tags:

jaxb

xsd

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.

like image 207
Rotem Avatar asked Sep 02 '25 06:09

Rotem


1 Answers

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

  • http://blog.bdoughan.com/2011/12/reusing-generated-jaxb-classes.html
like image 71
bdoughan Avatar answered Sep 04 '25 23:09

bdoughan