Home >Backend Development >XML/RSS Tutorial >XML Tutorial - Learn the details of XML syntax with an example

XML Tutorial - Learn the details of XML syntax with an example

黄舟
黄舟Original
2017-03-14 16:12:571403browse

The syntax rules of

XML are both very simple and very strict. These rules are easy to learn and easy to use. Because of this, creating software that can read and manipulate XML is not difficult. An example of an XML document XML uses a simple syntax that is self-describing. 233cfb2a71a70868177eb04d24a40069
The next four lines describe the four child elements of the root element (to, from, heading, and body):

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don&#39;t forget me this weekend!</body>

The last line defines the root element End of element:
9cdbc43ffb8d51970a9118e67b3fe241
We can see that this XML document contains a note left by Jani to Tove. Now, you should agree with us that XML has perfect Self-describing properties. All elements must have a closing tag

When using XML, it is illegal to omit the closing tag. In HTML, some elements do not necessarily have closing tags. In HTML the following code is legal:

<p>This is a paragraph

<p>This is another paragraph

In XML, all elements must have a closing tag:

<p>This is a paragraph</p>

<p>This is another paragraph</p>


Note: You may have noticed from the above example that the XML declaration does not have a closing tag. This is not an error. Declarations are not part of XML itself. It is not an XML element and does not require a closing tag. XML tags are case-sensitive
Unlike HTML, XML tags are case-sensitive. In XML, the tag f8ed5fdd9e04cae1b683dd2635e1e6f2 and the tag fabba62f13d4f8f2d3adb807e335e152 are different.
So the tag must be opened and closed with the same case:
58613a913f649e803df57c76e6a932e4This is wrong. dc97c94abba54b3574a1f2a9fcb1679b 761b5f615852937c78510c6ee32be47aThis is correct. dc97c94abba54b3574a1f2a9fcb1679b XML must be nested correctly

Incorrect tag nesting has no meaning to XML. In HTML, certain elements can be nested incorrectly within each other, like this:

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

In XML, all elements must be correctly nested Nested within each other, like this:
a4b561c25d9afb9ac8dc4d70affff4195a8028ccc7a7e27417bff9f05adf5932This text is bold and italic72ac96585ae54b6ae11f849d2649d9e60d36329ec37a2cc24d42c7229b69747a XML documents must have a root element

All XML must contain a single tag pair that defines the root element. All other elements must be inside this root element.
All elements can have child elements. Child elements must be properly nested within their parent elements:

<root>

  <child>

    <subchild>.....</subchild>

  </child>

</root> 





XML的属性值须加引号


In XML, it is illegal to omit quotes around an attribute value. Similar to HTML, XML can also have attributes (name/value pairs). In XML, XML attribute values ​​must be quoted. Please study the two XML documents below. The first is wrong, the second is correct:

<?xml version="1.0" encoding="ISO-8859-1"?>

<note date=12/11/2002>

<to>Tove</to>

<from>Jani</from>

</note>
<?xml version="1.0" encoding="ISO-8859-1"?>

<note date="12/11/2002">

<to>Tove</to>

<from>Jani</from>

</note>

In the first document, the date attribute is not quoted. This is correct: date="12/11/2002". This is wrong: date=12/11/2002. In XML, whitespace is preserved.

In XML, spaces will not be truncated. This is different from HTML. In HTML, a sentence like this:

Hello              my name is Tove,

will be displayed like this:

Hello my name is Tove,

This is because HTML will treat multiple consecutive The space characters are trimmed to one. In XML, CR/LF will be converted to LF

In XML, a new line (i.e. line feed) is stored as LF (Line Feed, line feed). Are you familiar with typewriters? A typewriter is a mechanical device used in the last century to create printed documents. :-)
After you type a line of text on the typewriter, you need to manually move the printing carriage to the left margin position and manually feed a line.
In Windows applications, new lines are usually stored as a pair of characters: carriage return (CR) and line feed (LF). This character pair is similar to the action of setting a new line on a typewriter. In Unix applications, new lines are usually stored as LF characters. And Macintosh applications only use CR characters to store new lines. Comments in XML
The syntax for writing comments in XML is similar to the syntax of HTML:

<!-- This is a comment -->





XML没什么特殊之处


XML没什么特殊之处。它只是带有被括在角形括号中的标签的纯文本而已。
可处理纯文本文件的软件也可以处理XML。在一个简单的文本编辑器中,XML标签也可被显示出来,不会被特殊地对待。
在可识别XML的(XML-aware)应用程序中,XML标签会被专门处理。根据不同的应用程序种类,这些标签也许会/也许不会被看到,又或许拥有某种功能意义。                                                

The above is the detailed content of XML Tutorial - Learn the details of XML syntax with an example. 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