Easy Tutorial
❮ Xml Summary Xml Tree ❯

XML Syntax Rules


XML's syntax rules are simple and logical. These rules are easy to learn and easy to use.


XML Documents Must Have a Root Element

XML must contain a root element, which is the parent of all other elements. For example, in the following instance, root is the root element:

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

In the following instance, note is the root element:

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

XML Declaration

The XML declaration is an optional part of the file. If present, it needs to be placed on the first line, as shown below:

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

The above example includes the XML version.

UTF-8 is also the default encoding for HTML5, CSS, JavaScript, PHP, and SQL.


All XML Elements Must Have a Closing Tag

In HTML, some elements do not require a closing tag:

<p>This is a paragraph.
<br>

In XML, omitting the closing tag is illegal. All elements must have a closing tag:

<p>This is a paragraph.</p>
<br />

Note: From the above example, you may have noticed that the XML declaration does not have a closing tag. This is not an error. The declaration is not part of the XML document itself; it does not have a closing tag.


XML Tags Are Case Sensitive

XML tags are case sensitive. The tag <Letter> is different from the tag <letter>.

You must use the same case to write the opening and closing tags:

<Message>This is wrong</message>
<message>This is correct</message>

Note: Opening and closing tags are often referred to as start and end tags. Regardless of the terminology you prefer, their concept is the same.


XML Must Be Correctly Nested

In HTML, you often see elements that are not correctly nested:

<b><i>This text is bold and italic</b></i>

In XML, all elements must be properly nested within each other:

<b><i>This text is bold and italic</i></b>

In the above example, proper nesting means that since the <i> element is opened within the <b> element, it must be closed within the <b> element.


XML Attribute Values Must Be Quoted

Similar to HTML, XML elements can also have attributes (name/value pairs).

In XML, attribute values must be quoted.

Study the following two XML documents. The first one is incorrect, and the second one is correct:

&lt;note date=12/11/2007>
<to>Tove</to>
<from>Jani</from>
</note>
<note date="12/11/2007">
<to>Tove</to>
<from>Jani</from>
</note>

The error in the first document is that the date attribute in the note element is not quoted.


Entity References

In XML, certain characters have special meanings.

If you place the character "<" in an XML element, it will cause an error because the parser will interpret it as the start of a new element.

This will cause an XML error:

<message>if salary < 1000 then</message>

To avoid this error, use an entity reference to replace the "<" character:

<message>if salary &lt; 1000 then</message>

In XML, there are five predefined entity references:

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

Note: In XML, only the characters "<" and "&" are truly illegal. The greater-than sign is legal, but using an entity reference for it is a good practice.


Comments in XML

The syntax for writing comments in XML is similar to that in HTML.

<!-- This is a comment -->

In XML, Spaces Are Preserved

HTML trims multiple consecutive space characters into one:

<p>This is a paragraph.</p>

In XML, spaces are preserved:

<p>This is a paragraph.</p>

In XML, spaces within the document are not trimmed.


XML Stores Line Breaks as LF

In Windows applications, line breaks are typically stored as a pair of characters: carriage return (CR) and line feed (LF).

In Unix and Mac OSX, a new line is stored using LF.

In older Mac systems, a new line is stored using CR.

XML stores line breaks as LF.

❮ Xml Summary Xml Tree ❯