XML Syntax Rules
The syntax rules for XML are simple and very logical.
These rules are easy to learn and easy to use.
XML Declaration
The XML declaration is an optional part of the file; if present, it must be placed on the first line of the document, as follows:
The example above 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 documents must have a root element
XML must contain a root element, which is the parent of all other elements. For example, in the following instance, root is the root element:
<
root
<
child
<
subchild.....
subchild
child
root
In the following example, note is the root element:
<
note
<
to
Tove
to
<
from
Jani
from
<
heading
Reminder
heading
<
bodyDon't forget me this weekend!
body
note
An XML document is made up of elements; each element consists of a start tag, an end tag, and the element content.
property
Elements can have attributes, which provide additional information about the element.
Attributes are placed in the start tag, for example:
John Doe
XML Single Tags
All XML elements generally have a closing tag, but the use of single tags is also allowed.
A single tag is written as follows:
A single tag contains both the start and end tags in one tag, similar to an empty element tag in HTML.
In XML, you can represent a single tag in the following two ways:
Using an empty element tag:
Using a start and end tag with no content:
These two forms 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
Here is a single tag representing a line break.
Single tag with attributes:
This tag represents an image element and contains the src and alt attributes, indicating the image path and alternative text.
Example XML file:
Example
In this example, and are all single tags. tags are used to convey information, while is an empty tag with no child elements.
XML tags are case-sensitive
XML tags are case-sensitive. The tag
must be written with the same case for the opening and closing tags:
这是错误的
这是正确的
**Note:** Opening tags and closing tags are usually called start tags and end tags. Whichever terminology you prefer, the concept is the same.
XML must be properly nested
In HTML, you often see elements that are not properly nested:
This text is bold and italic
In XML, all elements must beMustproperly nested within each other:
This text is bold and italic
In the example above, proper nesting means: since the element is opened within the element, it must be closed within the element.
XML attribute values must be quoted
Similar to HTML, XML elements can also have attributes (name/value pairs).
In XML, attribute values must be quoted.
Study the two XML documents below. The first one is wrong, the second one is correct:
Tove
Jani
Tove
Jani
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 meanings.
If you put 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:
if salary < 1000 then
To avoid this error, useEntity referencesinstead of the \"<\" character:
if salary < 1000 then
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 good practice to replace it with an entity reference.
Comments in XML
The syntax for writing comments in XML is similar to that of HTML.
Whitespace is preserved in XML
HTML trims (merges) multiple consecutive space characters into one:
| HTML: |
Hello Tove |
|---|---|
| Output result: | Hello Tove |
In XML, spaces in the document are not trimmed:
xx xxx
John Doe
29.99
The title content is read and displayed as follows:
xx xxx
XML stores line breaks with LF
In Windows applications, a line break is usually stored as a pair of characters: a carriage return (CR) and a line feed (LF).
In Unix and Mac OSX, LF is used to store a new line.
In older Mac systems, CR is used to store a new line.
XML stores line breaks with LF.