Tuesday, July 3, 2012

Well Formed XMLs and XML Validation

Well Formed and Valid XML

When we define an XML, we talk two things about it that whether an XML is well formed and whether the given XML is valid. Let us understand briefly what do we mean by that.

Well Formed XML

So, when we talk about an XML being well formed, by that we mean that the XML we are talking about is syntactically correct and meet the following conditions:
  1.  It has necessarily a root element.

    So, the below XML is incorrect because it doesn't contain the root element in it.
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <first>John</first>
    <middle>Dear</middle>
    <last>Jill</last>

    The correct XML is one with a root element
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <name id="10">
        <first>John</first>
        <middle>Dear</middle>
        <last>Jill</last>
    </name>

  2. All the elements in the given XML document have a closing tag

    So, the below element will be incorrect in an XML
    <last>Jill

    The correct XML element format would be
    <last>Jill</last>
    <last />
  3. Make sure that all XML tags match the closing tag (a thing to remember would be that XML tags are case sensitive)

    So, the below element will be incorrect in an XML
    <last>Jill</Last>

    The correct XML element format would be
    <last>Jill</last>
  4. All the elements are properly nested

    So, the below XML nesting is incorrect because it doesn't close tags in order.
    <name id="10">
        <first>John</name>
    </first>


    The correct XML is one with proper sequence of nesting
    <name id="10">
        <first>John</first>
    </name>
  5. All the attributes are quoted

    So, the below XML element is incorrect because it doesn't holds the attribute value in quotes
    <name id=10>
        <first>John</first>
    </name>


    The correct XML is elements containing attribute values in quotes
    <name id="10">
        <first>John</first>
    </name>

 

Valid XML

An XML is valid if it well formed and conforms to a prototype. We already know well formed XMLs from the previous section. What do we mean by conforming to a prototype and at first place why do we need it?
Think of the following scenario, suppose two applications (Application1, Application2) wants to communicate information using a common XML. Suppose that they decided to pass a name using an XML document just as in the above sections.
<?xml version="1.0" encoding="ISO-8859-1"?>
<name id="10">
    <first>John</first>
    <middle>Dear</middle>
    <last>Jill</last>
</name>

 
Now suppose that Application1 finds that a person doesn't have a middle name and it decides to skip that tag information and passes the following information
<?xml version="1.0" encoding="ISO-8859-1"?>
<name id="10">
    <first>John</first>
    <last>Jill</last>
</name>



Application2 on the other side starts decoding the message and expects <middle> tag after <first>, but finds <last> tag, so it fails. Similar kind of misunderstanding can happen many times. So, there is a need of a common structure for the XML instance to which both the application conforms to. In other words, a validation of the XML passed should be done by a common prototype. That is why, we need a validation tool for the XMLs.
XML validation is done by two popular ways: DTD and and XML based alternative to DTD known as XSD. The purpose of both of them is to define a structure to an XML.

XML DTD

The way DTD (Document Type Definition) defines structure for an XML is by listing list of legal elements. So, for our example XML, DTD defintion is as under:
<!DOCTYPE name
[
<!ELEMENT name (first, middle, last)>

<!ATTLIST name id CDATA #REQUIRED>
<!ELEMENT first (#PCDATA)>
<!ELEMENT middle (#PCDATA)>
<!ELEMENT last (#PCDATA)>
 ]>

You can find the W3C recommendation tutorial on this on http://www.w3schools.com/dtd/default.asp

XML Schema

XML Schema is an XML based alternative to DTD to verify an XML document defined by W3C. For our example XML, the schema definition is as under:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="name">

        <xs:complexType>
            <xs:sequence>
                <xs:element name="first" type="xs:string"/>
                <xs:element name="middle" type="xs:string"/>
                <xs:element name="last" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:string" use="required" />
        </xs:complexType>
    </xs:element>

</xs:schema>

Practical applications

Let me give some practical applications of the validation model.
  • Suppose you have created a web service and want users to use it. You have even decided on protocol or template in XML format on how you will describe your web service. But then you have a question in your mind. Will the users of my web service be able to understand my protocol. You need a common format based on which you can create the web service description and based on that all users of your web service will understand your service details. Based on these requirements, WSDL (Web Services Description Language) was created where the service publisher and receiver follows a common prototype of WSDL
  • Another example could be seen when you use a XML based GUI editor where you mention different layouts and other features for your GUI. The idea there again is to adhere to a common prototype set up by the application framework (say for example Java Swing, Android)

Online References

You can find the W3C recommendation tutorial on this on http://www.w3schools.com/schema/default.asp

Also, you can find several online XML and XSD validators online like the one on  http://xsdvalidation.utilities-online.info

No comments:

Post a Comment