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, you will often see elements that don't have a closing tag: <p>This is a paragraph <p>This is another paragraph |
<p>This is a paragraph</p> <p>This is another paragraph</p> |
XML Tags are Case Sensitive
XML elements are defined using XML tags.XML tags are case sensitive. With XML, the tag <Letter> is different from the tag <letter>.
Opening and closing tags must be written with the same case:
<Message>This is incorrect</message> <message>This is correct</message> |
XML Elements Must be Properly Nested
In HTML, you might see improperly nested elements:<b><i>This text is bold and italic</b></i> |
<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. <root> <child> <subchild>.....</subchild> </child> </root> |
XML Attribute Values Must be Quoted
XML elements can have attributes in name/value pairs just like in HTML.In XML the attribute value must always be quoted. Study the two XML documents below. The first one is incorrect, the second 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> |
Entity References
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:
<message>if salary < 1000 then</message> |
<message>if salary < 1000 then</message> |
< | < | less than |
> | > | greater than |
& | & | ampersand |
' | ' | apostrophe |
" | " | quotation mark |
Comments in XML
The syntax for writing comments in XML is similar to that of HTML.<!-- This is a comment -->
White-space is Preserved in XML
HTML truncates multiple white-space characters to one single white-space:HTML: | Hello my name is Tove |
Output: | Hello my name is Tove. |