10

Question: How do i make xjc/Jaxb generate the propper javaclasses for several schemas containing duplicate elementdefinitions in the same namespace?

Information: I have three .xsd schemas: A,B and C. All have the same targetnamespace. They are all 3 shemas that has been given to me, and i am not, in any way possible, allowed to change them in any way.

They A has some elements that is also found in B or C (but A has a lot of self declared elements as well) 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 into 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 happens to the other elements: arcgYearType, arcgYearMonthType and arcDateType.

I have read about a binding file that maybe could solve this problem, but i am not sure on how to do it so examples will be highly preferred.

Sofus Albertsen
  • 163
  • 1
  • 1
  • 11

2 Answers2

2

From what you describe, I assume that there is no include relationship between the XSD files. Also, I have to assume that you're 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 here is that if you try to "chain" together content from one XML to another (i.e. unmarshall from A and then marshall to B), then class C1 in package com.A, and class C1 in package com.B, while matching the same XSD complex type, are not "interchangeable"; you will have to develop a conversion between them. You need a custom binding file or if you use NetBeans, simply set different packages in the JAXB wizard.

The best way might be to use episodes (see this on SO). Process A.xsd, then use that episode to process B.xsd, etc.

Community
  • 1
  • 1
Petru Gardea
  • 21,373
  • 2
  • 50
  • 62
  • 1. No there is no include. 2. Yes i want to reuse, as they are the same, but that is not the primary goal, so if doing something else is easier then so be it. I am not interested in something from A.xsd in C.xsd, i just use them all in the same project. Is there a straight forward way to make the packages defer even though their xsd's come from the same TNS? – Sofus Albertsen Mar 13 '12 at 14:17
  • @Sofus, then why are you not using episodes? Or run xjc against each package using a custom binding targetting different namespaces? – Petru Gardea Mar 13 '12 at 15:48
  • I went with compiling each of the XSD's into it's own package. Yet again, thankyou! – Sofus Albertsen Mar 14 '12 at 09:27
1

You can resolve conflicts manually using binding file. Here is the example, where you can specify your custom 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>
Sergey Aslanov
  • 2,397
  • 15
  • 16
  • Is this not acually renaming the XML elements when they come out, or is it just renaming the javaclasses that is generated? – Sofus Albertsen Mar 13 '12 at 14:15
  • It's renaming generated java class. By default you get `FixedIncomeBook.java` and with this binding you get `PartyFixedIncomeBook.java` – Sergey Aslanov Mar 13 '12 at 14:21
  • i get: [ERROR] Unsupported binding namespace "". Perhaps you meant "http://java.sun.com/xml/ns/jaxb/xjc"? line 4 of file:/../C.xsd The line #4 is schema definitions: Can you se what's wrong? – Sofus Albertsen Mar 13 '12 at 15:05
  • I solved the problem described above by giving all element a jxb: prefix on. But i still cannot get the original error to go away. So even though i still consider this answer as the right one, it hasn't solved my problem. – Sofus Albertsen Mar 13 '12 at 15:40
  • Sorry, @SofusAlbertsen, I forgot to specify namespace, you're right. As for the problem, I found that it's probably not related to JAXB, but in general because of wrong XSD set. Please check this: http://stackoverflow.com/questions/2714588/resolve-naming-conflict-in-included-xsds-for-jaxb-compilation – Sergey Aslanov Mar 14 '12 at 07:05
  • I went with compiling each of the XSD's into it's own package, just like the question you related to. Thanks again for your help. – Sofus Albertsen Mar 14 '12 at 09:28