Syntax Rules of XML

The syntax rules of XML are not so easy, they are very simple and logical. The rules are easy to learn, and easy to use.

Must Have a Root Element

XML documents must have one root element which is the parent of all other elements:


In the below code <note> is the root element:

<?xml version="1.0" encoding="UTF-8"?>
<body>Don't forget me this weekend!</body>


  • XML tags are surrounded by the two characters < and >.
  • The start tag is often called the opening tag, and the end tag is called the closing tag.
  • XML tags must be closed in the proper order.
  • XML Tags are Case Sensitive.
  • The text between the start and end tags is the data.

XML Start Tag


XML End Tag


Empty XML Tag


XML Tags are Case Sensitive

XML tags are case sensitive. The tag <Message> is different from the tag <message>.

Opening and closing tags must be written with the same case:

<message>This is correct</message>

Elements Must be Properly Nested

In XML, Elements are properly nested:

<b><i>This text is bold and italic</i></b>

Entity References

Some characters have a special meaning in XML.

If you place a character like “<” inside an XML element, there will an error because the parser interprets it as the start of a new element. To avoid this error, replace the “<” character with an entity reference.

There are 5 predefined entity references in XML:

&lt;<less than
&gt;>greater than
&quot;quotation mark

XML comments

Web Browsers completely ignores XML comment tags. XML comment tags are used to insert comments into the source code.

Comment are used anywhere in the source code to add information about XML tags or data, which will be helpful in future for the developers to understand the existing code.

<!--This is a comment area, write your comment-->
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Employee Information-->
  <company>Tech Blicks</company>
  <phone>(91) 123-4567</phone>

XML Comments Rules

  • Comment cannot be used before an XML declaration.
  • Comment cannot be used within the attribute value.
  • There is no meaning to used comment inside a comment.

Leave a Comment

Your email address will not be published. Required fields are marked *