2012-11-28 104 views
5

我想創建JAXB爲XCCDF-1.1.4.xsd這是一個標準的架構,可以從XCCDF Schema LocationJAXB編譯問題 - [錯誤]房產「任何」已經被定義

來獲得我目前結合使用EclipseLink MOXy作爲我的JAXB實現,因爲我喜歡它也可以生成JSON綁定的事實。

我固定的幾個場合,我打了臭名昭著的「[錯誤]屬性‘值使用外部XML綁定’已經被定義」的錯誤,現在我對

[ERROR] Property "Any" is already defined. Use <jaxb:property> to resolve this conflict. 
line 441 of file:/home/dchu/Playground/Java/eclipselink_moxy/xccdf_1.1.4/xccdf-1.1.4.xsd 

[ERROR] The following location is relevant to the above error 
line 444 of file:/home/dchu/Playground/Java/eclipselink_moxy/xccdf_1.1.4/xccdf-1.1.4.xs 

下面是擊中了錯誤發生錯誤的XML模式中的一行代碼片段。

<xsd:sequence> 
    <xsd:choice minOccurs="1" maxOccurs="1"> 
     <xsd:any namespace="http://purl.org/dc/elements/1.1/" 
       minOccurs="1" maxOccurs="unbounded"/> 
     <xsd:any namespace="http://checklists.nist.gov/sccf/0.1" 
       processContents="skip" 
       minOccurs="1" maxOccurs="unbounded"/> 
    </xsd:choice> 
</xsd:sequence> 

有沒有人知道這裏會出現什麼問題?謝謝!

回答

11

您可以使用外部綁定文件來重命名其中一個屬性。

binding.xml

<jxb:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1"> 

    <jxb:bindings schemaLocation="schema.xsd"> 
     <jxb:bindings 
      node="//xsd:complexType[@name='foo']/xsd:sequence/xsd:choice/xsd:any[@namespace='http://checklists.nist.gov/sccf/0.1']"> 
      <jxb:property name="any2" /> 
     </jxb:bindings> 
    </jxb:bindings> 

</jxb:bindings> 

XML架構(schema.xsd)

下面是XML模式的一個簡化版本:

<?xml version="1.0" encoding="UTF-8"?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/schema" 
    xmlns="http://www.example.org/schema" 
    elementFormDefault="qualified"> 

    <xsd:complexType name="foo"> 
     <xsd:sequence> 
      <xsd:choice minOccurs="1" maxOccurs="1"> 
       <xsd:any namespace="" 
        minOccurs="1" maxOccurs="unbounded" /> 
       <xsd:any namespace="http://checklists.nist.gov/sccf/0.1" 
        processContents="skip" minOccurs="1" maxOccurs="unbounded" /> 
      </xsd:choice> 
     </xsd:sequence> 
    </xsd:complexType> 

</xsd:schema> 

XJC呼叫

下面介紹如何使用外部綁定文件進行XJC調用。

xjc -b binding.xml schema.xsd 

生成的類(美孚)

package org.example.schema; 

import java.util.*; 
import javax.xml.bind.annotation.*; 
import org.w3c.dom.Element; 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "foo", propOrder = { 
    "any", 
    "any2" 
}) 
public class Foo { 

    @XmlAnyElement(lax = true) 
    protected List<Object> any; 
    @XmlAnyElement 
    protected List<Element> any2; 


    public List<Object> getAny() { 
     if (any == null) { 
      any = new ArrayList<Object>(); 
     } 
     return this.any; 
    } 

    public List<Element> getAny2() { 
     if (any2 == null) { 
      any2 = new ArrayList<Element>(); 
     } 
     return this.any2; 
    } 

} 
1

使用Blaise的建議使用Java JAXB實現生成JAXB綁定時創建JAXB外部綁定XML會工作。

但是在使用EclipseLink MOXy jaxb-compiler.sh時它不起作用。 Blaise說這可能是編譯器腳本中的一個可能的錯誤。 bug ticket 395328

目前,此問題的解決方法是使用JDK中的JAVA XJC命令並在生成的目錄中手動添加jaxb.properties文件。 Specifying-eclipselink-moxy-as-yours