XML Syntax Rules
The syntax rules of XML are very simple and logical.
These rules are easy to learn and easy to use.
XML Declaration
The XML declaration is an optional part of the file. If it exists, it needs to be placed on the first line of the document, as shown below:
<?xml version="1.0" encoding="utf-8"?>
The above example contains the XML version (version="1.0"), and even the character encoding (encoding="utf-8").
UTF-8 is also the default encoding for HTML5, CSS, JavaScript, PHP, and SQL.
XML Document Must Have a Root Element
XML must contain a root element, which is the parent of all other elements. For example, in the following example, root is the root element:
In the following example, note is the root element:
XML documents are made up of elements. Each element includes a start tag, an end tag, and element content.
Attributes
Elements can contain attributes. Attributes provide additional information about an element.
Attributes are located in the start tag, for example:
<person age="30" gender="male">John Doe</person>
XML Self-closing Tags
All XML elements generally have a closing tag, but the use of self-closing tags is also allowed.
The syntax for a self-closing tag is as follows:
<elementName attribute="value" />
A self-closing tag is one that contains both the start and end tags in a single tag, similar in form to empty element tags in HTML.
In XML, you can express a self-closing tag in the following two ways:
Using an empty element tag:
<exampleTag />
Using a start and end tag, but without any content:
<exampleTag></exampleTag>
These two representations are equivalent; you can choose either one according to personal or project conventions.
In many XML parsers, both forms are interpreted as the same structure.
Example
<lineBreak />
Here<lineBreak />is a self-closing tag, representing a line break operation.
A self-closing tag with attributes:
<image src="logo.png.html" alt="Logo" />
This tag represents an image element. It contains src and alt attributes, which specify the image path and alternative text.
XML File Example:
Example
<root>
<note to="Tove" from="Jani" heading="Reminder" body="Don't forget me this weekend!" />
<emptyElement attribute="value" />
</root>
In this example,<note>and<emptyElement />both are self-closing tags.<note>The purpose of the tag is to convey information, and<emptyElement />is an empty tag without child elements.
XML Tags Are Case Sensitive
XML tags are case sensitive. The tag <Letter> is different from the tag <letter>.
You must use the same case for the opening tag and the closing tag:
<Message>这是错误的</message> <message>这是正确的</message>
Note:Opening tags and closing tags are often called start tags and end tags. Whichever terminology you prefer, the concepts are the same.
XML Must Be Properly Nested
In HTML, you often see elements that are not properly nested:
<b><i>This text is bold and italic</b></i>
In XML, all elementsmustbe properly nested within each other:
<b><i>This text is bold and italic</i></b>
In the example above, properly nested means: since the <i> element is opened inside the <b> element, it must be closed inside the <b> element.
XML Attribute Values Must Be Quoted
Similar to HTML, XML elements can also have attributes (name/value pairs).
In XML, XML attribute values must be quoted.
Please study the following two XML documents. The first one is incorrect, the second one is correct:
<note date=12/11/2007> <to>Tove</to> <from>Jani</from> </note>
<note date="12/11/2007"> <to>Tove</to> <from>Jani</from> </note>
The error in the first document is that the date attribute in the note element is not quoted.
Entity References
In XML, some characters have special meaning.
If you place the character "<" in an XML element, an error will occur because the parser will treat it as the start of a new element.
This will produce an XML error:
<message>if salary < 1000 then</message>
To avoid this error, useentity referenceto replace the "<" character:
<message>if salary < 1000 then</message>
In XML, there are 5 predefined entity references:
| < | < | less than |
| > | > | greater than |
| & | & | ampersand |
| ' | ' | apostrophe |
| " | " | quotation mark |
Note:In XML, only the characters "<" and "&" are truly illegal. The greater-than sign is legal, but it is a good habit to replace it with an entity reference.
Comments in XML
The syntax for writing comments in XML is very similar to that of HTML.
<!-- This is a comment -->
In XML, Spaces Are Preserved
HTML will collapse (merge) multiple consecutive space characters into one:
| HTML: | Hello Tove |
| Output: | Hello Tove |
In XML, spaces in a document are not collapsed:
<bookstore>
<book>
<title>xx xxx</title>
<author>John Doe</author>
<price>29.99</price>
</book>
</bookstore>
The title content is read and displayed as follows:
xx xxx
XML Stores Newlines as LF
In Windows applications, a newline is usually stored as a pair of characters: carriage return (CR) and line feed (LF).
In Unix and Mac OS X, LF is used to store a new line.
In old Mac systems, CR is used to store a new line.
XML stores newlines as LF.
Other Extensions