2011-12-06 40 views
0

我正在使用XSD,並且在其中包含許多xsd:元素的xsd:choice。現在我想稍微簡化一下,因此我分組了xsd:元素,這樣我就可以將它們外包(並重用)到單獨的xsd:元素中。如何將xsd:choice的元素組合在一起並將它們外包?

前:

<!-- main.xsd --> 
<xsd:element name="account"> 
    <xsd:complexType> 
     <xsd:choice> 
      <xsd:element name="login-id" type="xsd:string" /> 
      <xsd:element name="login-username" type="xsd:string" /> 
      <xsd:element name="login-password" type="xsd:string" /> 
      <xsd:element name="login-note" type="xsd:string" /> 

      <xsd:element name="contact-name" type="xsd:string" /> 
      <xsd:element name="contact-address" type="xsd:string" /> 
      <xsd:element name="contact-phone" type="xsd:string" /> 
      <xsd:element name="contact-note" type="xsd:string" /> 
     </xsd:choice> 
    </xsd:complexType> 
</xsd:element> 

後:

<!-- main.xsd --> 
<xsd:include schemaLocation="outsourced.xsd" /> 

<xsd:element name="account"> 
    <xsd:complexType> 
     <xsd:choice> 
      <xsd:element ref="login" /> 

      <xsd:element ref="contact" /> 
     </xsd:choice> 
    </xsd:complexType> 
</xsd:element> 

<!-- outsourced.xsd --> 
<xsd:element name="login"> 
    <xsd:complexType> 
     <xsd:choice> 
      <xsd:element name="login-id" type="xsd:string" /> 
      <xsd:element name="login-username" type="xsd:string" /> 
      <xsd:element name="login-password" type="xsd:string" /> 
      <xsd:element name="login-note" type="xsd:string" /> 
     </xsd:choice> 
    </xsd:complexType> 
</xsd:element> 

<xsd:element name="contact"> 
    <xsd:complexType> 
     <xsd:choice> 
      <xsd:element name="contact-name" type="xsd:string" /> 
      <xsd:element name="contact-address" type="xsd:string" /> 
      <xsd:element name="contact-phone" type="xsd:string" /> 
      <xsd:element name="contact-note" type="xsd:string" /> 
     </xsd:choice> 
    </xsd:complexType> 
</xsd:element> 

可悲的是這將創建一個新的類 '登錄',並生成Java的源代碼,從它,我想,以避免在 '接觸'。有沒有辦法將外包分組的xsd:元素來簡化xsd:choice?

回答

3

我不知道你在尋找什麼,但它應該是下列之一:

  • 你要麼要簡化所產生的場的名稱,以便代替N1N2N3你得到的東西更具可讀性;在這種情況下,我建議去this solution presented here
  • 我建議你閱讀Blaise的博客herehere - 它應該有助於理解你的解決方案或至少如何更好地制定你的問題。

更新:好吧,你需要做的是包裝這兩個羣體爲xsd:元素與一個xsd:序列,然後用綁定的播放文件,以獲得想要的東西(我假設你使用JAXB )。

更新2:所以它聽起來像你正在使用JAXB。然後,如果你採用下面的模式,我相信它給你你想要的,重用(通過引用模型組),並且沒有新的類。我會發布這兩個工件,但我建議你也嘗試一下(我使用過NetBeans),看看發生了什麼事情(還要編寫一個使用該類的Java測試客戶機,瞭解開發人員將看到的內容等)以及如果它仍然不是你想要的,用你的嘗試結果來說明你的問題。

XSD:

<?xml version="1.0" encoding="utf-8" ?> 
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="account"> 
     <xsd:complexType> 
      <xsd:choice> 
       <xsd:group ref="login"/> 
       <xsd:group ref="contact"/> 
      </xsd:choice> 
     </xsd:complexType> 
    </xsd:element> 

    <xsd:group name="login"> 
     <xsd:sequence> 
      <xsd:element name="login-id" type="xsd:string"/> 
      <xsd:element name="login-username" type="xsd:string"/> 
      <xsd:element name="login-password" type="xsd:string"/> 
      <xsd:element name="login-note" type="xsd:string"/> 
     </xsd:sequence> 

    </xsd:group> 

    <xsd:group name="contact"> 
     <xsd:sequence> 
      <xsd:element name="contact-name" type="xsd:string"/> 
      <xsd:element name="contact-address" type="xsd:string"/> 
      <xsd:element name="contact-phone" type="xsd:string"/> 
      <xsd:element name="contact-note" type="xsd:string"/> 
     </xsd:sequence> 
    </xsd:group> 
</xsd:schema> 

的Java生成的類(默認情況下,沒有自定義綁定,去除不相關的內容):

package sequnderchoice; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlType; 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "loginId", 
    "loginUsername", 
    "loginPassword", 
    "loginNote", 
    "contactName", 
    "contactAddress", 
    "contactPhone", 
    "contactNote" 
}) 
@XmlRootElement(name = "account") 
public class Account { 

    @XmlElement(name = "login-id") 
    protected String loginId; 
    @XmlElement(name = "login-username") 
    protected String loginUsername; 
    @XmlElement(name = "login-password") 
    protected String loginPassword; 
    @XmlElement(name = "login-note") 
    protected String loginNote; 
    @XmlElement(name = "contact-name") 
    protected String contactName; 
    @XmlElement(name = "contact-address") 
    protected String contactAddress; 
    @XmlElement(name = "contact-phone") 
    protected String contactPhone; 
    @XmlElement(name = "contact-note") 
    protected String contactNote; 

    public String getLoginId() { 
     return loginId; 
    } 

    public void setLoginId(String value) { 
     this.loginId = value; 
    } 

    public String getLoginUsername() { 
     return loginUsername; 
    } 

    public void setLoginUsername(String value) { 
     this.loginUsername = value; 
    } 

    public String getLoginPassword() { 
     return loginPassword; 
    } 

    public void setLoginPassword(String value) { 
     this.loginPassword = value; 
    } 

    public String getLoginNote() { 
     return loginNote; 
    } 

    public void setLoginNote(String value) { 
     this.loginNote = value; 
    } 

    public String getContactName() { 
     return contactName; 
    } 

    public void setContactName(String value) { 
     this.contactName = value; 
    } 

    public String getContactAddress() { 
     return contactAddress; 
    } 

    public void setContactAddress(String value) { 
     this.contactAddress = value; 
    } 

    public String getContactPhone() { 
     return contactPhone; 
    } 

    public void setContactPhone(String value) { 
     this.contactPhone = value; 
    } 

    public String getContactNote() { 
     return contactNote; 
    } 

    public void setContactNote(String value) { 
     this.contactNote = value; 
    }   
} 
+0

現在的問題是澄清了一下。 –

+0

這就是我的問題,我不知道該怎麼做(確切地說)。 –

+0

不幸的是,如果你想很好地「集合」一組元素,我不知道沒有創建一個新類的方法。如果您使用JAXB,則需要一個自定義綁定文件。原則上,我喜歡這種方式,而不是創建一個包裝元素,因爲它會保持你的XML結構平坦... –

相關問題