如何在xsd中嵌入complexType元素?

时间:2012-06-22 00:11:44

标签: xml validation xsd complextype

我有一个xml和xsd文件,它们都正确验证(在http://xsdvalidation.utilities-online.info/测试)。

但是,xml不会对xsd进行验证。我认为这是因为与xml相比,我在xsd中错误地嵌套了complexType元素。 people的外部元素似乎导致问题......

这是xml:

<?xml version = "1.0"?>

<people>
    <person>
        <firstname>Joe</firstname>
        <lastname>Schmoe</lastname>
    </person>

    <person>
        <firstname>Cletus</firstname>
        <lastname>Jenkins</lastname>
    </person>
</people>

...这里是xsd:

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

    <xs:element name = "people">
        <xs:complexType>
            <xs:sequence>

                <xs:element name = "person">
                    <xs:complexType>
                        <xs:sequence>

                            <xs:element name = "firstname" type = "xs:string" />
                            <xs:element name = "lastname" type = "xs:string" />

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

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

</xs:schema>

2 个答案:

答案 0 :(得分:12)

maxoccurs="unbounded"添加到名为“person”的元素中。它是一个或多个人元素的序列。

答案 1 :(得分:3)

试试这个XSD:

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

    <xs:element name="people" type="people"/>

    <xs:complexType name="people">
        <xs:sequence>
            <xs:element name="person" type="person" maxOccurs="unbounded" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="person">
        <xs:sequence>
            <xs:element name="firstname" type="xs:string" maxOccurs="1" minOccurs="1"/>
            <xs:element name="lastname" type="xs:string" maxOccurs="1" minOccurs="1"/>
       </xs:sequence>
    </xs:complexType>

</xs:schema>