1

我正在尝试使用 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>
4

1 回答 1

1

从您发布的 XML 模式片段中很难判断错误来自何处。我把它冲掉了一点,并从中成功地产生了一代人:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org/schema" xmlns="http://www.example.org/schema"
    elementFormDefault="qualified">

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

    <xs:simpleType name="StringLength1to128">
        <xs:restriction base="xs:string" />
    </xs:simpleType>

    <xs:complexType name="QueryType" />

    <xs:simpleType name="Type">
        <xs:restriction base="xs:string" />
    </xs:simpleType>

</xs:schema>

JAXB 实现不会为属性组生成属性,因此您定义的绑定文件将不起作用。我在下面包含了一个小示例,演示了如何处理属性组。

XML 模式

下面是一个示例 XML 模式,其中包含两个引用相同属性组的复杂类型。

架构.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/schema" 
    xmlns:tns="http://www.example.org/schema" 
    elementFormDefault="qualified">

    <attributeGroup name="my-attribute-group">
        <attribute name="att1" type="string"/>
        <attribute name="att2" type="int"/>
    </attributeGroup>

    <complexType name="foo">
        <attributeGroup ref="tns:my-attribute-group"/>
        <attribute name="foo-att" type="string"/>
    </complexType>

    <complexType name="bar">
        <attributeGroup ref="tns:my-attribute-group"/>
        <attribute name="bar-att" type="string"/>
    </complexType>

</schema>

生成模型

在下面生成的类中,我们看到属性组中的属性与复杂类型中定义的属性的处理方式相同。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "foo")
public class Foo {

    @XmlAttribute(name = "foo-att")
    protected String fooAtt;
    @XmlAttribute(name = "att1")
    protected String att1;
    @XmlAttribute(name = "att2")
    protected Integer att2;

}

酒吧

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "bar")
public class Bar {

    @XmlAttribute(name = "bar-att")
    protected String barAtt;
    @XmlAttribute(name = "att1")
    protected String att1;
    @XmlAttribute(name = "att2")
    protected Integer att2;

}
于 2013-07-30T13:47:40.990 回答