Home  >  Article  >  Backend Development  >  Detailed introduction to the use and learning of xml syntax

Detailed introduction to the use and learning of xml syntax

黄舟
黄舟Original
2017-03-30 13:46:151358browse

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 a 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 (like saying "This document is a note"):

<note>

The 3rd--6th line of the document describes the four elements of the root element. Child nodes (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:

</note>

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

---------------------------------------- ---------------------------------------

All XML documents must There is a closing tag
In an XML document, it is illegal to omit 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, The first line in the above example does not have a closing 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 not the same as 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 includes 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>

In XML, all elements 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>

-------- -------------------------------------------------- -----------------------

AttributeThe value must be quoted ""
In XML, the element Attribute values ​​without quotes are 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>
</note>


Tove
Jani
Reminder
Don't forget me this weekend!
</note>

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

------------- -------------------------------------------------- ------------------

Using XML, whitespace will be preserved
In XML documents, 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 Using XML for LF
, new lines are always marked as LF (Line Feed, line feed).

The above is the detailed content of Detailed introduction to the use and learning of xml syntax. For more information, please follow other related articles on the PHP Chinese website!

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