Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 people seems to be causing the problem...

Here is the 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:

<?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>
like image 277
Ian Campbell Avatar asked Jun 22 '12 00:06

Ian Campbell


2 Answers

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

Note: write maxOccurs in lowerCamelCase not in lower case

like image 131
John Watts Avatar answered Nov 13 '22 15:11

John Watts


Try this for your 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>
like image 27
Jon Burgess Avatar answered Nov 13 '22 16:11

Jon Burgess