java 符号已定义。使用 JAXB 属性解决冲突

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15168583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:42:31  来源:igfitidea点击:

Symbol is already defined. Use JAXB property to resolve the conflict

javaxmlxpathjaxbxml-serialization

提问by Nicolas

I have a xsd file (yahoo.xsd) where I import another xsd file like this:

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

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

The stock.xsd looks like this:

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>

When I am compiling with xjc I am getting the following error message:

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

[ERROR] Property "Symbol" is already defined. Use <jaxb:property> to resolve this conflict.
[错误] 属性“符号”已定义。使用 <jaxb:property> 解决此冲突。

I basically found the solution for this here on SO (JAXB Compiling Issue - [ERROR] Property "Any" is already defined) but I can't get it to work. I am guessing my XPath is wrong.

我基本上在 SO(JAXB 编译问题 - [错误] 属性“任何”已定义)上找到了解决方案,但我无法让它工作。我猜我的 XPath 是错误的。

This is the binding file I am using:

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

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

If I am now compiling with xjc -b it says that the XPath evaluation results in an empty target node.

如果我现在使用 xjc -b 进行编译,它表示 XPath 评估会导致一个空的目标节点。

I probably have to rename the Symbol definition and then the ref as well? how to do this automatically?

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

采纳答案by Pawe? Wyrwiński

Let me ask about this line:

让我问一下这条线:

<xs:element ref="Symbol"/>

is Symbol defined in yahoo.xsd or locally in the same xsd file?

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

I'll try to deduce some facts.

我会试着推断出一些事实。

I assume you have two XSDs: yahoo.xsdand some.xsd(first one in your post). I have strong confidence "Symbol" type is defined in some.xsdand not in yahoo.xsd. If it were otherwise i would expect some namespace prefix ("yahoo:Symbol" ?).

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

Now, is it true your some.xsd looks similar to this:

现在,您的 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>

If what i say is true, then your jaxb binding should look like this:

如果我说的是真的,那么您的 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>

And generated java class will be:

生成的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;
    ....