Home > front end >  How do i create correct Validation from XML to XSD?
How do i create correct Validation from XML to XSD?

Time:12-18

First time trying XML. I auto generated the XSD but its not correct if i validate XML against XSD. I always get the error => Element 'LOC': This element is not expected. Expected is ( JOB ). If i create 2 Datasets for each element it is working but i want to use 1 Dateset for 2 element "Dept" and "EMP".

<?xml version="1.0" encoding="UTF-8"?>
<DB>
    <DEPT>
      <DATASET deptno="10" dname="research">
        <LOC>New York</LOC>
       </DATASET>

     <DATASET deptno="20" dname="research">
      <LOC>DALLAS</LOC>
     </DATASET>

      <DATASET deptno="30" dname="sales">
       <LOC>CHICAGO</LOC>
     </DATASET>

     <DATASET deptno="40" dname="operations">
     <LOC>BOSTON</LOC>
     </DATASET>
   </DEPT>

    <EMP>
    <DATASET empno="7369" ename="smith">
      <JOB>CLERK</JOB>
      <MGR>7902</MGR>
      <HIREDATE>1980-12-17</HIREDATE>
      <SAL>800,00</SAL>
      <COMM>NULL</COMM>
      <DEPTNO>20</DEPTNO>
    </DATASET>

      <DATASET empno="7499" ename="allen">
      <JOB>SALESMAN</JOB>
      <MGR>7698</MGR>
      <HIREDATE>1981-02-20</HIREDATE>
      <SAL>1600,00</SAL>
      <COMM>300,00</COMM>
      <DEPTNO>30</DEPTNO>
      </DATASET>

      <DATASET empno="7521" ename="ward">
      <JOB>SALESMAN</JOB>
      <MGR>7698</MGR>
      <HIREDATE>1981-02-22</HIREDATE>
      <SAL>1250,00</SAL>
      <COMM>500,00</COMM>
      <DEPTNO>30</DEPTNO>
      </DATASET>

      <DATASET empno="7566" ename="jones">
      <JOB>MANAGER</JOB>
      <MGR>7839</MGR>
      <HIREDATE>1981-04-02</HIREDATE>
      <SAL>2975,00</SAL>
      <COMM>NULL</COMM>
      <DEPTNO>20</DEPTNO>
      </DATASET>

   </EMP>
</DB>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="DB">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="DEPT"/>
        <xs:element ref="EMP"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="DEPT">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="DATASET" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="DATASET">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="JOB" type="xs:string"/>
        <xs:element name="MGR" type="xs:string"/>
        <xs:element name="HIREDATE" type="xs:string"/>
        <xs:element name="SAL" type="xs:string"/>
        <xs:element name="COMM" type="xs:string"/>
        <xs:element name="DEPTNO" type="xs:string"/>
        <xs:element name="LOC" type="xs:string"/>
      </xs:sequence>
      <xs:attribute name="deptno" type="xs:string"/>
      <xs:attribute name="dname" type="xs:string"/>
      <xs:attribute name="empno" type="xs:string"/>
      <xs:attribute name="ename" type="xs:string"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="EMP">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="DATASET" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

CodePudding user response:

Please try the following XSD.

XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="DB">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="DEPT"/>
        <xs:element ref="EMP"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="DEPT">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="DATASET"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="EMP">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="DATASET"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="DATASET">
    <xs:complexType>
      <xs:choice>
        <xs:element ref="LOC"/>
        <xs:sequence>
          <xs:element ref="JOB"/>
          <xs:element ref="MGR"/>
          <xs:element ref="HIREDATE"/>
          <xs:element ref="SAL"/>
          <xs:element ref="COMM"/>
          <xs:element ref="DEPTNO"/>
        </xs:sequence>
      </xs:choice>
      <xs:attribute name="deptno" type="xs:integer"/>
      <xs:attribute name="dname" type="xs:string"/>
      <xs:attribute name="empno" type="xs:integer"/>
      <xs:attribute name="ename" type="xs:string"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="LOC" type="xs:string"/>
  <xs:element name="JOB" type="xs:string"/>
  <xs:element name="MGR" type="xs:integer"/>
  <xs:element name="HIREDATE" type="xs:date"/>
  <xs:element name="SAL" type="xs:string"/>
  <xs:element name="COMM" type="xs:string"/>
  <xs:element name="DEPTNO" type="xs:integer"/>
</xs:schema>
  • Related