sky_diver89
sky_diver89

Reputation: 25

Invalid Content Was Found Starting With Element

I have error by writing xml and xsd. I wrote the students.xsd:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema attributeFormDefault="unqualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">
    <xs:element name="students">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="student">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name">
                                <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:element name="faculty">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:enumeration value="fkp"/>
                                        <xs:enumeration value="fitu"/>
                                        <xs:enumeration value="fre"/>
                                        <xs:enumeration value="fksis"/>
                                        <xs:enumeration value="ftk"/>
                                        <xs:enumeration value="fnido"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name="address">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="country">
                                            <xs:simpleType>
                                                <xs:restriction base="xs:string">
                                                    <xs:minLength value="2"/>
                                                    <xs:maxLength value="20"/>
                                                </xs:restriction>
                                            </xs:simpleType>
                                        </xs:element>
                                        <xs:element name="city">
                                            <xs:simpleType>
                                                <xs:restriction base="xs:string">
                                                    <xs:minLength value="2"/>
                                                    <xs:maxLength value="20"/>
                                                </xs:restriction>
                                            </xs:simpleType>
                                        </xs:element>
                                        <xs:element name="street">
                                            <xs:simpleType>
                                                <xs:restriction base="xs:string">
                                                    <xs:minLength value="2"/>
                                                    <xs:maxLength value="30"/>
                                                </xs:restriction>
                                            </xs:simpleType>
                                        </xs:element>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="phone">
                                <xs:simpleType>
                                    <xs:restriction base="xs:positiveInteger">
                                        <xs:length value="6"/>
                                        <xs:pattern value="[0-9]"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                        </xs:sequence>
                        <xs:attribute name="id" type="xs:int" use="required"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

And I have file students.xml:

<?xml version="1.0" encoding="utf-8" ?>
<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="students.xsd">
    <student id="1">
        <name>
            <firstName>Ivan</firstName>
            <lastName>Ivanov</lastName>
        </name>
        <faculty>fksis</faculty>
        <address>
            <country>Belarus</country>
            <city>Minsk</city>
            <street>Rokossovskogo 24</street>
        </address>
        <phone>6677088</phone>
    </student>
    <student id="2">
        <name>
            <firstName>Petr</firstName>
            <lastName>Petrov</lastName>
        </name>
        <faculty>fre</faculty>
        <address>
            <country>Belarus</country>
            <city>Mogilev</city>
            <street>Leninskaya 25</street>
        </address>
        <phone>5467043</phone>
    </student>
</students>

In students.xml I have the error (Invalid content was found starting with element 'student'. No child element is expected at this point.) in part:

<student id="2">
        <name>
            <firstName>Petr</firstName>
            <lastName>Petrov</lastName>
        </name>
        <faculty>fre</faculty>
        <address>
            <country>Belarus</country>
            <city>Mogilev</city>
            <street>Leninskaya 25</street>
        </address>
        <phone>5467043</phone>
    </student>

What is problem? How to correct this error? Thank you!

Upvotes: 1

Views: 2018

Answers (1)

aryn.galadar
aryn.galadar

Reputation: 735

The way your schema is set up, it defaults to allow only one "student" element in "students". You should set the maxOccurs to unbounded if you want one or more student elements.

<xs:element name="student" maxOccurs="unbounded">

(maxOccurs defaults to 1 if omitted)

Upvotes: 1

Related Questions