我正在尝试使用 JAXB 基于一些 xsd 文件生成 java 类。我遇到了一个我自己无法解决的问题。我只想强调我不能修改 xsd 文件。所以这就是我遇到的问题:有 2 个 xsd 文件 MultiDestScheduleRQ 和 MultiDestScheduleRS,它们都具有相似的结构:
<xs:attributeGroup name="ResponseGroup">
<xs:annotation>
<xs:documentation source="Description" xml:lang="en">BLA</xs:documentation>
</xs:annotation>
<xs:attribute name="MoreIndicator" type="xs:boolean" use="optional">
<xs:annotation>
<xs:documentation source="Description" xml:lang="en">BLA BLA</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="MoreDataEchoToken" type="StringLength1to128" use="optional">
<xs:annotation>
<xs:documentation source="Description" xml:lang="en">BLA BLA BLA.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:attributeGroup>
<xs:element name="MultiDestScheduleRQ">
<xs:complexType>
<xs:complexContent>
<xs:extension base="QueryType">
<xs:sequence>
<xs:element name="Journeys">
<xs:complexType>
<xs:sequence>
<xs:element name="Journey" maxOccurs="12">
<xs:complexType>
<xs:attribute name="RPH" type="Type" use="required">
<xs:annotation>
<xs:documentation>BLA</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attributeGroup ref="ResponseGroup"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
MultiDestScheduleRS 的唯一区别是元素的名称。当我尝试生成 java 类时,出现错误:
org.xml.sax.SAXParseException: 'ResponseGroup' is already defined
我尝试使用自定义绑定来修复它:
<jxb:bindings schemaLocation="./../Validated/MultiDestScheduleRQ.xsd" >
<jxb:bindings node="//xs:attributeGroup[@name='ResponseGroup']//xs:attributeGroup">
<jxb:property name="ResponseGroupRQ"/>
</jxb:bindings>
</jxb:bindings>
但它只改变了错误信息
com.sun.istack.SAXParseException2: XPath evaluation of "//xs:attributeGroup[@name='ResponseGroup']//xs:attributeGroup" results in empty target node
我也试过
<jxb:bindings schemaLocation="./../Validated/MultiDestScheduleRQ.xsd" >
<jxb:bindings node="//xs:attributeGroup[@name='ResponseGroup']">
<jxb:property name="ResponseGroupRQ"/>
</jxb:bindings>
</jxb:bindings>
只是再次得到第一个错误(那个说'ResponseGroup'已经定义)。
有人可以帮忙吗?
更新 我使用maven插件生成类,它是pom的片段
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.7.4</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<schemaDirectory>./src/main/xsd</schemaDirectory>
<schemaIncludes>
<include>**/*/*.xsd</include>
</schemaIncludes>
<bindingDirectory>./src/main/xsd/541_Grammar_Multidest/xjb</bindingDirectory>
<bindingIncludes>
<include>binding.xml</include>
</bindingIncludes>
<generatePackage>com.my.package</generatePackage>
<extension>true</extension>
<staleFile>${project.build.directory}/jaxb2/.schema2XjcStaleFlag</staleFile>
</configuration>
</plugin>