Reputation: 283
Persons will 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 student must have an address with the following entry- doorno,street,city and state. The admin has come up with an XML document.
Here is my 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>
and my XSD :
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="persons">
<xs:complexType>
<xs:sequence>
<xs:element name="person" maxOccurs="unbounded">
<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:complexType>
<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:element>
<xs:element name="student">
<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">
<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>
Iam new to XML and a learner.
The error:
Exception: cvc-complex-type.2.4.b: The content of element 'person' is not comple te. One of '{staff}' is expected.
Upvotes: 0
Views: 4527
Reputation: 2211
Your XSD says that there should be <xs:element name="student">
followed by <xs:element name="staff">
, but instead you would like to have "either a student or staff". For this XSD has another element, <xs:choice>
:
<xs:choice>
<xs:element name="student">
<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">
<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:choice>
Please also check out another answer: How to specify in an XML schema that either one of two fields must be present?
Upvotes: 1