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

霸气de小男生 提交于 2020-01-05 07:11:22

问题


So ..I have this ER diagram:

Hence I wrote an 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 as follows:

    <?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 am not sure whether my xml represents the ER diagram as it is. If not please help me out with the correct xml template.

Since Iam a beginner..I have doubts on the XSd I have written. ':(

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.

The new error:

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

Help me out here pals!


回答1:


I too had the same two errors. The error does not require any changes in the linking part of the xml and xsd file. Actually, the scorm editor can be very tricky.

one of {employee,contractemployee} is expected

This error expects some of the attributes in the element contract employee and employee such minOccurs and MaxOccurs attributes.

I have posted the appropriate code for the xml and xsd that runs 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>



回答2:


The XML + XSD you posted do not create the error you mentioned, the XML is not actually valid xml. However you are very close, there is a problem with your namespace declaration in the XML and also in the XSD.

In the XSD you need to set the target namespace, like this:

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

Now to make sure the document validates, the namespace in the xml needs to be the same as the one you put in the XSD. In the XML the formatting of the namespace declaration is also invalid. Make it as follows:

<company xmlns="company.namespace">

Keep in mind this is just a namespace, it is not an actual reference to the xsd file, a lot of namespaces look like URI's, however most are not actual functional URI's either.

I replaced the namespace with an example "company.namespace" just to make it clearer. Using filenames might create the suggestion they actually point to files.

When using the schemaLocation in the XML to identify the corresponding XSD file you need to add the namespace/prefix declaration. Currently the xml parser does not recognise the xs prefix because it is unknown in the XML file. You can solve this 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>


来源:https://stackoverflow.com/questions/44344663/exception-cvc-complex-type-2-4-a-invalid-content-was-found-starting-with-eleme

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!