XML syntax
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 a closing tag
In HTML, some elements do not have to have a closing tag:
<br>
In XML, it is illegal to omit the closing tag. All elements must have a closing tag:
<br />
Note: From the above example, you may have noticed that the XML declaration does not have a closing tag. This is not an error. The declaration is not part of the XML document itself, it does not have a closing tag.
XML tags are case-sensitive
XML tags are case-sensitive. The tag <Letter> is different from the tag <letter>.
Opening and closing tags must be written using the same case:
<message>This is correct< ;/message>
Note: Opening tags and closing tags are often called 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:
In XML, all elementsmustbe nested correctly within each other:
In the above example, correct nesting means: due to The <i> element is opened within the <b> element, then it must be closed within the <b> element.
XML documents must have a root element
XML documents must have one element that is the parent element of all other elements. This element is called the root element.
<child>
<subchild>.....</subchild>
</child>
</root>
XML attribute values must be quoted
Like 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 wrong, the second one is correct:
<to>Tove</to>
<from>Jani</from>
</note>
<from>Jani</from>
</note>
Entity referenceIn XML, some characters have special meanings. If you put the character "<" inside an XML element, an error will occur because the parser will treat it as the beginning of a new element. This will generate an XML error:
entity reference instead of the "<" character:
< | less than | |
> | greater than | |
& | ampersand | |
' | apostrophe | |
" | quotation mark |
In XML, only the characters "<" and "&" is indeed illegal. The greater than sign is legal, but it is a good practice to replace it with an entity reference
Comments in XMLWriting comments in XML. The syntax is very similar to the syntax of HTML. Keep
HTML will cut (merge) multiple consecutive space characters into one:
Hello
#Output:In XML, spaces in the document are not trimmed. XML stores newlines as LFIn Windows applications, newlines are usually stored as a pair of characters: a carriage return (CR) and a line feed (LF). In Unix and Mac OSX, use LF to store new lines. In older Mac systems, CR was used to store new lines. XML stores line breaks in LF. |