我有一个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进行编译时,我收到以下错误消息:
[错误]属性“符号”已定义。使用&lt; jaxb:property&gt;解决这个冲突。
我基本上在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定义,然后重命名?如何自动完成?
答案 0 :(得分:6)
让我问一下这句话:
<xs:element ref="Symbol"/>
是在yahoo.xsd中定义的符号还是在同一个xsd文件中的本地?
我会尝试推断一些事实。
我假设你有两个XSD:yahoo.xsd
和some.xsd
(你帖子中的第一个)。
我有很强的信心“符号”类型在some.xsd
中定义,而不在yahoo.xsd
中定义。如果是这样,我会期待一些名称空间前缀(“雅虎:符号”?)。
现在,你的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;
....