3
votes

I am trying to type XSD for XML but an error has occurred. I don't know why it's occurring. I'm new to XML so don't really know much about it. Error is:

Exception: s4s-elt-must-match.1: The content of 'persons' must match (annotation?, (simpleType | complexType)?, (unique | key | keyref)*)). A problem was found starting at: complexType.

Any help or suggestion is really appreciated.

XML

<?xml version="1.0" encoding="utf-8"?>

<persons>

    <person>

        <name>Tom</name>

        <age>11</age>

        <gender>M</gender>

        <address>

            <doorno>27</doorno>

            <street>Tony's road, koramangala</street>

            <city>Bangalore</city>

            <state>Karnataka</state>

        </address>

        <student>

            <rollno>10</rollno>

            <standard>6</standard>

            <section>A</section>

        </student>  

    </person>

    <person>

        <name>Shiny</name>

        <age>12</age>

        <gender>F</gender>

        <address>

            <doorno>10</doorno>

            <street>Main Bazar, Madiwala</street>

            <city>Bangalore</city>

            <state>Karnataka</state>

        </address>

        <staff>

            <staffid>123</staffid>

            <subject>Maths</subject>

        </staff>

    </person>

</persons>

XSD that I'm trying to write

<?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" maxOccurs="unbounded" minOccurs="1" type="personType"/>
            </xs:sequence>
        </xs:complexType>
        <xs:complexType name="personType">
            <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" type="addType"/>
                <xs:element name="student" maxOccurs="unbounded" minOccurs="0" type="studentType"/>
                <xs:element name="staff" maxOccurs="unbounded" minOccurs="0" type="staffType"/>
            </xs:sequence>
        </xs:complexType>
        <xs:complexType name="addType">
            <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:complexType>
        <xs:complexType name="studentType">
            <xs:sequence>
                <xs:element name="rollno" type="xs:integer"/>
                <xs:element name="standard" type="xs:string"/>
                <xs:element name="section" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
        <xs:complexType name="staffType">
            <xs:sequence>
                <xs:element name="staffid" type="xs:integer"/>
                <xs:element name="stubject" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
1

1 Answers

2
votes

Two fixes required:

  1. Close the xs:element tag for person before the complex type declarations of the remaining types.
  2. Fix a typo: stubject should be `subjection.

Altogether, the following XSD will validate your XML document successfully:

<?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" maxOccurs="unbounded" minOccurs="1" type="personType"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="personType">
    <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" type="addType"/>
      <xs:element name="student" maxOccurs="unbounded" minOccurs="0" type="studentType"/>
      <xs:element name="staff" maxOccurs="unbounded" minOccurs="0" type="staffType"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="addType">
    <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:complexType>
  <xs:complexType name="studentType">
    <xs:sequence>
      <xs:element name="rollno" type="xs:integer"/>
      <xs:element name="standard" type="xs:string"/>
      <xs:element name="section" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="staffType">
    <xs:sequence>
      <xs:element name="staffid" type="xs:integer"/>
      <xs:element name="subject" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>