complexType中的XML模式complexType

时间:2016-10-18 09:17:35

标签: xml xsd

我正在为以下XML编写XSD:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="WS.xslt" ?>
<interface name="WS">
    <package>com.example</package>
    <import>java.rmi.RemoteException</import>
    <import>java.io.*</import>
        <abstract_method name="login">
            <modifier>public</modifier>
            <arguments>
                <parameter type="int">id</parameter>
                <parameter type="String">pass</parameter>
            </arguments>
            <return>boolean</return>
        </abstract_method>
        <abstract_method name="send">
            <modifier>private</modifier>
            <arguments>
                <parameter type="String">subject</parameter>
                <parameter type="String">message</parameter>
            </arguments>
            <exceptions>
                <exception>IOException</exception>
                <exception>RemoteException</exception>
            </exceptions>
            <return>void</return>
        </abstract_method>
</interface>        

我目前的XSD:

<?xml version="1.0"?>
<xsd:schema  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:complexType name ="WebServiceInterface">
    <xsd:sequence>
    <xsd:element name ="package" type="xsd:string"/>
    <xsd:element name ="import" minOccurs="0" maxOccurs="unbounded"/>
    <xsd:complexType name ="abstract_method" minOccurs="0" maxOccurs= "unbounded">
        <xsd:element name ="modifier" type="xsd:string"/>
        <xsd:complexType name="arguments">
            <xsd:element name ="parameters" minOccurs="0" maxOccurs= "unbounded"/>
        </xsd:complexType>
        <xsd:complexType name ="exceptions">
            <xsd:element name ="exception" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:complexType>
        <xsd:element name = "return"/>
    </xsd:complexType>
    </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

错误讯息:

  

“S4s-elt-must-match.1:'序列'的内容必须   匹配(注释?,(元素|组|选择|序列|任意)*)。一个   找到问题从:ComplexType开始。“

如何在没有此错误的情况下包含界面?

1 个答案:

答案 0 :(得分:1)

您必须(解决方案1)将<xsd:complexType>包裹在<xsd:element>或(解决方案2)中,在其他位置定义<xsd:complexType name="...">并使用<xsd:element type="...">引用它。 complexType旨在为您要创建(和键入)的任何元素定义内容模型。解决方案2有时是首选,因为它允许您重用(按原样,或扩展或限制)您定义的内容模型。

<强>示例:

对于解决方案1:

  <xsd:element name="interface">
    <xsd:complexType>
       [... definition of the complexType ...]

对于解决方案2:       

  <xsd:complexType name="WebServiceInterface">
       [... definition of the complexType ...]

<强>最后:

因此,您可以通过以下方式更正架构:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:element name="interface" type="WebServiceInterface"/>

    <xsd:complexType name="WebServiceInterface">
        <xsd:sequence>
            <xsd:element name="package" type="xsd:string"/>
            <xsd:element name="import" minOccurs="0" maxOccurs="unbounded"/>
            <xsd:element name="abstract_method" minOccurs="0" maxOccurs="unbounded">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="modifier" type="xsd:string"/>
                        <xsd:element name="arguments">
                            <xsd:complexType>
                                <xsd:sequence>
                                    <xsd:element name="parameter" minOccurs="0" maxOccurs="unbounded"/>
                                </xsd:sequence>
                            </xsd:complexType>
                        </xsd:element>
                        <xsd:element name="exceptions">
                            <xsd:complexType>
                                <xsd:sequence>
                                    <xsd:element name="exception" minOccurs="0" maxOccurs="unbounded"/>
                                </xsd:sequence>
                            </xsd:complexType>
                        </xsd:element>
                        <xsd:element name="return"/>
                    </xsd:sequence>
                    <xsd:attribute name="name" type="xsd:string"/>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
        <xsd:attribute name="name" type="xsd:string"/>
    </xsd:complexType>

</xsd:schema>