xml 你如何在 xsd 中嵌套 complexType 元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11148609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:33:05  来源:igfitidea点击:

How do you nest complexType elements in an xsd?

xmlvalidationxsdcomplextype

提问by Ian Campbell

I have an xml and xsd file that both validate correctly (tested at http://xsdvalidation.utilities-online.info/).

However, the xml does not validate against the xsd. I think this is because I am incorrectly nesting complexType elements in the xsd, as compared to the xml. The outer element of peopleseems to be causing the problem...

Here is the xml:

我有一个 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>

...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>

回答by John Watts

Add maxoccurs="unbounded"to the element named "person". It is a sequence of one or more person elements.

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

回答by Jon Burgess

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>