xjc:两个声明导致 ObjectFactory 类发生冲突

新手上路,请多包涵

运行以下 xjc 命令会引发错误:

 $ xjc "ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd"
parsing a schema...
compiling a schema...
[ERROR] Two declarations cause a collision in the ObjectFactory class.
  line 340 of ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd

[ERROR] (Related to above error) This is the other declaration.
  line 475 of ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd

虽然我了解 JAXB 绑定以及 XJC 中的冲突,但我不了解当前模式中的冲突在哪里。

我应该怎么解决这个问题?

谢谢,

皮埃尔

更新:这是错误的上下文:

 $ curl -s "ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd" | sed 's/^[ \t]*//' | cat -n | egrep -w -A 10 -B 10 '(340|475)'
   330  <xs:element maxOccurs="1" name="Description"
   331  type="xs:string" minOccurs="0">
   332  <xs:annotation>
   333  <xs:documentation>
   334  Optionally provide description especially when "eOther" is selected
   335  </xs:documentation>
   336  </xs:annotation>
   337  </xs:element>
   338  <xs:element name="BioSampleSet" minOccurs="0" maxOccurs="1"><xs:annotation><xs:documentation>Identifier of the BioSample when known</xs:documentation>
   339  </xs:annotation>
   340  <xs:complexType><xs:sequence><xs:element name="ID" maxOccurs="unbounded" type="xs:token"></xs:element>
   341  </xs:sequence>
   342  </xs:complexType>
   343  </xs:element>
   344  </xs:sequence>
   345  <xs:attribute name="sample_scope" use="required">
   346  <xs:annotation>
   347  <xs:documentation>
   348  The scope and purity of the biological sample used for the study
   349  </xs:documentation>
   350  </xs:annotation>
--
   465  <xs:documentation>Please,  fill Description element when choose "eOther"</xs:documentation>
   466  </xs:annotation>
   467  </xs:enumeration>
   468  </xs:restriction>
   469  </xs:simpleType>
   470  </xs:attribute>
   471  </xs:complexType>
   472  </xs:element>
   473  <xs:element name="TargetBioSampleSet">
   474  <xs:annotation><xs:documentation>Set of Targets references to BioSamples</xs:documentation></xs:annotation>
   475  <xs:complexType>
   476  <xs:sequence>
   477  <xs:element name="ID" type="xs:token" minOccurs="1" maxOccurs="unbounded"></xs:element>
   478  </xs:sequence>
   479  </xs:complexType>
   480  </xs:element>
   481  </xs:choice>
   482  <xs:element name="Method" minOccurs="1">
   483  <xs:annotation>
   484  <xs:documentation>
   485  The core experimental approach used to obtain the data that is submitted to archival databases

原文由 Pierre 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.2k
2 个回答

我将引用网上 关于 JAXB 的最官方的非官方指南

当模式包含相似的元素/类型名称时,它们可能会导致“两个声明导致 ObjectFactory 类发生冲突”错误。更准确地说,对于所有类型和许多元素中的每一个(究竟哪些元素有工厂,哪些没有工厂有点难以解释),XJC 在同一个包中的 ObjectFactory 类上生成一个方法。 ObjectFactory 类是为 XJC 生成一些文件的每个包创建的。方法名来源于XML元素/类型名,如果两个元素/类型试图生成相同的方法名,就会报错。

也就是说,您有两个选择。

第一种是像这样定义一个外部绑定 XML

 <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  version="1.0">
  <jaxb:bindings schemaLocation="Core.xsd">
    <jaxb:bindings node="//xs:element[@name='BioSampleSet']/xs:complexType">
      <jaxb:factoryMethod name="TypeBioSampleSet"/>
    </jaxb:bindings>
    <jaxb:bindings node="//xs:element[@name='TargetBioSampleSet']/xs:complexType">
      <jaxb:factoryMethod name="TypeTargetBioSampleSet"/>
    </jaxb:bindings>
  </jaxb:bindings>
</jaxb:bindings>

In the generated ObjectFactory class this will create two methods called createTypeBioSampleSet and createTypeTargetBioSampleSet (JAXB will append the name you specify to the word create ) 可用于生成 BioSampleSetTargetBioSampleSet 对象。

(没有必要为 这两种 类型定义绑定。)

我不确定为什么 JAXB 拒绝从给定模式生成类,但是当我只指定一个绑定(例如 BioSampleSet )时,另一种类型的工厂方法被命名为 createTypeProjectProjectTypeSubmissionWhateverThisAndThatTargetTargetSampleBioCatDogWoofTypeIDoNotKnowWhatElse 所以我认为 JAXB 对这个长方法标识符感到窒息,因为它以某种方式设法为两种类型创建了相同的方法标识符。我认为这是 JAXB 中的一些实现细节。

另一种解决方案是为 BioSampleSet 创建一个基类型,并像这样在两个位置使用它

<xs:element name="ProjectTypeSubmission">

...

  <xs:element name="Target">

    ...

    <xs:element name="BioSampleSet" type="typeBioSampleSet" minOccurs="0" maxOccurs="1"/>

    ...

  </xs:element>

  ...

  <xs:element name="TargetBioSampleSet" type="typeBioSampleSet"/>

  ...

<xs:element/>

...

<xs:complexType name="typeBioSampleSet">
  <xs:sequence>
    <xs:element name="ID" maxOccurs="unbounded" type="xs:token"></xs:element>
  </xs:sequence>
</xs:complexType>

最好的解决方案是从您的架构中删除所有匿名类型声明。如果你能做到,那就去做吧,因为这个模式看起来一团糟(至少对我来说)。

原文由 Kohányi Róbert 发布,翻译遵循 CC BY-SA 3.0 许可协议

命令中删除-p包

xjc -d src -XautoNameResolution TFMData_Service.xsd

原文由 vks 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题