XML Syntax

The XML syntax has very simple, logical, easy to learn, and easy to use rules.

XML Syntax Rules:

XML Documents Must Have a Root Element:

The root element is the parent of all other elements and it must be present in every XML document.

Syntax:


  
    .....
  

Example:



  Sapna
  Tom
  Meeting
  At 11 AM on Monday morning.

Explanation:

In the above example, the root element is <note>.

XML Prolog:


Being optional, the XML prolog if exists must come first in the document. The international characters, like Norwegian øæå or French êèé, can also be included in an XML document. The encoding used, however, must be specified or the XML files must be saved as UTF-8, to avoid errors. Along with being the default character encoding for XML documents, UTF-8 is also the default encoding for HTML5, CSS, JavaScript, PHP, and SQL.

All XML Elements Must Have a Closing Tag:

In XML, all elements must have a closing tag and it is not allowed to omit the closing tag. However, there is no closing tag in an XML prolog. Because the prolog is not a part of the XML document. Thus, it is not an error.

Example:

Hello!


XML Tags are Case Sensitive:

Example:

The <Note> tag is different from the <note> tag.

Opening and closing tags or the start and end tags should be written with the same case:

Example:

I am the information.

XML Elements Must be Properly Nested:

Improperly nested elements are possible in HTML.

Example:

Hello!

Only the elements that are properly nested within each other are allowed in XML which means that an element opened inside the first element must be closed inside the first element.

Example:

Hello!

XML Attribute Values Must Always be Quoted:

Similar to HTML, the attributes of the XML elements can have name/value pairs, however, the attribute values must always be quoted in XML elements.

Example:



  Sapna
  Tom
  Meeting
  At 11 AM on Monday morning.

Entity References:

In XML, some characters have a special meaning, i.e., on placing the “<” inside an XML element, an error will be generated because the parser interprets it as the start of a new element.

Example:

id < 10

Explanation:

An error will be generated.

Example:

id < 10

Explanation:

Replacing the “<” character with an entity reference will avoid the error.

Entity references:

In XML, we have five predefined entity references, but only < and & are strictly illegal in XML. It is, however, considered a good habit to replace > with &gt; entity reference. These predefined entity references are:

&lt; < less than
&gt; > greater than
&amp; & ampersand
&apos; apostrophe
&quot; quotation mark

Comments in XML:

Just like HTML, the syntax for writing comments in XML is:

Example:


In the middle of a comment, we cannot place two dashes.

Example:


White-space is Preserved in XML:

Contrary to HTML, multiple white-spaces are not truncated to one single white-space in XML.

Example:

XML:	Hello World
HTML:	Hello World

XML Stores New Line as LF:

A new line is stored as a carriage return and line feed (CR+LF), by Windows applications.

  • Unix and Mac OS X: Stores a new line as LF.
  • Old Mac systems: Stores a new line as CR.
  • XML: Stores a new line as LF.

Well-Formed XML:

Conforming to all the above syntax rules means that an XML document is a “Well Formed” XML document.