<p>This is a paragraph.<br>
In XML, it is illegal to omit the closing tag. All elements must have a closing tag:
<?xml version="1.0" encoding="UTF-8" ?>
<p>This is a paragraph.</p>
Note: From the above example, you may have noticed that the declaration in the first line of the XML 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. The tag <Message> is different from the tag <message>.
Opening and closing tags must be written using the same case:
<Message>This is incorrect</message><message>This is correct</message>
Note: Opening and closing tags are often called start and end tags. No matter which term you prefer, the concept is the same.
In HTML, it's common to see elements that are not nested correctly:
<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 example above, correct nesting means: since the <i> element is opened within a <b> element, it must be closed within a <b> element.
An XML document must have one element that is the parent of all other elements. This element is called the root element .
<root><child><subchild>.....</subchild></child></root>
Like HTML, XML elements can have attributes (name/value pairs).
In XML, XML attribute values must be quoted.
Please study the following two XML documents. The first one is wrong, the second one is right:
<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.
In XML, some characters have special meaning.
If you put the character "<" inside an XML element, an error occurs because the parser treats it as the beginning of a new element.
This will generate an XML error:
<message>if salary < 1000 then</message>
To avoid this error, use an entity reference instead of the "<" character:
<message>if salary < 1000 then</message>
In XML, there are 5 predefined entity references:
< | < | less than |
> | > | greater than |
& | & | ampersand |
' | ' | apostrophe |
" | " | quotation mark |
Note: In XML, only the characters "<" and "&" are indeed illegal. The greater than sign is legal, but it is a good practice to replace it with an entity reference.
The syntax for writing comments in XML is very similar to that of HTML.
<!-- This is a comment -->
Tip: You can also learn about the "HTML<!--...-->comment tag".
HTML will trim (merge) multiple consecutive space characters into one:
HTML: | Hello Tove |
Output: | Hello Tove |
In XML, whitespace in the document is not trimmed.
In Windows applications, newlines are usually stored as a pair of characters: carriage return (CR) and line feed (LF).
In Unix and Mac OSX, use LF to store new lines.
On older Mac systems, CR is used to store new lines.
XML stores line breaks in LF.
The above are the grammatical rules that you need to pay attention to when using XML. Follow these rules to write a correct XML document!