符号已定义。使用JAXB属性来解决冲突 [英] Symbol is already defined. Use JAXB property to resolve the conflict
问题描述
我有一个xsd文件(yahoo.xsd),我在其中导入另一个xsd文件,如下所示:
I have a xsd file (yahoo.xsd) where I import another xsd file like this:
<xs:import schemaLocation="stock.xsd"/>
<xs:attribute name="lang" type="xs:NCName"/>
stock.xsd如下所示:
The stock.xsd looks like this:
<?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进行编译时,我收到以下错误消息:
When I am compiling with xjc I am getting the following error message:
[错误]属性符号已定义。使用< jaxb:property>来解决此冲突。
[ERROR] Property "Symbol" is already defined. Use <jaxb:property> to resolve this conflict.
我基本上在SO上找到了这个解决方案( JAXB编译问题 - [错误]属性任何已定义)但我无法使其工作。我猜我的XPath是错误的。
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.
这是我正在使用的绑定文件:
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>
如果我现在正在编译xjc -b它表示XPath评估会产生一个空的目标节点。
If I am now compiling with xjc -b it says that the XPath evaluation results in an empty target node.
我可能要重命名Symbol定义,然后重命名ref?如何自动执行此操作?
I probably have to rename the Symbol definition and then the ref as well? how to do this automatically?
推荐答案
让我问一下这句话:
<xs:element ref="Symbol"/>
是在yahoo.xsd中定义的符号还是在同一个xsd文件中的本地?
is Symbol defined in yahoo.xsd or locally in the same xsd file?
我会尝试推断一些事实。
I'll try to deduce some facts.
我假设您有两个XSD: yahoo.xsd
和 some.xsd
(你帖子中的第一个)。
我有很强的信心符号类型在 some.xsd
中定义,而不是在 yahoo.xsd
。如果是这样,我会期待一些名称空间前缀(雅虎:符号?)。
I assume you have two XSDs: yahoo.xsd
and some.xsd
(first one in your post).
I have strong confidence "Symbol" type is defined in some.xsd
and not in yahoo.xsd
. If it were otherwise i would expect some namespace prefix ("yahoo:Symbol" ?).
现在,你的some.xsd看起来是这样的:
Now, is it true your some.xsd looks similar to this:
<?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绑定应该是这样的:
If what i say is true, then your jaxb binding should look like this:
<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类将是:
And generated java class will be:
@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;
....
这篇关于符号已定义。使用JAXB属性来解决冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!