XML Syntax Rules

XML syntax rules are strongly and very simple logically. XML syntax rules are specifies how to write valid XML document.

Rules for Write Valid or Well Formatted XML Document

8 XML Syntax Rules for Write Valid or Well Formatted XML Document

XML documents must have only one root element

XML document must have only one root element that is the parent of the all other elements.


Element must have closing tag

In HTML, some element does not have a closing tag.

<p>This is the paragraph    <!-- Invalid -->
<br >                       <!-- Invalid -->

<p>This is the paragraph</p>  <!-- Valid -->
<br />                        <!-- Valid -->

In XML every element must have closing tag.

<name>Opal Kole             <!-- Invalid -->
<name>Opal Kole</name>      <!-- Valid -->

Element must be properly nested

In XML, every element properly nested within each other.

  <name>Opal Kole </contact_info> </name>   <!-- Invalid -->

  <name>Opal Kole</name>
</contact_info>                             <!-- Valid -->

XML Comments

Write comment in XML is similar to a HTML,

<!-- This is a comment text -->

XML tag case sensitive

XML tag name are case sensitive. <name>, <Name> and <NAME> different tag from each other. Always opening and closing tag must be write with same case.

<name>Opal Kole<Name>   <!-- Invalid -->

<name>Opal Kole<name>   <!-- Valid -->
<Name>Opal Kole<Name>   <!-- Valid -->
<NAME>Opal Kole<NAME>   <!-- Valid -->

XML Attribute value must be quoted

XML elements can have specifies several attributes along with name value pair. XML attribute values must be quoted.

<emp_info id=1>
    <name>Opal Kole</name>
</emp_info>           <!-- Invalid -->

<emp_info id="1">
    <name>Opal Kole</name>
</emp_info>           <!-- Valid -->

White-space is not preserved in XML

XML break multiple white space character to a single white-space.

<name>Opal       Kole<name>   <!-- Valid -->

Output value combine multiple white space into single white space.

Opal Kole

Entity Support

In XML special character have some special meaning similar to a HTML.

If you are use < sign or > sign inside XML element, It'll generate a error because document parse interprets assume it's start new element.

<emp_info>employee number < 15 </emp_info>     <!-- Invalid -->

For avoiding this error use entity character instead of some special character (<, >).

<emp_info>employee number &lt; 15 </emp_info>   <!-- Valid -->

XML specification defines five predefined entities represent special characters. Following table represent five XML predefined entities lists.

Character Entities Unicode Description Standard
" &quot; U+0022 (34) Double quotation XML 1.0
' &apos; U+0027 (39) Apostrophe (Single quotation) XML 1.0
& &apos; U+0026 (38) ampersand sign XML 1.0
< &lt; U+003C (60) less than XML 1.0
> &gt; U+003E (62) greater than XML 1.0