Exception: cvc-complex-type.2.4.a: Invalid content was found starting at eleme nt 'employee'. One of the "{ContractEmployee}" is expected to

So I have this ER diagram: ER diagram

Hence, I wrote company.xml as:

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

<company xmlns="urn:company.Namespace" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:schemaLocation="urn:company.Namespace companyxsd.xsd">
    <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 companyxsd.xsd like this:

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

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="urn:company.Namespace" xmlns="urn:company.Namespace">

    <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'm not sure if my xml represents the ER diagram as is. If not, please help me with the correct xml template.

Since I start .. I have doubts about the XSd I wrote. ": (

For the above, I keep getting errors as such

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

      

New error:

Exception: cvc-elt.1.a: Cannot find the declaration of element 'company'
      

Help me here, buddies!

+1


source to share


2 answers


I had the same two errors too. The error does not require any changes in the referenced part of the file xml

and xsd

. In fact, the Scorm editor can be very complex.

one of {employee, contractemployee} is expected

This error expects some of the element's employee and contracted attributes, such as the minOccurs and MaxOccurs attributes.

I have posted the relevant code for xml

and xsd

which works successfully in the Scorm editor. HAPPY CODING !!!



XML:

<?xml version="1.0" encoding="UTF-8"?>
<company>
    <companyname>company name</companyname>
    <address>address</address>
    <department>
        <dname>dname</dname>
        <deptphoneno>22238500</deptphoneno>
        <deptfaxno>123456789</deptfaxno>
        <deptemail>email@email</deptemail>
        <employee>
            <empid>123</empid>
            <ename>ename</ename>
            <emailid>email@email</emailid>
            <phoneno>9444872829</phoneno>
        </employee>
        <contractemployee>
            <name>name</name>
            <phoneno>9445815259</phoneno>
        </contractemployee>
    </department>
</company>

      

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 maxOccurs="unbounded" name="department">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="dname" type="xs:string"/>
                            <xs:element name="deptphoneno" type="xs:string"/>
                            <xs:element name="deptfaxno" type="xs:integer"/>
                            <xs:element name="deptemail" type="xs:string"/>
                            <xs:element maxOccurs="unbounded" minOccurs="1" 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 maxOccurs="unbounded" minOccurs="0" 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>

      

+1


source


The XML + XSD you posted does not throw the error you specified, XML is not actually valid xml. However you are very close, there is a problem with the namespace declaration in XML as well as in XSD.

In XSD, you need to set target namespace like:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="company.namespace">

      

Now, to make sure the document is validated, the namespace in the xml should be the same as the XSD. XML also has incorrect formatting of the namespace declaration. Do it like this:

<company xmlns="company.namespace">

      

Keep in mind that this is just a namespace, it is not an actual link to the xsd file, many namespaces look like URIs, however, most are not actual functional URIs.

I have replaced the namespace with the example "company.namespace" to make it clearer. Using filenames can create an assumption that they actually point to files.



When using schemaLocation in XML, you need to add a namespace / prefix declaration to identify the corresponding XSD file. The xml parser does not currently recognize the xs prefix because it is unknown in the XML file. You can solve this problem as follows:

<company xmlns="urn:company.Namespace"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
 xs:schemaLocation="urn:company.Namespace companyxsd.xsd"> 

      

Here are the resulting files: XML

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

<company xmlns="urn:company.Namespace"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
 xs:schemaLocation="urn:company.Namespace companyxsd.xsd">    <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>    

      

XSD

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

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="urn:company.Namespace" xmlns="urn:company.Namespace">

    <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>

      

0


source







All Articles