1
votes

I am a newbie in xml and all and i have been assigned a task to make an xml file to load configuration in a c++ program. Since i have never done this before i used xml copy editor to create the following xml and then selected the create schema option to create a xsd for the xml however when i am trying to validate it is giving error. can someone tell me what i am doing wrong.

I have not written the c++ code as my first priority is to create a valid xml and xsd.

xml :

<?xml version="1.0" encoding="UTF-8"?>
<part_id>
   <class_id>
      <head>
         <defect />
         <dim>
            <dia>
               <unit />
               <tolrence />
            </dia>
            <rad>
               <unit />
               <tolrence />
            </rad>
            <height>
               <unit />
               <tolrence />
            </height>
         </dim>
      </head>
      <stamp>
         <defect />
         <dim>
            <depth>
               <unit />
               <tolrence />
            </depth>
            <width>
               <unit />
               <tolrence />
            </width>
         </dim>
      </stamp>
      <prethread>
         <defect />
         <dim>
            <dia>
               <unit />
               <tolrence />
            </dia>
            <length>
               <unit />
               <tolrence />
            </length>
         </dim>
      </prethread>
      <thread_seg>
         <defect />
         <dim>
            <dia_major>
               <unit />
               <tolrence />
            </dia_major>
            <length>
               <unit />
               <tolrence />
            </length>
         </dim>
      </thread_seg>
      <terminal>
         <defect />
         <dim>
            <height>
               <unit />
               <tolrence />
            </height>
            <length>
               <unit />
               <tolrence />
            </length>
         </dim>
      </terminal>
   </class_id>
</part_id>

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
   <xs:element name="part_id">
      <xs:complexType>
         <xs:sequence>
            <xs:element ref="class_id" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="class_id">
      <xs:complexType>
         <xs:sequence>
            <xs:element ref="head" />
            <xs:element ref="stamp" />
            <xs:element ref="prethread" />
            <xs:element ref="thread_seg" />
            <xs:element ref="terminal" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="head">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="defect" type="xs:string" />
            <xs:element ref="dim" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="dim">
      <xs:complexType>
         <xs:sequence>
            <xs:element ref="depth" minOccurs="0" />
            <xs:element ref="width" minOccurs="0" />
            <xs:element ref="dia_major" minOccurs="0" />
            <xs:element ref="dia" minOccurs="0" />
            <xs:element ref="rad" minOccurs="0" />
            <xs:element ref="height" minOccurs="0" />
            <xs:element ref="length" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="dia" type="xs:">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="unit" type="xs:decimal" />
            <xs:element name="tolrence" type="xs:decimal" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="rad">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="unit" type="xs:decimal" />
            <xs:element name="tolrence" type="xs:decimal" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="height">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="unit" type="xs:decimal" />
            <xs:element name="tolrence" type="xs:decimal" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="stamp">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="defect" type="xs:string" />
            <xs:element ref="dim" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="depth">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="unit" type="xs:decimal" />
            <xs:element name="tolrence" type="xs:decimal" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="width">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="unit" type="xs:decimal" />
            <xs:element name="tolrence" type="xs:decimal" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="prethread">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="defect" type="xs:string" />
            <xs:element ref="dim" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="length">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="unit" type="xs:decimal" />
            <xs:element name="tolrence" type="xsdecimal" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="thread_seg">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="defect" type="xs:string" />
            <xs:element ref="dim" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="dia_major">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="unit" type="xs:decimal" />
            <xs:element name="tolrence" type="xs:decimal" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="terminal">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="defect" type="xs:string" />
            <xs:element ref="dim" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
</xs:schema>

for validation I went to :https://www.freeformatter.com/xml-validator-xsd.html

Error: S4s-att-invalid-value: Invalid Attribute Value For 'type' In Element 'element'. Recorded Reason: Cvc-datatype-valid.1.2.1: 'xs:' Is Not A Valid Value For 'QName'.

1
what is the error and please share code you've writtenmohit sharma
correct your typos in XSD <xs:element name="tolrence" type="xsdecimal" /> (a 'e' and ':' are missing). In XML, you should have tolerence as name of elements.Loic Mouchard

1 Answers

0
votes

Errors in your XSD

<xs:element name="dia" type="xs:">

and

<xs:element name="tolrence" type="xsdecimal" />

Then you need a value in the unit and tolrence elements (as they are decimals)

<dia>
    <unit>123</unit>
    <tolrence>123</tolrence>
</dia>

Then use an XML Editor or online XSD validator to check it.