Latest web development tutorials

XML Syntax

XML syntax rules are simple and very logical. These rules are easy to learn, and very easy to use.


All XML elements must have a closing tag

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

<p>This is a paragraph.
<br>

In XML, omit closing tag is illegal. All elementsmust have a closing tag:

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

Note: From the above examples, you might have noticed that the XML declaration did not close the tab.This is not an error. Declaration is not part of the XML document itself, it has no closing tag.


XML tags are case sensitive

XML tags are case sensitive. Tag <Letter> tag <letter> is different.

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

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

Note: Opening and closing tags are commonly referred to as the start and end tags.Whether you like what terms, their concepts are the same.


XML must be properly nested

In HTML, you often see are not properly nested elements:

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

In XML, all elementsmust be properly nested within each other:

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

In the example above, the correct meaning is nested: Because <i> element within <b> element to open, then it must be closed in the <b> element.


XML document must have a root element

XML document must have aelement is the parentof all other elements. This elementis called the root element.

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


XML attribute values ​​must be quoted

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

In XML, XML attribute values ​​must be quoted.

Please study the following two XML documents. The first one is incorrect, 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>

Error in the first document is, note the date property element is not quoted.


Entity reference

In XML, some characters have special significance.

If you put the character "<" on the XML element, an error occurs because the parser will use it as the beginning of a new element.

This will generate an XML error:

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

To avoid this error, please useentity references instead of "<" character:

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

In XML, there are 5 predefined entity references:

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

Note: In XML, only the characters "<" and "&" is indeed illegal.Greater than sign is legal, but using entity references instead it is a good habit.


XML comments in

Write a comment in XML syntax and HTML syntax is very similar.

<-! This is a comment ->


In XML, the space will be reserved

HTML will cut more than one consecutive space characters (merged) into one:

HTML: Hello Tove
Output: Hello Tove

In XML, a document spaces will not be deleted.


XML is stored in LF line feed

In Windows applications, a new line is usually used to store a pair of characters: carriage return (CR) and line feed (LF).

In Unix and Mac OSX, use LF to store a new line.

In the old Mac system, use CR to store the new row.

XML is stored in LF line feed.