Thursday 23 June 2016

Difference between Well Formed Xml and Valid Xml


In This post we are going to see Difference between Well Formed Xml and Valid Xml  in SQL SERVER.


Well Formed XML: XML which adheres to the syntax rules.


1. XML documents must have a root element
2. XML elements must have a opening and closing tag
3. XML tags are all case sensitive
4. XML elements must be properly nested parent and child elements
5. XML attribute values must be quoted
6. XML elements should start from alphabetic name and can have alpha numeric in between the names

 Well Formed XML

<Employees>   
   <Employee id="E001">      
     <firstname>Rajesh</firstname>      
     <lastname>G</lastname>      
     <role>Architect</role>      
     <salary>1</salary>      
     <joindate>2000-10-01</joindate>      
   </Employee>    
   <Employee id="E002">     
     <firstname>Suresh</firstname>      
     <lastname>G</lastname>      
     <role>Tech Lead</role>      
     <salary>1</salary>      
     <joindate>2000-10-01</joindate>   
   </Employee>
</Employees>


            


Valid XML: It is a well formed xml and which is validated against rules present in DTD

 Valid XML

<?xml version="1.0standalone="yes" ?>
<!DOCTYPE Employees [
  <!ELEMENT EMPLOYEES (EMPLOYEE)*>
  <!ELEMENT EMPLOYEE  (firstname,lastname,role,salary,joindate)>
  <!ELEMENT firstname (#PCDATA)>
  <!ELEMENT lastname  (#PCDATA)>
  <!ELEMENT role      (#PCDATA)>
  <!ELEMENT salary    (#PCDATA)>
  <!ELEMENT joindate  (#PCDATA)>
] >
<x:Employees xmlns:x="urn:Employees">   
   <Employee id="E001">      
     <firstname>Rajesh</firstname>      
     <lastname>G</lastname>      
     <role>Architect</role>      
     <salary>1</salary>      
     <joindate>2000-10-01</joindate>      
   </Employee>    
   <Employee id="E002">     
     <firstname>Suresh</firstname>      
     <lastname>G</lastname>      
     <role>Tech Lead</role>      
     <salary>1</salary>      
     <joindate>2000-10-01</joindate>   
   </Employee>
</x:Employees>



 XSD

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"            
  targetNamespace="urn:Employees"            
  xmlns:bks="urn:Employees">   

<xsd:element name="bookstype="bks:EmployeesForm"/>   

<xsd:complexType name="EmployeesForm">    
     <xsd:sequence>      
        <xsd:element name="Employee                  
                     type="bks:EmployeeForm                  
                     minOccurs="0                  
                     maxOccurs="unbounded"/>      
     </xsd:sequence>  
</xsd:complexType> 
  
  <xsd:complexType name="EmployeeForm">    
     <xsd:sequence>      
       <xsd:element name="firstname"   type="xsd:string"/>      
       <xsd:element name="lastname"    type="xsd:string"/>      
       <xsd:element name="role"    type="xsd:string"/>      
       <xsd:element name="salary"    type="xsd:float/>      
       <xsd:element name="joindatetype="xsd:date/>    
     </xsd:sequence>
     <xsd:attribute name="id"   type="xsd:string"/>  
  </xsd:complexType>

</xsd:schema>



From this post you can learn what is the difference between the Well Formed Xml and Valid Xml



No comments:

Post a Comment