1

Generate XML for thisenter image description here

My Xml:

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

<company>
    <companyname>ABC company</companyname>
    <address>xyz street, India.</address>

    <department>
        <dname>Marketing</dname>
        <deptphoneno>9876543210</deptphoneno>
        <deptfaxno>0442456879</deptfaxno>
        <deptemail>marketing@abc.com</deptemail>

        <employee>
            <empid>101</empid>
            <ename>Rishie</ename>
            <emailid>rishie@abc.com</emailid>
            <phoneno>9876543211</phoneno>
        </employee>

        <contractemployee>
            <name>Ravi</name>
            <phoneno>9874563214</phoneno>
        </contractemployee>
    </department>

</company>   

and my XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="company">
     <xs:complexType>
      <xs:sequence>
       <xs:element name="companyname" type="xs:string"/>
       <xs:element name="address" type="xs:string"/>

       <xs:element name="department">
        <xs:complexType>
         <xs:sequence>
          <xs:element name="dname" type="xs:string"/>
          <xs:element name="deptphoneno" type="xs:integer"/>
          <xs:element name="deptfaxno" type="xs:integer"/>
          <xs:element name="deptemail" type="xs:string"/>

          <xs:element name="employee">      
           <xs:complexType>
            <xs:sequence>
             <xs:element name="empid" type="xs:integer"/>
             <xs:element name="ename" type="xs:string"/>
             <xs:element name="emailid" type="xs:string"/>
             <xs:element name="phoneno" type="xs:integer"/>
            </xs:sequence>
           </xs:complexType>
          </xs:element>


          <xs:element name="contractemployee">      
           <xs:complexType>
            <xs:sequence>
             <xs:element name="name" type="xs:string"/>
             <xs:element name="phoneno" type="xs:integer"/>
            </xs:sequence>
           </xs:complexType>
          </xs:element>

         </xs:sequence>
        </xs:complexType>
       </xs:element>
      </xs:sequence>
     </xs:complexType>
    </xs:element>

</xs:schema>

I know that at first glance everything looks correct...but i keep getting some errors! I hope someone could help me out with this! Iam not sure whether its the xml or the xsd.

The Error:

Exception: cvc-complex-type.2.4.a: Invalid content was found starting with eleme
nt 'employee'. One of '{contractemployee}' is expected.

Please help me out with the tag <xs:schema> in my XSD and <company> in xml.

isekaid_maou
  • 273
  • 1
  • 9
  • 19

2 Answers2

4
***XML file-***
<?xml version="1.0" encoding="UTF-8"?>
<company>
 <companyname>ABC</companyname>
 <address>Mumbai</address>
 <department>
    <dname>IT</dname>
    <deptphoneno>123</deptphoneno>
    <deptfaxno>456</deptfaxno>
    <deptemail>abc@gmail.com</deptemail>
    <employee>
        <empid>8</empid>
        <ename>xyz</ename>
        <emailid>xyz@gmail.com</emailid>
        <phoneno>789</phoneno>
    </employee>
  </department>
  <department>
    <dname>EE</dname>
    <deptphoneno>789</deptphoneno>
    <deptfaxno>901</deptfaxno>
    <deptemail>mno@gmail.com</deptemail>
    <employee>
        <empid>9</empid>
        <ename>abc</ename>
        <emailid>pqr@gmail.com</emailid>
        <phoneno>567</phoneno>
    </employee>
    <contractemployee>
        <name>asd</name>
        <phoneno>234</phoneno>
    </contractemployee>
  </department>
</company>


***XSD file***
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="company">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="companyname" type="xs:string"/>
            <xs:element name="address" type="xs:string"/>

            <xs:element name="department" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="dname" type="xs:string"/>
                    <xs:element name="deptphoneno" type="xs:integer"/>
                    <xs:element name="deptfaxno" type="xs:integer"/>
                    <xs:element name="deptemail" type="xs:string"/>

                    <xs:element name="employee" maxOccurs="unbounded">
                        <xs:complexType>
                          <xs:sequence>
                            <xs:element name="empid" type="xs:integer"/>
                            <xs:element name="ename" type="xs:string"/>
                            <xs:element name="emailid" type="xs:string"/>
                            <xs:element name="phoneno" type="xs:integer"/>
                          </xs:sequence> 
                        </xs:complexType>
                    </xs:element>

                    <xs:element name="contractemployee" minOccurs="0" maxOccurs="unbounded">
                        <xs:complexType>
                          <xs:sequence>
                            <xs:element name="name" type="xs:string"/>
                            <xs:element name="phoneno" type="xs:integer"/>
                          </xs:sequence> 
                        </xs:complexType>
                    </xs:element>

                  </xs:sequence> 
                </xs:complexType>
            </xs:element>

          </xs:sequence> 
        </xs:complexType>
    </xs:element>
</xs:schema>
0

Your XML is valid against your XSD.

The error message in your question does not result from validating the XML with the XSD in your question.

Recheck the difference between what you've posted and what you're really using that's giving you a problem.

See also

kjhughes
  • 106,133
  • 27
  • 181
  • 240