XSD - <xs:sequence>没有<xs:complextype>?

时间:2015-10-14 09:32:30

标签: xml xsd

我有一个简单的XML数据,想要使用它来验证它 我的XSD文件。我想在任何类型的中创建XSD文件 “面向对象”的方式。因为至少在我看来,理解/阅读会更好。

MY XML:

<?xml version="1.0" encoding="UTF-8"?>
<DSExport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="TestXSD.xsd">

    <Job Identifier="someString1">
        <Project name="someString2">
            <tag1 />
            <tag2 />
        </Project>
    </Job>

</DSExport>

MY XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <!-- "MAIN" -->
    <xs:element name="DSExport">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Job" type="JobType">

                    <!-- NOTE: SOMETHING WENT WRONG HERE -->
                    <xs:sequence>
                        <xs:element name="Project" type="ProjectType">
                            <xs:sequence>
                                <xs:element name="tag3"></xs:element>
                                <xs:element name="tag4"></xs:element>
                            </xs:sequence>
                        </xs:element>
                    </xs:sequence>
                    <!-- NOTE: SOMETHING WENT WRONG HERE -->

                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <!--  "FUNCTIONS" (WANT TO SEPERATE THE SPECIALIZED VALIDATION OF ALL ELEMENTS)-->
    <xs:complexType name="JobType">
        <xs:attribute name="Identifier" type="xs:string" use="required" />
    </xs:complexType>

    <xs:complexType name="ProjectType">
        <xs:attribute name="name" type="xs:string" use="required" />
    </xs:complexType>

我使用eclipse。我有任何错误消息,如:

s4s-elt-must-match.1: The content of 'Job' must match (annotation?, (simpleType | complexType)?, (unique | 
 key | keyref)*)). A problem was found starting at: sequence.

如果我要在“主要”部分写下每个部分,我知道如何解决它... 但我怎么能像“面向对象”那样解决呢?我想分开 每个元素的complexType定义!

感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

您已经两次定义了Job和Project元素。您应该将序列移动到类型定义,如下所示:

<xs:element name="DSExport">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Job" type="JobType"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:complexType name="JobType">
    <xs:sequence>
        <xs:element name="Project" type="ProjectType"/>
    </xs:sequence>
    <xs:attribute name="Identifier" type="xs:string" use="required" />
</xs:complexType>

<xs:complexType name="ProjectType">
    <xs:sequence>
        <xs:element name="tag3"></xs:element>
        <xs:element name="tag4"></xs:element>
    </xs:sequence>
    <xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>

另见:XSD element with both attributes and child elements