What are the syntax rules for XML?

0

The rules of an XML document are logic and simple. The XML syntax is easy to learn and utilize.

All the XML elements must have an ending tag.

In HTML, you can find many elements having an ending tag.

 

<p>This is a paragraph

<p>this is another paragraph

in XML, the ending tags are mandatory. All the elements must have an ending tag:

<p>This is a paragraph</p>

<p>This is another paragraph</p>

 

Attention! You probably noticed in the previous example that the XML declaration has no ending tag. It is not an error, as the declaration is not a part of the document itself, so it doesn’t need an ending tag.

 

The XML tag is case sensitive. For example, the tag <Yourself> has a different effect than the tag <yourself>

the beginning and ending tags must be written the same, with capital letters, or with normal letters:

<Message>This is a wrong example</message>

<message>This is the right example</message>

 

Attention! “The closing and ending tags” are commonly addressed to “start and stop tags.” You can use both variants, it is the same thing.

The XML elements must be correctly combined

 

In XML, you can often see elements that are not correctly combined, just like in the next example:

<b><i>This is a bold and italic font</b></i>

The next example is correct:

<b><i>This is a bold and italic font</i></b>

in the last example, “correctly combined” means that the element <i> was opened inside the element <b>, and he is also closed inside the element <b>.

The XML documents need a root.

 

The XML documents must contain an element that identifies the file for all the documents used. It is also called a root element:

<root>

<row>

<subrow>…..</subrow>

</row>

</root>

the values of the XML attributes must be quoted.

The XML elements could have pairs attributed, just like in HTML.

In XML, the value of an attribute must always be quoted like in the next example:

<message data=”14/05/2011″>

<to>Anne</to>

<sender>Michael</sender>

</message>

 

the references of the entities

some characters have a special signification in XML. If you introduce a character like “>” in an XML element, an error will be produced, as the XML interpreter will consider it as the beginning of an element.

To resolve this problem, you will have to use the entities associated with the characters. We can find 5 special characters in XML:

&lt;      <          smaller

&gt;     >          greater

&amp;             &         ampersand

&apos;             ‘           simple quotes

&quot;             “          double quotes

 

Attention! Only the characters “<” and “&” are forbidden in XML. The character “>” can be used, but it is not a habit for the programmers to use it.

 

Comments in XML

The writing syntax of comments is similar with the one from HTML:

<!– This is a comment –>

With XML, the spaces between the words are preserved.

HTML reduces the empty spaces to one space, while XML preserves every space.

XML considers every new line as a LF (line feed). As a conclusion, if you put two spaces in an XML declaration, the two spaces will appear in the main document.

Leave a Reply

Your email address will not be published. Required fields are marked *