Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling multiple schemas into different packages using JAXB 2.1

Tags:

jaxb

I have a CommonTypes.xsd which I'm including in my all other XSDs using xs:include. I get

Multiple <schemaBindings> are defined for the target namespace ""

when I try to compile it into different packages using binding files. Please tell me whether there is a way to compile them into different packages. I'm using jaxb 2.1

like image 633
Priya Avatar asked May 04 '10 11:05

Priya


4 Answers

Yeah, there is a way.
Assuming:

xsd/common/common.xsd
xsd/foo/foo.xsd 

In the common directory place common.xjb:

<jxb:schemaBindings>
    <jxb:package name="mypkg.common">
    </jxb:package>
</jxb:schemaBindings> 

In the foo directory place foo.xjb:

<jxb:schemaBindings>
    <jxb:package name="mypkg.foo">
     </jxb:package>
</jxb:schemaBindings> 

In the build.xml file, create one xjc task for each:

<xjc destdir="${app.src}" readOnly="true" package="mypkg.common">
    <schema dir="${app.schema}/common" includes="*.xsd" />
    <binding dir="${app.schema}/common" includes="*.xjb" />
</xjc>
<xjc destdir="${app.src}" readOnly="true" package="mypkg.foo">
    <schema dir="${app.schema}/foo" includes="*.xsd" />
    <binding dir="${app.schema}/foo" includes="foo.xjb" />
</xjc>

You need to make sure that common.xsd has a targetNameSpace that is different from foo.xsd's targetNameSpace.

like image 180
Shantanu Avatar answered Nov 12 '22 21:11

Shantanu


As stated already by Ben there is no way to do that if they have the same namespace. But how to do it if you do have different namespaces?

<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
    <jxb:bindings namespace="http://www.openapplications.org/oagis/9/unqualifieddatatypes/1.1" schemaLocation="oagi/Common/UNCEFACT/ATG/CoreComponents/UnqualifiedDataTypes.xsd" >
        <jxb:schemaBindings>
            <jxb:package name="com.test.oagi.udt"/>
        </jxb:schemaBindings>
    </jxb:bindings>
    <jxb:bindings namespace="http://www.openapplications.org/oagis/9/codelists" schemaLocation="oagi/Common/OAGi/Components/CodeLists.xsd" >
        <jxb:schemaBindings>
            <jxb:package name="com.test.oagi.cl"/>
        </jxb:schemaBindings>
    </jxb:bindings>
</jxb:bindings>

but be sure you do not use the command line parameter -p, since that will override your config.

like image 27
mmoossen Avatar answered Nov 12 '22 23:11

mmoossen


I've meet the same problem and haven't solve it yet, but I'm afraid that it can't be possible to generate XSD into differents packages :

It is not legal to have more than one <jaxb:schemaBindings> per namespace, so it is impossible to have two schemas in the same target namespace compiled into different Java packages

from Compiler Restrictions at the end of this page

but if some one find some work around, just inform us please

like image 5
LE GALL Benoît Avatar answered Nov 12 '22 22:11

LE GALL Benoît


I know it is an old post, but, as there is no answer for the exact question, here is my proposal:

As mmoossen explained, the trick is to specify different namespaces for the XSDs. But, adding a namespace attribute in the jxb:bindings tag doesn't work:

<jxb:bindings namespace="http://www.openapplications.org/oagis/9/unqualifieddatatypes/1.1" schemaLocation="oagi/Common/UNCEFACT/ATG/CoreComponents/UnqualifiedDataTypes.xsd" >

Instead of that, you need to add a targetNamespace attribute to the xs:schema tags of your XSDs:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified" attributeFormDefault="unqualified"
    targetNamespace="some.namespace"
    version="1.0">

Once done, you will be able to have 1 external customization file (.xjb) declaring different schemaBindings, each of them possibly using a different package:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.1"
               xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
               jaxb:extensionBindingPrefixes="xjc annox inherit">


    <jaxb:bindings schemaLocation="MyFirstXSD.xsd">
        <jaxb:schemaBindings>
            <jaxb:package name="com.test.a" />
        </jaxb:schemaBindings>
    </jaxb:bindings>

    <jaxb:bindings schemaLocation="MySecondXSD.xsd">
        <jaxb:schemaBindings>
            <jaxb:package name="com.test.b" />
        </jaxb:schemaBindings>
    </jaxb:bindings>

    <jaxb:bindings schemaLocation="MyThirdXSD.xsd">
        <jaxb:schemaBindings>
            <jaxb:package name="com.test.c" />
        </jaxb:schemaBindings>
    </jaxb:bindings>

</jaxb:bindings>
like image 4
Ardemius Avatar answered Nov 12 '22 23:11

Ardemius