2011-12-06 45 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结构平坦... –

相关问题