Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XML syntax


May 27, 2021 XML


Table of contents


XML syntax rules

The purpose of this section is to let you understand the rules under which syntax in XML is based and to avoid errors in writing XML documents.

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


All XML elements must have a close label

In HTML, some elements do not have to have a close label:

<p>This is a paragraph.
<br>

In XML, it is illegal to omit a closed label. All elements must have a close label:

<?xml version="1.0" encoding="UTF-8" ?>
<p>This is a paragraph.</p>

Note: From the example above, you may have noticed that the first line of XML declares that the label is not closed, that this is not an error, that the declaration is not part of the XML document itself, and that it does not close the label.


XML tags are case sensitive

XML tags are case sensitive. The label is different from the label slt;message.

You must use the same case to write open and close labels:

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

Note: Opening and closing labels are often referred to as start and end labels. No matter what term you like, they all have the same concept.


XML must be nested correctly

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

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

In XML, all elements must nest correctly with each other:

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

In the example above, the correct nesting means that since the element is opened within the element, it must be closed within the element.


The XML document must have a root element

The XML document must have an element that is the parent of all other elements. This element is called the root element.

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


XML property values must be quoted

Similar to HTML, XML elements can also have properties (pairs of names/values).

In XML, the property values of XML must be quoted.

Look at the following two XML documents. The first is wrong and the second is correct:

<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 property in the note element is not quoted.


The entity reference

In XML, some characters have a special meaning.

An error occurs if you place the character "lt;" in an XML element because the parser treats it as the beginning of a new element.

This results in an XML error:

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

To avoid this error, replace the character with an entity reference instead of the word ""

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

In XML, there are five predefined entity references:

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

Note: In XML, only the characters """"" A larger sign is legal, but it is a good practice to replace it with an entity reference.


Comments in XML

The syntax of writing comments in XML is similar to the syntax of HTML.

<!-- This is a comment -->

Tip: You can also learn about "HTML slt;!--...-- and Comment Labels".


In XML, spaces are preserved

HTML cuts (merges) multiple consecutive space characters into one:

HTML: Hello Tove
Output: Hello Tove

In XML, spaces in a document are not deleted.


XML stores line-overs in LF

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

In Unix and Mac OSX, LF is used to store new rows.

In older Mac systems, CR is used to store new rows.

XML stores line-overs in LF.

These are the syntax rules you need to be aware of when using XML, and write the correct XML documentation according to those rules!