XML Syntax Rules
- Previous Page XML Tree Structure
- Next Page XML Elements
The syntax rules of XML are simple and logical. These rules are easy to learn and easy to use.
An XML document must have a root element
An XML document must contain aroot element, which is the parent of all other elementsparent element:
<root> <child> <subchild>.....</subchild> </child> </root>
In this example,<note>
is the root element:
<?xml version="1.0" encoding="UTF-8"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
XML Prolog (XML Prolog)
This line is called the XML Prologue:
<?xml version="1.0" encoding="UTF-8"?>
The XML prolog is optional. If it exists, it must be on the first line of the document.
XML documents can contain international characters, such as Norwegian øæå or French êèé.
To avoid errors, you should specify the encoding used and save the XML file as UTF-8.
UTF-8 is the default character encoding for XML documents.
You can find more information on ourCharacter Set Tutoriallearn about character encoding here.
Tip:UTF-8 is the default encoding for HTML5, CSS, JavaScript, PHP, and SQL.
All XML elements must have an end tag
In XML, omitting the end tag is illegal. All elements mustIn XML, all elementsHave an end tag:
<p>This is a paragraph.</p> <br />
Note:XML prolog does not have an end tag! This is not an error. The prolog is not part of the XML itself. It is not an XML element and does not require a closing tag.
XML tags are case-sensitive
XML tags are case-sensitive. The tag <Letter> is different from the tag <letter>.
Start and end tags must use the same case:
<message>This is correct</message>
Comment:Opening and closing tags are usually referred to as start tags and end tags. Regardless of which term you prefer, the concept is the same.
XML 元素必须正确嵌套
XML elements must be properly nested
In HTML, you may see elements that are not properly nested:
<b><i>This text is bold and italic</b></i>In XML, all elementsMust
Correctly nested
<b><i>This text is bold and italic</i></b>
In the above example, the correct nesting means: since the <i> element is opened within the <b> element, it must be closed within the <b> element.
XML attribute values must be quoted
Like HTML, XML also has attributes (name/value pairs).
<note date="12/11/2007"> <to>George</to> <from>John</from> </note>
Entity references
In XML, some characters have special meanings.
If you place the character "<" inside an XML element, an error will occur because the parser will treat it as the start of a new element.
This will cause an XML error:
<message>if salary < 1000 then</message>
To avoid this error, please useEntity referencesto replace the "<" character:
<message>if salary < 1000 then</message>
In XML, there are 5 predefined entity references:
< | < | Less than |
> | > | Greater than |
& | & | And |
' | ' | ' |
" | " | " |
Comment:In XML, only the characters "<" and "&"" are indeed illegal. The character '>" is legal, but it is a good habit to use it instead.
XML comments
The syntax for writing comments in XML is very similar to that in HTML:
<!-- This is a comment -->
Two dashes are not allowed in the middle of a comment:
<!-- This is an invalid -- comment -->
XML preserves whitespace
HTML will reduce multiple consecutive space characters to one, whereas in XML, spaces within the document are not deleted:
XML: Hello George HTML: Hello George
XML stores new lines as LF
In Windows applications, a newline is typically stored as a pair of characters: the carriage return (CR) and the newline (LF). This pair of characters is similar to the action of typing a new line on a typewriter.
Unix and Mac OSX use LF.
Old Mac systems use CR.
XML stores new lines as LF.
Well-formed XML
An XML document that conforms to the above grammar rules is called a 'well-formed' XML document.
- Previous Page XML Tree Structure
- Next Page XML Elements