XML Syntax and Rules

There are few rules and structure that you need follow while you design or create your XML document. It’s easy to learn them and use into your application.

<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<name>Apple iPhone 8S</name>
<color>Red</color>
<price>$950.00</price>
<discount>$30.00</discount>
</product>
</products>

You should notice in above example, there are two kind of details.

  • Markup :     <name>
  • Values: Apple iPhone 8S

Rule 1: Root Element in XML and Declaration

It is necessary that you can use root element in your XML document. Without root element, you can’t create any XML document or define any other elements within a XML document. Main rule about root element is that, you can use only one root element in your XML document. This root element must be on top of the page and it’s also considered as parent of all other elements in your XML document.

<root>
Your all other elements for XML document
</root>

Let’s look into one example to understand

<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<name>Sony Xperia R1 Dual</name>
<color>Silver</color>
<price>$650.00</price>
<discount>$20.00</discount>
</product>
<product>
<name>Apple iPhone 8S</name>
<color>Red</color>
<price>$950.00</price>
<discount>$30.00</discount>
</product>
</products>

In above example, <products> is root element. You can also say its root “Node”. Under root element, there are child elements like <product>, <name>, <color>, <price> and <discount>

Rule 2: Prelog in XML

In every XML document, there is one line on top of the document that is called as “Prelog”. It is not compulsory but you should add Prelog before you start root element in XML document. But, it is mandatory to add on very first line if you add Prelog in document. You cannot add Prelog somewhere else like in middle or bottom of the document.

<?xml version="1.0" encoding="UTF-8"?>

This define that the document is XML document. It may happen that XML document contains some other characters like Spanish, French or Swedish so, you must define “UTF-8” value in encoding parameter in Prelog. Otherwise, your application will not be able to access or read XML document. When you create XML document by using some editor, they may automatically add it in encoding parameter, but if you use notepad or wordpad, you must specify that.

Most of all editors or frameworks are enough smart now a days and they add that as default encoding parameter while you create document like ASPX, XML, HTML, CSS, JavaScript, SQL, or PHP.

Rule 3: Closing Tag

When there is a start, there must be end or close. Same way, we must have to add closing XML tag in XML document for any opening XML tag. If there is no closing tag for any element, then you will face compilation or access error in your application while reading that document.

Look into following example;

This define that the document is XML document. It may happen that XML document contains some other characters like Spanish, French or Swedish so, you must define “UTF-8” value in encoding parameter in Prelog. Otherwise, your application will not be able to access or read XML document. When you create XML document by using some editor, they may automatically add it in encoding parameter, but if you use notepad or wordpad, you must specify that.

Most of all editors or frameworks are enough smart now a days and they add that as default encoding parameter while you create document like ASPX, XML, HTML, CSS, JavaScript, SQL, or PHP.

<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<name>Sony Xperia R1 Dual</name>
<color>Silver</color>
<price>$650.00</price>
<discount>$20.00</discount>
</product>
</products>

Every XML element has closing tag. For example, </products> has closing tag </product> and <name> has a closing tag </name> . You just need to use “/” (slash) before your tag name, that’s all what closing tag is about.

Rule 4: Case Sensitive

Yes, all elements or tags which you use in your XML document, they are case sensitive. You cannot specify capital and small letters for your tag. Either you can use capital or small letter for your tag. For example, <product> and <Product> are not same, they have difference with their case.

Example:
<name>Sony Xperia R1 Dual</name>          =       Correct
<Name>Sony Xperia R1 Dual</name>          =       Incorrect
In short, starting and closing tags must be same with their case/capitalization.

Rule 5: Hierarchical Elements

It is must that your XML structure follows proper hierarchy or nested with all elements or tags. For example, whatever tags or element you start, you must close them before you add tags for other parent elements.

<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<name>Sony Xperia R1 Dual</name>
<color>Silver</color>
<price>$650.00</price>
<discount>$20.00</discount>
</product>
</products>

In above example, you can see <products> and </products> are starting and closing to define one product details. Now, in that you cannot start element inside both of them and close it out side somewhere in XML.

Let’s look into example to understand it clear.

Correct Example:

<products>
<product><name>Sony Xperia R1 Dual</name></product>
</products>

Incorrect Example:

<products>
<product><name>Sony Xperia R1 Dual</product></name>
</products>

Note: It means, you must define proper hierarchical order for starting and closing your tags in XML document. For example, <name> tag must be closed before closing of its parent tag <product>

Rule 6: Attributes and their values

Attributes are kind of pair with name and its value in XML elements. You can define your own name and its values as well.

Example 1:

<products>
<product>
<name color=”white” price=”$650.00” discount=”$20.00”>Sony Xperia R1 Dual</name>
</product>
</products>

Example 2:

<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<name>Sony Xperia R1 Dual</name>
<color>Silver</color>
<price>$650.00</price>
<discount>$20.00</discount>
</product>
</products>

In first example, we created some attributes with pair of name and values. We adjust color, price and discount as attribute and define their values as well.

Ultimately, both example are same but they are in different structure only.

Rule 7: Special Characters in XML OR References

Reference characters are always start with & ( Ampersand) and end with ; (Semicolon). For special character or references, we always need to handle in different way. For example “<” and “>”. You cannot use as values for tag and name because this characters are used to create XML elements so they have special meaning in XML document.

For Example, <name>Sony < Xperia R1 Dual</name>       =     Error or Incorrect

This will give error to read XML document. To avoid error, we must have to use special characters in different way like following.

For Example, <name>Sony &lt; Xperia R1 Dual</name>     =     Correct

Following references will help you to understand special characters in XML document

Valid Not Valid Meaning Example Result
&gt; > Greater Than <price>100 &gt; 90</price> 100 > 90
&lt; < Less Than <price>90 &lt; 100</price> 90 < 100
&apos; ' Apostrophe <price>It&apos;s</price> It’s
&quot; " Quotes <price>&quot;Hello&quot;</price> “Hello”
&amp; & Ampersand <price>XML &amp; HTML</price> XML & HTML

Rule 8: Comments in XML

Comments is values in XML document which doesn’t affect to read XML document or display in web page. Comments syntax is same in XML and HTML.

Example:

<!—Your comment should be here →        =     Correct

<!—Your—comment should be here →      =     Incorrect

Note: You can use multiple dashes in comment value, its considered as special symbol to write XML comment in document.

Rule 9: Blank Values or White Spaces in XML

It doesn’t like HTML, XML always display white space if you add into value. For example, if you add multiple white space in XML, XML doesn’t truncate or trim it automatically, it display as it is. While in HTML, it automatically truncated or trimmed.

Lets look into below example with XML and HTML for white space.

XML HTML
Hello      World Hello World

Summary:
XML is structured language so you must follow above syntax and rules to create your XML document proper without any error.