An XML document is said to be well formed if it follows it’s syntax rules very closely. The rules that must apply to an XML document are listed below:

All XML elements must have closing tags

Example

<book>

<title>Harry Potter series</title>

<author>J.K.Rowling</author>

</book>

XML tags are case-sensitive

Opening and closing tags must be written in same case.

Example

<title>Harry Potter series</title> <!--- Correct --->

<title>Harry Potter series</Title> <!--- Not correct --->

XML tags must be properly nested

Unlike HTML, XML is sensitive to the order of tags.

Example

<b><i>See the order of tags</b></i> //Valid in HTML but invalid in XML.

XML documents must have the root element

The root element s the parent to all other elements in the document.

Example

<root>

<child>

      <sub-child>…</sub-child>

</child>

</root>

XML attributes are set in name:value pair

The name:value pair gives more information on the tag itself. The value should be quoted as shown below.

Example

<book type=”fiction”>

<title>Harry Potter series</title>

<author>J.K.Rowling</author>

</book>

Parsing properly the predefined entities

If a XML document has predefined entity such as “<”, “>” etc... which will be interpreted by parser as start or end of new element, it has to be replaced with entity references. The entity references and it’s symbol are listed below:

  • < - &lt;
  • > - &gt;
  • & - &amp;
  • ‘ - &apos;
  • “ - &quot;

Example

<p>Sanju & Geetha are friends </p> <!--- Not correct --->

<p>Sanju &amp; Geetha are friends </p> <!--- Not correct --->

Properly adding comments

To add a comment <!---- comment here ----> symbol is used as in HTML.

Preservation of white spaces

White spaces are preserved in XML and not truncated as in HTML.

Example

<p>May     June</p> is not same as

<p>May  June</p>

New line storage

A new line is stored as LF.

 

›› go to examples ››