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

帅比萌擦擦* 提交于 2019-12-13 12:03:27

问题


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.


回答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>



回答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.



来源:https://stackoverflow.com/questions/9683907/compile-several-xsds-containing-duplicate-definitions-of-the-same-element-with

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!