Compile multiple XSDs containing duplicate definitions of the same element with JAXB

Question: How to get xjc / Jaxb to generate javaclass for propper for several schemes containing duplicate definitions of elements in one namespace?

Information: I have three .xsd schemes: A, B and C. They all have the same namespace. These are all 3 shemas that were provided to me, and in no way can I change them in any way.

They have some elements that are also in B or C (but A also has many self-declared elements) Example: this is the same β€œcode” for A and C:

<xs:simpleType name="y_ym_ymdDatoType"> <xs:union memberTypes="arcgYearType arcgYearMonthType arcDateType"/> </xs:simpleType> <xs:simpleType name="arcgYearType"> <xs:restriction base="xs:gYear"> <xs:minInclusive value="1700"/> <xs:maxInclusive value="2100"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="arcgYearMonthType"> <xs:restriction base="xs:gYearMonth"> <xs:minInclusive value="1700-01"/> <xs:maxInclusive value="2100-12"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="arcDateType"> <xs:restriction base="xs:date"> <xs:minInclusive value="1700-01-01"/> <xs:maxInclusive value="2100-12-31"/> </xs:restriction> </xs:simpleType> 

When using xjc to compile them in javaclasses, I get the following exception:

 [ERROR] 'y_ym_ymdDatoType' is already defined line 297 of file:../c.xsd [ERROR] (related to above error) the first definition appears here line 309 of file:../a.xsd 

and the same thing happens with other elements: arcgYearType, arcgYearMonthType and arcDateType.

I read about a binding file that might possibly solve this problem, but I'm not sure how to do this, so examples will be very preferred.

+8
java xsd jaxb xjc
source share
2 answers

You can resolve conflicts manually using the binding file. Here is an example where you can specify your name for conflicting names:

 <bindings schemaLocation="../party.xsd" version="1.0" node="/xs:schema"> <bindings node="//xs:complexType[@name='FixedIncomeBook']"> <class name="PartyFixedIncomeBook"/> </bindings> </bindings> 
0
source share

From what you described, I assume that there is no connection between the XSD files. Also, I have to assume that you are trying to reuse classes where content overlaps.

The easy way out is to β€œcompile” each file independently and provide a different Java package for each of the XSD files. The problem is that if you try to β€œmerge” content from one XML to another (that is, unmarshall from A and then marshall to B), then class C1 in package com.A and class C1 in package com.B, then as coinciding with the same XSD complex, are not "interchangeable"; you will have to develop a conversion between them. Do you need a special binding file or if you are using NetBeans, just install different packages in the JAXB wizard.

A better way would be to use episodes (see this on SO ). A.xsd process, then use this episode to process B.xsd, etc.

+2
source share

All Articles