Yes, even though the language is quite free in it’s structure and syntax, but what did you expect… you could write
anything anywhere?!! Sorry, but that’s not the case…But still, the developers have lent us with quite a very few
rules. So, the language still remains to be quite easy. Congrats.!! this is what i said to myself when I myself was
Rule 1: All opening tags should have a closing tag
A closing tag is defined by placing a ‘/’ before the tag name of the opening tag.
All opening tags are strictly required to have a matching closing tag.
Rule 2: XML tags are case sensitive
Take care when defining tags as the tag names are case sensitive i.e. ‘Ayush’ and ‘ayush’ are two totally different
Rule 3: XML tags should be properly nested
It means that if tag A started in tag B then it should end before tag B.
<aa><bb> Testing tags </aa></bb>———–illegal
<aa><bb> Testing tags </bb></aa>———–legal
Though this rule is not valid in HTML. In HTML, both of the above code lines would be valid. (just the tags need to
be changed to valid ones)
This is a very important rule as it preserves the tree structure of the data we are inputting in the XML file.
You should take care to preserve the tree structure of the data.
Rule 4: All XML files should contain a root element
The first tag of a XML file is known as the root element
Rule 5: XML attributes must be quoted
All tags of XML can have properties known as attributes. They are defined with the tag name.
- <Ayush age=”19″>
Here Ayush is the tag name and age is his attribute. You should always remember to put the attribute value in
Rule 6:Avoid using special characters of XML and use Entity Reference
Some characters have a special meaning in XML.
For example, If we use a character like ‘<‘ in our XML element, the XML parser will take it as a starting of a new
element and generate an error.
- <check>if age < 18 </check>
It will generate an XML error:
To avoid this error, replace the “<” character with an entity reference:
There are 5 predefined entity references in XML:
Note: Only the characters “<” and “&” are strictly illegal in XML. The greater than character is legal, but it is a
good habit to replace it.
Rule 7: Adding comments in XML is an easy task
It does not seem to be a rule but make sure that no tag of yours turns out to be interpreted as a comment the
XML parser. You can add comments to XML by using syntax:
Rule 8: XML saves your white spaces
Unlike HTML, XML preserves the white spaces added by you in it’s elements or other things.
- Hello, my name is Ayush
HTML: Hello, my name is Ayush
XML: Hello, my name is Ayush
How to name your XML tags?
- Names can contain letters, numbers and characters
- They should not start with a number or punctuation character
- Don’t start the names by XML, xml etc
- They should not contain spaces
- No words are reserved, so you can use any word