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.
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, it is not difficult to create software that can read and manipulate XML An example of an XML document
XML uses a simple syntax that is self-describing #.
##
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
The first line in this document - the XML declaration - defines the version of XML and the character encoding used in the document. In this case, the XML 1.0 specification is followed. , and uses the ISO-8859-1
character set. The following line describes the root element of the document (as if to say: "This document is a sticky note"):
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't forget me this weekend!</body>
The last line defines the root element End of element:
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
So the tag must be opened and closed with the same case:
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:
This text is bold and italic
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!