Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile several XSD's containing duplicate definitions of the same element with JAXB

Tags:

java

jaxb

xsd

xjc

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.

like image 538
Sofus Albertsen Avatar asked Mar 13 '12 12:03

Sofus Albertsen


People also ask

What is XJB file in JAXB?

Generally we create a bindings file with . xjb extension to resolve any conflicts in the WSDL or schema. For example if two elements have the same name and you want to distinguish between them you can rename one by specifying it the bindings file.

How do I use JAXB XJC?

Open a command prompt. Run the JAXB schema compiler, xjc command from the directory where the schema file is located. The xjc schema compiler tool is located in the app_server_root \bin\ directory. Use the generated JAXB objects within a Java application to manipulate XML content through the generated JAXB classes.

What is XJC in Java?

XJC (XML to Java Compiler) is a utility for generating those objects from an XML schema. It is included as part of the JDK since Java SE 6.

Which of the following in Java language is similar to a XML schema file?

DTDs are also known as XML Markup Declarations. XML Schema language serves a similar purpose to DTDs, but it is more flexible in specifying XML document constraints and potentially more useful for certain applications.


1 Answers

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.

like image 147
Petru Gardea Avatar answered Sep 28 '22 02:09

Petru Gardea