Home >Backend Development >XML/RSS Tutorial >XML development basics-detailed explanation of XML syntax rules

XML development basics-detailed explanation of XML syntax rules

黄舟
黄舟Original
2017-03-25 17:09:131950browse

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 closing tags

In HTML, you often see elements without closing tags:

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

In XML, it is illegal to omit the closing tag . All elements must have a closing tag:

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

Comment: You may have noticed 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

XML elements are defined using XML tags.

XML tags are case-sensitive. In XML, the tag is different from the tag .

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

<Message>这是错误的。</message>
<message>正式正确的。</message>

Note: Opening tags and closing tags are often referred to as 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 elements must be correctly nested with each other Nesting:

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

In the above example, correct nesting means: since the element is opened within the element, it must be closed within the element .

The XML document must have a root element

The XML document 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>

Attributes of XMLValues ​​must be quoted

Similar to HTML, XML can also have attributes (name/value pairs).

In XML, XML attribute values ​​must be enclosed in quotation marks. Please study the two XML documents below. The first one is wrong, the second one is correct:

<note date=08/08/2008>
<to>George</to>
<from>John</from>
</note>
<note date="08/08/2008">
<to>George</to>
<from>John</from>
</note>

Entity reference

In XML, some characters have special meanings.

If you put the character "

This will generate an XML error:

<message>if salary < 1000 then</message>

To avoid this error, use an entity reference instead of the "

<message>if salary < 1000 then</message>

In XML, there are 5 Predefined entity references:

XML development basics-detailed explanation of XML syntax rules

Note: In XML, only the characters "

Comments in XML

The syntax for writing comments in XML is very similar to the syntax of HTML:

<!-- This is a comment -->
在XML中,空格会被保留
HTML会把多个连续的空格字符裁减为一个:
HTML: Hello      my name is Tove

Output: Hello my name is Tove.

In XML, spaces in the document will not be truncated.

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). This pair of characters has similarities to the action of a typewriter setting a new line. In Unix applications, new lines are stored as LF characters. Macintosh applications use CR to store new lines.

The above is the detailed content of XML development basics-detailed explanation of XML syntax rules. 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