XML Syntax and Elements

XML Syntax and Elements

 
XML is used for structured, descriptive markup. Each XML element can contain text, other XMLelements that are known as its children, or nothing. In designing an XML document, we should think about what will contain what. You can think of the structure as a family tree with many branches, or as containers within containers. The root element is the top-level element that is the parent of all of the other elements–that is, it contains everything else that appears in the document.
 
Basic XML Rules
 
• Elements that contain data must have start tags and ending tags
 
<email> […] </email>.
 
• Empty tags must be closed
 
If a tag contains no data and therefore takes no closing tag (e.g., for a page or line break or an image), then embed the closing within the tag itself: <br />
or provide a closing tag: <br> </br>
 
• Nest tags properly
 
Elements should not overlap.
 
Bad Nesting
 
<sender> <sender-email-id> </sender> </sender-email-id>
 
Good Nesting
 
<sender> <sender-email-id> </sender-email-id> </sender>
 
 
• All attribute values must be wrapped in quotation marks
 
For instance, you should use:
 
<a href=”products.php”>
rather than
<a href=products.php>
 
• A declaration must appear at the top of an XML document to signify what it is:
 
XML Declaration, e.g.
 
<?xml version=”1.0″?>
 
• Use a consistent case
 
Whereas in HTML you can use upper and lower case with abandon, it is good form to use a consistent case in XML—generally lower case. XML is case sensitive, so <TAG> and <Tag> will be treated differently. It is recommended that you use lowercase as tag names.
 
• One root element per file
 
Another rule for XML files is only one root element per file is allowed.
 
 
XML Elements
 
Elements serve as the building blocks of XML–as the basic units of description.
 
An element is comprised of an opening and closing tag as well as the content within:
 
<associate>Dharmendra Das </associate>
 
XML tags typically come in pairs: opening tags and closing tags. Each tag is wrapped in angle brackets; end tags have a backward slash before the element name.
 
XML permits users to invent element names, so long as the names begin with a letter or an underscore (_) and do not include white spaces.
 
Some elements are empty–that is, they do not wrap around any content and do not close. For instance, a page or a link break would be an empty element, as would be the insertion of an image.
 
 
 
XML Declaration
 
XML documents must begin with an XML declaration, which provides processing instructions and identifies the version of XML in use.
 
At present, XML 1.0 is the only version, so the declaration would be:
 
<?xml version=”1.0″?>
 
 
XML Root Element
 
Immediately following the XML declaration is the root element, which will close at the very end of the document.
 
For instance, if we were writing an XML document whose root element is “book”, it would look like:
 
<?xml version=”1.0″?>
<associate_details>
[everything else]
</associate_details>
 
Always remember, for XML files is only one root element per file is allowed.