【问题标题】:JAXB xjc: Two declarations cause a collisionJAXB xjc:两个声明导致冲突
【发布时间】:2019-06-23 17:42:33
【问题描述】:

我发现了关于此主题的类似讨论here。但是那里的情况与这个完全不同,解决方案对我不起作用。所以我再次提出这个问题。

我的 XSD (sample.xsd)

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="field">
      <xsd:complexType>
         <xsd:choice maxOccurs="unbounded" minOccurs="0">
            <xsd:element maxOccurs="unbounded" minOccurs="0" name="ProgramLevel">
               <xsd:complexType>
                  <xsd:attribute name="value" type="xsd:string" use="optional"/>
                  <xsd:attribute name="desc" type="xsd:string" use="optional"/>
               </xsd:complexType>
            </xsd:element>
            <xsd:element maxOccurs="unbounded" minOccurs="0" name="Program">
               <xsd:complexType>
                  <xsd:sequence maxOccurs="unbounded" minOccurs="0">
                     <xsd:element maxOccurs="unbounded" minOccurs="0" name="Level">
                        <xsd:complexType>
                           <xsd:attribute name="value" type="xsd:string" use="optional"/>
                           <xsd:attribute name="desc" type="xsd:string" use="optional"/>
                        </xsd:complexType>
                     </xsd:element>
                  </xsd:sequence>
               </xsd:complexType>
            </xsd:element>
         </xsd:choice>
      </xsd:complexType>
    </xsd:element>
</xsd:schema>

我的 XML

<field>
    <ProgramLevel value="x" />
</field>
<field>
    <Program>
        <Level value="y" />
    </Program>
</field>

运行 xjc 命令时出现以下错误

[ERROR] Two declarations cause a collision in the ObjectFactory class.
  line 7 of file:/D:/ProgramPractice/CreateXSD/JAXB/sample.xsd

[ERROR] (Related to above error) This is the other declaration.
  line 16 of file:/D:/ProgramPractice/CreateXSD/JAXB/sample.xsd

Failed to produce code.

知道如何借助绑定文件解决'ProgramLevel''Program-&gt;Level' 之间的冲突吗?提前致谢。

【问题讨论】:

    标签: binding xsd jaxb schema xjc


    【解决方案1】:

    这很简单。 您在 field 元素类型的定义中使用了 choice 作曲家。 但是choice 表示在其中包含的所有粒子中,只有一个可能出现在目标 XML 中。因此,根据您的 sample.xsd 架构,&lt;ProgramLevel&gt;&lt;Program&gt; 可能不会同时作为 &lt;field&gt; 元素的子元素出现。 只能使用其中之一!

    如果两者都需要,则应改用 xsd:sequence composer 并按如下方式修改架构:

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:element name="field">
        <xsd:complexType>
           <xsd:sequence maxOccurs="unbounded" minOccurs="0">
           ...
           </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    

    然后,您的 XML 将通过验证(没有冲突)。

    请注意,xsd:sequence composer 也有其限制。这意味着其中指定的粒子应严格按照与 XML 模式中相同的顺序出现在目标 XML 中。也就是说,以下内容也会出错:&lt;field&gt;&lt;Program&gt;...&lt;/Program&gt;&lt;ProgramLevel/&gt;&lt;/field&gt;。因此,如果需要,您应该相应地修改架构。但是,如果完全不能限制排序,请使用allcomposer(甚至更复杂的构造)。

    附: 对于那些使用 XML 模式和 WSDL 的人来说,以下链接可能会很有趣:

    如果您重视我对本网站的参与,请不要删除这些链接!

    【讨论】:

    • 感谢您的回复。我编辑了 XML,因为只有一种类型的粒子可以在字段标记下。那是之前的错别字。我必须在我的情况下使用选择..
    【解决方案2】:

    通过在绑定文件中添加以下内容最终解决了该问题:

    <jxb:bindings schemaLocation="sample.xsd" node="/xsd:schema">
        <jxb:bindings node="//xsd:element[@name='ProgramLevel']/xsd:complexType">
          <jxb:class name="ProgramLevelInfo"/>
        </jxb:bindings>
    </jxb:bindings>
    

    【讨论】:

      【解决方案3】:

      我通过在 jaxb 插件配置中添加 XautoNameResolution 作为 arg 解决了这个问题。

       <plugin>
                  <groupId>org.jvnet.jaxb2.maven2</groupId>
                  <artifactId>maven-jaxb2-plugin</artifactId>
                  <version>0.14.0</version>
                  <executions>
                      <execution>
                          <id>schema2</id>
                          <goals>
                              <goal>generate</goal>
                          </goals>
                          <configuration>
                              <args>
                                  <arg>-XautoNameResolution</arg>
                              </args>
                              <schemaLanguage>WSDL</schemaLanguage>
                              <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory>
                              <schemaIncludes>
                                  <include>products.wsdl</include>
                              </schemaIncludes>
                              <generatePackage>com.suhas.generated</generatePackage>
                              <generateDirectory>${project.basedir}/src/main/java</generateDirectory>
                              <cleanPackageDirectories>true</cleanPackageDirectories>
                          </configuration>
                      </execution>
                  </executions>
              </plugin>
      

      【讨论】:

        猜你喜欢
        • 2012-11-05
        • 2013-04-04
        • 1970-01-01
        • 2011-11-21
        • 2011-06-27
        • 2012-08-06
        • 1970-01-01
        • 1970-01-01
        • 2016-01-06
        相关资源
        最近更新 更多