Java 使用 JAXB 编译多个包含相同元素重复定义的 XSD
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9683907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compile several XSD's containing duplicate definitions of the same element with JAXB
提问by Sofus Albertsen
Question:How do i make xjc/Jaxb generate the propper javaclasses for several schemas containing duplicate elementdefinitions in the same namespace?
问题:如何让 xjc/Jaxb 为在同一命名空间中包含重复元素定义的多个模式生成正确的 javaclasses?
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.
信息:我有三个 .xsd 架构:A、B 和 C。都具有相同的目标命名空间。它们都是给我的 3 个 shemas,我不能以任何可能的方式改变它们。
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:
它们 A 有一些也在 B 或 C 中找到的元素(但 A 也有很多自我声明的元素) 示例:这是 A 和 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:
使用 xjc 将它们编译为 javaclass 时,出现以下异常:
[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.
其他元素也是如此:arcgYearType、arcgYearMonthType 和 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.
我已经阅读了一个可能可以解决这个问题的绑定文件,但我不确定如何去做,因此示例将是非常受欢迎的。
采纳答案by Sergey Aslanov
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>
回答by Petru Gardea
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.
根据您的描述,我假设 XSD 文件之间没有包含关系。此外,我必须假设您正在尝试重用内容重叠的类。
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.
简单的方法是独立“编译”每个文件,并为每个 XSD 文件提供不同的 Java 包。这里的问题是,如果您尝试将一个 XML 中的内容“链接”到另一个 XML(即从 A 解组,然后编组到 B),则包 com.A 中的类 C1 和包 com.B 中的类 C1,而匹配相同的 XSD 复杂类型,不可“互换”;您将不得不在它们之间进行转换。您需要一个自定义绑定文件,或者如果您使用 NetBeans,只需在 JAXB 向导中设置不同的包。
The best way might be to use episodes (see this on SO). Process A.xsd, then use that episode to process B.xsd, etc.
最好的方法可能是使用剧集(参见SO 上的这个)。处理 A.xsd,然后使用该情节处理 B.xsd 等。