关于 java:Symbol 已经定义。使用 JAXB 属性解决冲突 | 珊瑚贝

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


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

1
2
  <xs:import schemaLocation=“stock.xsd”/>
  <xs:attribute name=“lang” type=“xs:NCName”/>

stock.xsd 看起来像这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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 编译时,我收到以下错误消息:

[ERROR] Property”Symbol” is already defined. Use to resolve this conflict.

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

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

1
2
3
4
5
6
7
8
9
10
<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?如何自动执行此操作?

  • 您是否尝试将架构添加为基本节点?例如。? <bindings schemaLocation=”yahoo.xsd” version=”1.0″ node=”/xs:schema”>


让我问一下这条线:

1
<xs:element ref=“Symbol”/>

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

我会尝试推断一些事实。

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

现在,您的 some.xsd 看起来是否类似于:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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 绑定应该是这样的:

1
2
3
4
5
6
7
8
9
10
11
<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类将是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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 会自动更新对重命名节点的所有引用?我现在不能尝试,但今晚会检查它。
  • 好吧,事实证明这正是问题所在。将绑定节点更改为 <bindings node=”//xs:element[@name=\\’Symbol\\’]”> 可以解决问题。


来源:https://www.codenong.com/15168583/

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?