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:

<root>
  <child>
    <subchild>.....</subchild>
  </child>
</root>

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

<?xml version="1.0" encoding="UTF-8"?>
<note>  
<to>James</to>  
<from>Andrew</from>  
<heading>Reminder</heading>  
<body>Don't forget me this weekend!</body>
</note>

XML Tag

Syntax:
<name>James</name>
  • 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

Syntax:
<name>

XML End Tag

Syntax:
</name>

Empty XML Tag

Syntax:
<name></name>

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
&amp;&ampersand
&apos;apostrophe
&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.

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

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.

Pankul Bindal

Pankul Bindal is Digital Marketer, Graphic Designer and Programmer. He has completed his Computer Engineering. His passion is to write blogs, Designing and Digital Marketing. He works for many Companies in his freelancing Career.

0 Comments

Leave a Reply

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