【发布时间】:2016-07-11 13:25:08
【问题描述】:
我的 JAXB 代有问题。我有两个 XSD(都在同一个层次结构中),它们的模式定义非常相似:
A.xsd
<xs:schema>
<xs:element name="A">
<xs:complexType>
<xs:sequence>
<xs:element name="CacheInfo">
<xs:complexType>
<xs:complexContent>
<xs:extension base="CacheType">
<xs:sequence ... />
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element> <!--CacheInfo -->
</xs:sequence>
</xs:complexType>
</xs:element> <!-- A -->
<xs:complexType name="CacheType" ... />
<xs:complexType name="TimeType" ... />
</xs:schema>
B.xsd
<xs:schema>
<xs:element name="B">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element name="CacheInfo" type="CacheType">
</xs:element> <!--CacheInfo -->
<xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element> <!-- B -->
<xs:complexType name="CacheType" ... />
<xs:complexType name="TimeType" ... />
</xs:schema>
这两个 XSD 中 CacheType 的结构是不同的。只是名称相同。
我现在的问题是,当我尝试生成代码时,我得到了这个错误: [错误] 文件:A.xsd [95,38] org.xml.sax.SAXParseException;系统ID:文件:A.xsd;行号:95;列号:38; 'CacheType' 已定义(TimeType 的问题相同)
当我删除其中一个文件时,生成正常。我无法编辑 XSD,因此我需要一个绑定文件来重命名这两种特殊情况的类型:
<bindings schemaLocation="../xsd/A.xsd" node="//xs:complexType[@name='CacheType']">
<class name="ACacheType" />
</bindings>
<bindings schemaLocation="../xsd/B.xsd" node="//xs:complexType[@name='CacheType']">
<class name="BCacheType" />
</bindings>
但这不起作用。 当我尝试将类型绑定到属性时,它也不起作用(我最终会遇到同样的错误):
<bindings schemaLocation="../xsd/A.xsd">
<bindings node="//xs:complexType[@name='CacheType']">
<property name="ACacheType" />
</bindings>
<bindings node=".//xs:complexType[@name='TimeType']">
<property name="ATimeType" />
</bindings>
</bindings>
<bindings schemaLocation="../xsd/B.xsd">
<bindings node="//xs:complexType[@name='CacheType']">
<property name="BCacheType" />
</bindings>
<bindings node=".//xs:complexType[@name='TimeType']">
<class name="BTimeType" />
</bindings>
</bindings>
有什么我看不到的吗?为什么我不能使用这些绑定生成这两个 XSD? 为了完成这个,这里是我的 pom.xml sn-p:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>generate-htng-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
</plugin>
【问题讨论】:
-
可能检查明显:绑定文件是否位于目录
/src/main/xjb中?该插件在该目录中查找绑定文件。如果它们位于不同的位置,则插件需要显式配置。请参阅plugin documentation 中的示例 6。