2013-03-01 353 views
6

我有一個XSD文件(yahoo.xsd),我導入另一個XSD文件是這樣的:符號已被定義。使用JAXB屬性來解決衝突

<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編譯時,我收到以下錯誤消息:

[錯誤]屬性「符號」已定義。使用< jaxb:property>來解決此衝突。

我基本上在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的評價結果​​在一個空的目標節點。

我可能必須重命名符號定義,然後重新命名?如何自動做到這一點?

+0

您是否嘗試添加模式作爲基節點?例如。? 2013-03-04 03:36:14

回答

6

讓我問這行:

<xs:element ref="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; 
    .... 
+0

謝謝:我已經試過了,但它沒有奏效。但是你所說的另一件事可能是問題的根源:符號是在其他地方定義的,並且只在我試圖用XPath更改的行中引用。我將其添加到我原來的帖子中。我想我必須自動重命名他們兩個?或者Jaxb會自動更新對重命名節點的所有引用?我現在不能嘗試它,但今天晚上會檢查它。 – Nicolas 2013-03-04 15:43:35

+1

好吧,原來這就是問題所在。將綁定節點更改爲解決了這個問題。 – Nicolas 2013-03-04 22:48:06