XML technical m...login
XML technical manual
author:php.cn  update time:2022-04-14 15:57:53

XML syntax



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


All XML elements must have a closing tag

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

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

In XML, it is illegal to omit the closing tag. 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>.

Opening and closing tags must be written using the same case:

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

Note: Opening tags and closing tags are often called opening tags and closing tags. No matter which term you prefer, the concept is the same.


XML must be nested correctly

In HTML, it is common to see elements that are not nested correctly:

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

In XML, all elementsmustbe nested correctly within each other:

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

In the above example, correct nesting means: due to The <i> element is opened within the <b> element, then it must be closed within the <b> element.


XML documents must have a root element

XML documents must have one element that is the parent element of all other elements. This element is called the root element.

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


XML attribute values ​​must be quoted

Like HTML, XML elements can also 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 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 attribute in the note element is not quoted.


Entity reference

In XML, some characters have special meanings.

If you put the character "<" inside an XML element, an error will occur because the parser will treat 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 Use

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 "&" is indeed illegal. The greater than sign is legal, but it is a good practice to replace it with an entity reference

Comments in XML

Writing comments in XML. The syntax is very similar to the syntax of HTML. Keep

HTML will cut (merge) multiple consecutive space characters into one:

HTML:

Hello                                                                            

#Output:Hello Tove

In XML, spaces in the document are not trimmed.


XML stores newlines as LF

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

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

In older Mac systems, CR was used to store new lines.

XML stores line breaks in LF.