0
votes

XYZ school wants to store the details of students and staff in an XML file. The following scenario helps in designing the XML document:

  • persons will be the root tag.
  • persons will have the entry of each person with name, age, gender, address.
  • A person can be either a student or staff.
  • student will have rollno, standard and section.
  • If staff, then staffid and subject.
  • Every person must have an address with the following entry- doorno, street,city and state.

I am getting this error:

Exception: cvc-complex-type.2.4.a: Invalid content was found starting with element 'student'. One of '{name}' is expected.

2
This seems to be a schema validation error so it is worth providing both the schema document and your xml document to get the effective solution. - Alexey R.
Improved the formatting of the question by introducing a list and formatting the error message. Changed title to something more meaningful. - zx485

2 Answers

1
votes

xsd

    <?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="persons">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="person">
               <xs:complexType>
                  <xs:sequence>
                     <xs:element name="name" type="xs:string" />
                     <xs:element name="age" type="xs:integer" />
                     <xs:element name="gender" type="xs:string" />
                     <xs:element name="address">
                        <xs:sequence>
                           <xs:element name="doorno" type="xs:integer" />
                           <xs:element name="street" type="xs:string" />
                           <xs:element name="city" type="xs:string" />
                           <xs:element name="state" type="xs:string" />
                        </xs:sequence>
                     </xs:element>
                     <xs:element name="student" minOccurs="0">
                        <xs:complexType>
                           <xs:sequence>
                              <xs:element name="rollno" type="xs:integer" />
                              <xs:element name="standard" type="xs:integer" />
                              <xs:element name="section" type="xs:string" />
                           </xs:sequence>
                        </xs:complexType>
                     </xs:element>
                     <xs:element name="staff" minOccurs="0">
                        <xs:complexType>
                           <xs:sequence>
                              <xs:element name="staffid" type="xs:integer" />
                              <xs:element name="subject" type="xs:string" />
                           </xs:sequence>
                        </xs:complexType>
                     </xs:element>
                  </xs:sequence>
               </xs:complexType>
            </xs:element>
         </xs:sequence>
      </xs:complexType>
   </xs:element>
</xs:schema>
0
votes

One of the names may not be of valid type. Try the following code.

<?xml version="1.0" encoding="utf-8"?>
<persons>
    <person>
        <name>Sam</name>
        <age>11</age>
        <gender>M</gender>
        <address>
            <doorno>20</doorno>
            <street>Gandhi Colony</street>
            <city>Hyderabad</city>
            <state>Telangana</state>
        </address>
        <student>
            <rollno>39</rollno>
            <standard>6</standard>
            <section>A</section>
        </student>
    </person>
    <person>
        <name>Sally</name>
        <age>30</age>
        <gender>F</gender>
        <address>
            <doorno>17</doorno>
            <street>Ambedkar Road</street>
            <city>Hyderabad</city>
            <state>Telangana</state>
        </address>
        <staff>
            <staffid>120</staffid>
            <subject>English</subject>
        </staff>
    </person>
</persons>