XML - Validation



Validation is a process by which an XML document is validated. An XML document is said to be valid if its contents match with the elements, attributes and associated document type declaration(DTD), and if the document complies with the constraints expressed in it. Validation is dealt in two ways by the XML parser. They are −

  • Well-formed XML document
  • Valid XML document

Well-formed XML Document

An XML document is said to be well-formed if it adheres to the following rules −

  • Non DTD XML files must use the predefined character entities for amp(&), apos(single quote), gt(>), lt(<), quot(double quote).

  • It must follow the ordering of the tag. i.e., the inner tag must be closed before closing the outer tag.

  • Each of its opening tags must have a closing tag or it must be a self ending tag.(<title>....</title> or <title/>).

  • It must have only one attribute in a start tag, which needs to be quoted.

  • amp(&), apos(single quote), gt(>), lt(<), quot(double quote) entities other than these must be declared.

Example

Following is an example of a well-formed XML document −

<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>
<!DOCTYPE address
[
   <!ELEMENT address (name,company,phone)>
   <!ELEMENT name (#PCDATA)>
   <!ELEMENT company (#PCDATA)>
   <!ELEMENT phone (#PCDATA)>
]>

<address>
   <name>Tanmay Patil</name>
   <company>TutorialsPoint</company>
   <phone>(011) 123-4567</phone>
</address>

The above example is said to be well-formed as −

  • It defines the type of document. Here, the document type is element type.

  • It includes a root element named as address.

  • Each of the child elements among name, company and phone is enclosed in its self explanatory tag.

  • Order of the tags is maintained.

Valid XML Document

If an XML document is well-formed and has an associated Document Type Declaration (DTD), then it is said to be a valid XML document. We will study more about DTD in the chapter XML - DTDs.

Advertisements