简体   繁体   English

如何在 xsd 中嵌套 complexType 元素?

[英]How do you nest complexType elements in an xsd?

I have an xml and xsd file that both validate correctly (tested at http://xsdvalidation.utilities-online.info/ ).我有一个 xml 和 xsd 文件都正确验证(在http://xsdvalidation.utilities-online.info/测试)。

However, the xml does not validate against the xsd.但是,xml 不针对 xsd 进行验证。 I think this is because I am incorrectly nesting complexType elements in the xsd, as compared to the xml.我认为这是因为与 xml 相比,我在 xsd 中错误地嵌套了 complexType 元素。 The outer element of people seems to be causing the problem... people的外部因素似乎是造成问题的原因......

Here is the xml:这是 xml:

<?xml version = "1.0"?>

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

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

...and here is the xsd: ...这是 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>

Add maxOccurs="unbounded" to the element named "person".maxOccurs="unbounded"添加到名为“person”的元素。 It is a sequence of one or more person elements.它是一个或多个人元素的序列。

Note: write maxOccurs in lowerCamelCase not in lower case注意:将 maxOccurs 写成 lowerCamelCase 而不是小写

Try this for your XSD:为你的 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM