The syntax rules of XML are very simple and logical. The rules are easy to learn, and easy to use.
All XML Elements Must Have a Closing Tag
In HTML, elements do not have to have a closing tag:
In XML, it is illegal to omit the closing tag. All elements must have a closing tag:Code:<p>This is a paragraph <p>This is another paragraph
Note: You might have noticed from the previous example that the XML declaration did not have a closing tag. This is not an error. The declaration is not a part of the XML document itself, and it has no closing tag.Code:<p>This is a paragraph</p> <p>This is another paragraph</p>
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 with the same case:
Note: "Opening and closing tags" are often referred to as "Start and end tags". Use whatever you prefer. It is exactly the same thing.Code:<Message>This is incorrect</message> <message>This is correct</message>
XML Elements Must be Properly Nested
In HTML, you might see improperly nested elements:
In XML, all elements must be properly nested within each other:Code:<b><i>This text is bold and italic</b></i>
In the example above, "Properly nested" simply means that since the <i> element is opened inside the <b> element, it must be closed inside the <b> element.Code:<b><i>This text is bold and italic</i></b>
XML Documents Must Have a Root Element
XML documents must contain one element that is the parent of all other elements.
This element is called the root element.
XML Attribute Values Must be QuotedCode:<root> <child> <subchild>.....</subchild> </child> </root>
XML elements can have attributes in name/value pairs just like in HTML.
In XML, the attribute values must always be quoted.
Study the two XML documents below. The first one is incorrect, the second is correct:
Code:<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.Code:<note date="12/11/2007"> <to>Tove</to> <from>Jani</from> </note>
Some characters have a special meaning in XML.
If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element.
This will generate an XML error:
To avoid this error, replace the "<" character with an entity reference:Code:<message>if salary < 1000 then</message>
There are 5 predefined entity references in XML:Code:<message>if salary < 1000 then</message>
Note: Only the characters "<" and "&" are strictly illegal in XML. The greater than character is legal, but it is a good habit to replace it.Code:< < less than > > greater than & & ampersand ' ' apostrophe " " quotation mark
Comments in XML
The syntax for writing comments in XML is similar to that of HTML.
White-space is Preserved in XMLCode:<!-- This is a comment -->
HTML truncates multiple white-space characters to one single white-space:
HTML: Hello Tove
Output: Hello Tove With XML, the white-space in a document is not truncated.
XML Stores New Line as LF
In Windows applications, a new line is normally stored as a pair of characters: carriage return (CR) and line feed (LF). In Unix applications, a new line is normally stored as an LF character. Macintosh applications also use an LF to store a new line.
XML stores a new line as LF.