XML Syntax (2)
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. But
in XML the attribute value must always be quoted. Study the two XML documents
below. The first one is incorrect, the second is correct:
<customer dateentry=08/11/2008>
<firstname>Michael</firstname>
<lastname>Smith</lastname>
</customer>
<customer dateentry="08/11/2008">
<firstname>Michael</firstname>
<lastname>Smith</lastname>
</customer>
The error in the first document is that the date attribute in the note element
is not quoted.
Some characters have a special meaning in XML.
If you place a character like "<" inside an XML element, for example:
<category>GPS Products < $1000</category>
The character "<" will generate an error because the parser interprets
it as the start of a new element. To avoid this error, replace the "<"
character with an entity reference:
<category>GPS Products < $1000</category>
There are 5 predefined entity references in XML:
< < less than
> > greater than
& & ampersand
' ' apostrophe
" " quotation mark
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.
Comments in XML. The syntax for writing comments in XML is the same as in HTML.
<!-- This is a comment -->
In XML, White Space is Preserved. In an HTML file, multiple white space characters
are reduced to a single white space. But in an XML file, the white space is
not truncated.
|