JAXB multiple schemas with element reference

Alexander picture Alexander · Mar 1, 2011 · Viewed 7k times · Source

I have two schemas which are processed using JAXB. The first schema is preprocessed and information of this is used using an episode file (following http://www.java.net/blog/2006/09/05/separate-compilation-jaxb-ri-21). The second schema imports the first, and again using jaxb, is processed. This all works as expected.

But now I have an element in the first schema, which is used in the second using a reference.

Schema a:

<schema elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:test="http://www.example.org/Test/"
targetNamespace="http://www.example.org/Test/">
<element name="type" type="test:MyType"></element>

Schema b:

<schema elementFormDefault="qualified" 
xmlns="http://www.w3.org/2001/XMLSchema" 
xmlns:second="http://www.example.org/Second/"
xmlns:test="http://www.example.org/Test/"
targetNamespace="http://www.example.org/Second/">

<import namespace="http://www.example.org/Test/" />

<complexType name="SomeType">
    <sequence>
        <element ref="test:type" minOccurs="1" maxOccurs="unbounded" />
    </sequence>
</complexType>

During processing nothing is wrong, but the generated code for both schemas provide the same method:

public JAXBElement<EventType> createType(TypeType value)

At runtime, this results in the following error:

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of 
    IllegalAnnotationExceptions
The element name {http://www.example.org/Type/}type has more than one mapping.

How can I prevent JAXB from creating the duplicate createType methods?

Thanks in advance!

Update: I asked this same question on the JAXB mailing list, on that list I also posted a working example. The thread and example can be found at: http://java.net/projects/jaxb/lists/users/archive/2011-03/message/18

On this list I've been suggested a workaround, and now I can use the schemas the way I like. But I still think JAXB should not create the additional "create" method, since it should already be in the episode file.

Answer

Andrew Carr picture Andrew Carr · May 4, 2011

I've written a few Schema Definitions in my day. You are declaring your first xsd in your second schema declaration and then you are importing it.

As per MSDN, when you import an XSD you do not include it in the Schema Declaration. This is where it is in your schema declaration.

xmlns:test="http://www.example.org/Test/"

Remove this and just do the import... ( <xs:import namespace="http://www.example.com/IPO" /> )

see:http://msdn.microsoft.com/en-us/library/ms256480.aspx