【问题标题】:Symbol is already defined. Use JAXB property to resolve the conflict符号已定义。使用 JAXB 属性解决冲突
【发布时间】:2013-02-16 14:16:21
【问题描述】:

我有一个 xsd 文件 (yahoo.xsd),我在其中导入另一个 xsd 文件,如下所示:

  <xs:import schemaLocation="stock.xsd"/>
  <xs:attribute name="lang" type="xs:NCName"/>

stock.xsd 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng">
<xs:import namespace="http://www.yahooapis.com/v1/base.rng" schemaLocation="yahoo.xsd"/>
<xs:element name="quote">
<xs:complexType>
  <xs:sequence>  
    <xs:element ref="Symbol"/>
  </xs:sequence>
  <xs:attribute name="symbol" use="required" type="xs:NCName"/>
</xs:complexType>
</xs:element>
<xs:element name="Symbol" type="xs:NCName"/>
</xs:schema>

当我使用 xjc 进行编译时,我收到以下错误消息:

[错误] 属性“符号”已定义。使用 来解决此冲突。

我基本上在 SO (JAXB Compiling Issue - [ERROR] Property "Any" is already defined) 上找到了解决方案,但我无法让它工作。我猜我的 XPath 是错误的。

这是我正在使用的绑定文件:

<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
      xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      version="2.1">
<bindings schemaLocation="yahoo.xsd" version="1.0" >
    <!-- rename the value element -->
        <bindings node="//xs:element[@name='quote']/xs:complexType/xs:sequence/xs:element[@ref='Symbol']">
            <property name="SymbolAttribute"/>
    </bindings>
</bindings>

如果我现在使用 xjc -b 进行编译,则表示 XPath 评估结果为空目标节点。

我可能必须重命名 Symbol 定义,然后还要重命名 ref?如何自动执行此操作?

【问题讨论】:

  • 您是否尝试将架构添加为基本节点?例如。?

标签: java xml xpath jaxb xml-serialization


【解决方案1】:

让我问一下这条线:

<xs:element ref="Symbol"/>

Symbol 是在 yahoo.xsd 中定义还是在同一个 xsd 文件中本地定义?

我会尝试推断一些事实。

我假设您有两个 XSD:yahoo.xsdsome.xsd(您帖子中的第一个)。 我坚信“符号”类型是在 some.xsd 中定义的,而不是在 yahoo.xsd 中定义的。如果不是这样,我会期望一些命名空间前缀(“yahoo:Symbol”?)。

现在,您的 some.xsd 是否与此类似:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" >
    <!-- It's not important right now: -->
    <!--<xs:import namespace="http://www.yahooapis.com/v1/base.rng" schemaLocation="yahoo.xsd"/>-->

    <!-- declaration you omitted in your post, it's only example -->
    <xs:element name="Symbol">
        <xs:simpleType>
            <xs:restriction base="xs:integer">
              <xs:minInclusive value="0"/>
              <xs:maxInclusive value="100"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>

    <xs:element name="quote">
        <xs:complexType>
          <xs:sequence>  
            <xs:element ref="Symbol"/>
          </xs:sequence>
          <xs:attribute name="symbol" use="required" type="xs:NCName"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

如果我说的是真的,那么你的 jaxb 绑定应该是这样的:

<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
      xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      version="2.1">
    <bindings schemaLocation="some.xsd"> <!-- not yahoo.xsd -->
        <bindings node="//xs:element[@name='quote']/xs:complexType/xs:sequence/xs:element[@ref='Symbol']">
            <property name="SymbolAttribute" />
        </bindings>
    </bindings>

</bindings>

生成的java类将是:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "symbolAttribute"
})
@XmlRootElement(name = "quote")
public class Quote {

    @XmlElement(name = "Symbol")
    protected int symbolAttribute;
    @XmlAttribute(name = "symbol", required = true)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlSchemaType(name = "NCName")
    protected String symbol;
    ....

【讨论】:

  • 谢谢:我已经尝试过了,但是没有用。但是您所说的另一件事可能是问题的根源:符号在其他地方定义,并且仅在我尝试使用 XPath 更改的行中引用。我将其添加到我的原始帖子中。我想我必须自动重命名它们?还是 Jaxb 会自动更新对重命名节点的所有引用?我现在不能尝试,但今晚会检查它。
  • 好的,事实证明这正是问题所在。将绑定节点更改为 即可解决问题。
猜你喜欢
  • 2011-05-22
  • 1970-01-01
  • 2016-01-12
  • 2019-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多