# 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:

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

Example:

<?xml version="1.0" encoding="UTF-8"?> <note> <to>Sapna</to> <from>Tom</from> <heading>Meeting</heading> <body>At 11 AM on Monday morning.</body> </note>

Explanation:

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

### XML Prolog:

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

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:

<p>Hello!</p> <br />

### 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:

<info>I am the information.</info>

### XML Elements Must be Properly Nested:

Improperly nested elements are possible in HTML.

Example:

<b><i>Hello!</b></i>

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:

<b><i>Hello!</i></b>

### 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:

<?xml version="1.0" encoding="UTF-8"?> <note date="4/06/2020"> <to>Sapna</to> <from>Tom</from> <heading>Meeting</heading> <body>At 11 AM on Monday morning.</body> </note>

### 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:

<info>id < 10</info>

Explanation:

An error will be generated.

Example:

<info>id &lt; 10</info>

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:

 < < less than > > greater than & & ampersand ' apostrophe " “ quotation mark

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

Example:

<!-- I am a comment -->

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

Example:

<!-- Invalid -- comment -->

### 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.