Home  >  Article  >  Backend Development  >  XML Guide—XML Syntax

XML Guide—XML Syntax

黄舟
黄舟Original
2017-02-11 15:31:151679browse


The syntax rules of XML are simple and strict, making it very easy to learn and use.
Because of this, it is relatively easy to write software that reads and manipulates XML.



An example of an XML document
XML documents use self-describing and simple syntax.

<?xml version="1.0" encoding="ISO-8859-1"?> 
<note> 
<to>Lin</to> 
<from>Ordm</from> 
<heading>Reminder</heading> 
<body>Don&#39;t forget me this weekend!</body> 
</note>


Line 1 of the document: XML declaration - defines the version of the XML standard that this document conforms to, in this case version 1.0 of the standard, using ISO-8859 -1 (Latin-1/West European) character set.
The 2nd line of the document is the root element (just like saying "this document is a note"):
8197c7d9a62aa70b2f07974ee724bef2


The 3rd--6th line of the document Describes the four child nodes of the root element (to, from, heading, and body):

<to>Lin</to> 
<from>Ordm</from> 
<heading>Reminder</heading> 
<body>Don&#39;t forget me this weekend!</body>


The last line of the document is the end of the root element:
9cdbc43ffb8d51970a9118e67b3fe241

Can you tell from this document that this is the note Ordm left for Lin? Can we not admit that XML is a beautiful self-describing language?

All XML documents must have a closing tag
In XML documents, it is illegal to ignore the closing tag.
In HTML documents, some elements may not have closing tags. The following code is completely legal in HTML:

<p>This is a paragraph 
<p>This is another paragraph


However, there must be a closing tag in the XML document, like the following example:

<p>This is a paragraph</p> 
<p>This is another paragraph</p>


Note: You may have noticed that the first line in the above example does not have an end tag. This is not a mistake. Because the XML declaration is not part of the XML document, it is not an XML element, and there should be no closing tag.


XML tags are case-sensitive
This is different from HTML, XML tags are case-sensitive.
In XML, the tag f8ed5fdd9e04cae1b683dd2635e1e6f2 and the tag fabba62f13d4f8f2d3adb807e335e152 are two different tags.
Therefore, the capitalization of the opening tag and the closing tag in the XML document must be consistent.

<Message>This is incorrect</message>   //错误的 
<message>This is correct</message>     //正确的

All XML elements must be reasonably included
Incorrect nested inclusions are not allowed in XML.
In HTML, some incorrect inclusions are allowed. For example, the following code can be parsed by the browser:

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

All elements in XML must have correct nested inclusions, the above code It should be written like this:

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

All XML documents must have a root element
The first element in the XML document is the root element.
All XML documents must contain a single tag to define, and all other elements must be nested in pairs within the root element. XML documents have and can only have one root element.
All elements can have child elements, and the child elements must be correctly nested in the parent element. The following code can illustrate it vividly:

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

Attribute values ​​must use quotation marks ""
In XML, the attribute value of an element without quotation marks is illegal.
Like HTML, XML elements can also have attributes. Attributes of XML elements appear in name/value pairs. The XML syntax specification requires that XML element attribute values ​​must be quoted. Look at the two examples below, the first is wrong and the second is right.

 
 
<to>Lin</to> 
<from>Ordm</from> 
<heading>Reminder</heading> 
<body>Don&#39;t forget me this weekend!</body> 
<?xml version="1.0" encoding="ISO-8859-1"?> 
<note date="12/11/99"> 
<to>Tove</to> 
<from>Jani</from> 
<heading>Reminder</heading> 
<body>Don&#39;t forget me this weekend!</body> 
</note>

The error in the first document is that the attribute value is not quoted.
The correct way of writing is: date="12/11/99". The incorrect way of writing: date=12/11/99.

Using XML, whitespace will be preserved
In XML documents , the whitespace will not be automatically removed by the parser.
This is different from HTML. In HTML, a sentence like this:
"Hello my name is Ordm" will be displayed as: "Hello my name is Ordm",
because the HTML parser will automatically remove the blank parts of the sentence.

Using XML, CR / LF are converted to LF
Using XML, new lines are always marked as LF (Line Feed, line feed).
Do you know what a typewriter is? Haha, a typewriter is a specialized typing machine used in the last century. ^&^
When you finish typing a line of words on a typewriter, you usually have to move the typehead to the left end of the paper.
In Windows applications, new lines in text are usually identified as CR LF (carriage return, line feed, carriage return, line feed). In Unix applications, new lines are usually identified as LF. There are also applications that only use CR to represent a new line.

Comments in XML
The syntax of comments in XML is basically the same as that in HTML.
cf2a8215cfeca7c74714a9990fb8e262

There is nothing special about XML
There is really nothing special about XML. It's just plain text held together by angle brackets.
Software that edits ordinary text can also edit XML documents.
However, in an application that supports XML, XML tags often correspond to special operations. Some tags may be visible, while some tags may not be displayed without any special operations.          

The above is the XML guide - XML ​​syntax content. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn