如何将xsd:choice的xsd:choice组合在一起并将其外包?

时间:2011-12-06 20:19:17

标签: xsd jaxb

我正在使用XSD,我有一个xsd:选项,其中包含许多xsd:元素。现在我想稍微简化它,因此我将xsd:元素分组,以便我可以在单独的xsd:elements中外包(并重用)它们。

在:

<!-- 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-Source时创建一个新的Classes'login'和'contact',这是我想要避免的。有没有办法将分组的xsd:元素外包以简化xsd:choice?

1 个答案:

答案 0 :(得分:2)

我不确定你到底在找什么,但它应该是以下之一:

  • 你要么想简化生成字段的名称,要么取代n1n2n3,你会得到更具可读性的东西;在这种情况下,我建议使用this solution presented here
  • 我建议您阅读Blaise的博客herehere - 它应该有助于了解您的解决方案,或者至少如何更好地制定您的问题。

更新:好的,你需要做的是用xsd:sequence包装这两组xsd:元素,然后使用绑定文件来获得你想要的东西(我假设你正在使用JAXB)。 / p>

更新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 Generated类(默认情况下,没有自定义绑定,删除不相关的内容):

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;
    }        
}