我正在使用一组由第三方编写的模式描述符文件。我需要为它们生成 JAXB 存根。每个 XSD 都定义了不同的消息类型,以及许多支持的简单和复杂类型。许多类型对于每个 XSD 都是通用的,但作者没有将它们分解到单独的 XSD 中,而是选择在每个命名空间中定义它们。当我尝试使用 xjc 将 XSD 编译到单个包中时,这会产生命名空间冲突的问题。我被迫将它们分成独特的包。问题是这使得它们在应该的时候不可互换。我必须做很多额外的转换才能在不同的消息类型中使用来自一种消息类型的数据。
我的问题:有什么方法(绑定自定义?)我可以指示 xjc 为每个共享类型使用一个 java 类,而不是分布在不同包中的唯一类?
这是一个简化的例子。我有两个 XSD,一个用于插入消息,另一个用于响应消息。响应旨在引用插入消息。
<!-- insert.xsd -->
<xs:schema
xmlns="msg.insert"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="msg.insert">
<xs:element name="Message" type="Message" />
<xs:complexType name="Message">
<xs:sequence>
<xs:element
maxOccurs="1"
minOccurs="1"
name="MessageId"
type="Identifier" />
<xs:element
maxOccurs="1"
minOccurs="1"
name="SequenceId"
type="Identifier" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Identifier">
<xs:sequence>
<xs:element
maxOccurs="1"
minOccurs="1"
name="ID"
type="xs:string" />
<xs:element
maxOccurs="1"
minOccurs="1"
name="Created"
type="xs:dateTime" />
</xs:sequence>
</xs:complexType>
</xs:schema>
这是第二个 XSD...
<!-- response.xsd -->
<xs:schema
xmlns="msg.response"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="msg.response">
<xs:element name="Message" type="Message" />
<xs:complexType name="Message">
<xs:sequence>
<xs:element
maxOccurs="1"
minOccurs="1"
name="MessageId"
type="Identifier" />
<xs:element
maxOccurs="1"
minOccurs="1"
name="SequenceId"
type="Identifier" />
<xs:element
maxOccurs="1"
minOccurs="1"
name="ReferenceId"
type="Identifier" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Identifier">
<xs:sequence>
<xs:element
maxOccurs="1"
minOccurs="1"
name="ID"
type="xs:string" />
<xs:element
maxOccurs="1"
minOccurs="1"
name="Created"
type="xs:dateTime" />
</xs:sequence>
</xs:complexType>
请注意Identifier
,两种模式中的复杂类型是相同的。它可以而且应该在消息类型之间互换。但是当我这样运行 xjc 时......
xjc -d java -p example.msg insert.xsd response.xsd
...我在 Message、Identifier 和 ObjectFactory 类上遇到如下冲突。
[ERROR] A class/interface with the same name "example.msg.Message" is already in use. Use a class customization to resolve this conflict.
line 8 of insert.xsd
[ERROR] (Relevant to above error) another "Message" is generated from here.
line 8 of response.xsd
[ERROR] A class/interface with the same name "example.msg.Identifier" is already in use. Use a class customization to resolve this conflict.
line 15 of insert.xsd
[ERROR] (Relevant to above error) another "Identifier" is generated from here.
line 16 of response.xsd
[ERROR] Two declarations cause a collision in the ObjectFactory class.
line 8 of insert.xsd
[ERROR] (Related to above error) This is the other declaration.
line 8 of response.xsd
我完全理解 xjc 抱怨的原因,我试图找到一种方法来哄 xjc 使用 Identifier 类型的公共类,以及解决 ObjectFactory 类中的冲突。一种解决方案是将常用类型分解到单独的命名空间中,并从每个消息类型的 XSD 中引用它们,但如上所述,这些都是由第三方编写的,我无法更改它们。
现在我将每个 XSD 编译成一个单独的 java 包。这可行,但非常非常麻烦。
错误输出表明有一种方法可以通过绑定自定义来做到这一点,但到目前为止我还没有弄清楚如何做到这一点。谁能指出我正确的方向?