Tagging an XML document is, in many ways, similar to tagging an HTML document. Here are some of the most important guidelines to follow.
Rule #1: Remember the XML Declaration
This declaration goes at the beginning of the file and alerts the browser or other processing tools that this document contains XML tags. The declaration looks like this:
<?xml version="1.0" standalone="yes/no" encoding="UTF-8"?>
You can leave out the encoding attribute and the processor will use the UTF-8 default.
Rule #2: Do What the DTD Instructs
If you are creating a valid XML file, one that is checked against a DTD, make sure you know what tags are part of the DTD and use them appropriately in your document. Understand what each does and when to use it. Know what the allowable values are for each. Follow those rules. The XML document will validate against the specified DTD.
Rule #3: Watch Your Capitalization
XML is case-sensitive. <P> is not the same as <p>. Be consistent in how you define element names. For example, use ALL CAPS, or use Initial caps, or use all lowercase. It is very easy to create mis-matching case errors.
Also, make sure starting and ending tags use matching capitalization, too. If you start a paragraph with the <P> tag, you must end it with the </P> tag, not a </p>.
Rule #4: Quote Attribute Values
In HTML there is some confusion over when to enclose attribute values in quotes. In XML the rule is simple: enclose all attribute values in quotes, like this:
<NAME dob="1960">Ben Johnson</NAME>
Rule #5: Close All Tags
In XML you must close all
tags. This means that paragraphs must have corresponding end paragraph tags. Anchor names must have corresponding anchor end tags. A strict interpretation of HTML says we should have been doing this all along, but in reality, most of us haven't.
Rule #6: Close Empty Tags, Too
In HTML, empty tags, such as <br>
, do not close. In XML, empty tags do close. You can close them either by adding a separate close tag (</tagname>) or by combining the open and close tags into one tag. You create the open/close tag by adding a slash, /, to the end of the tag, like this:
This table shows some HTML common tags and how they would be treated in XML.
||Technically, in HTML, you're supposed to close this tag. In XML, it's essential to close it.
||All Elements in XML must have a Start-tag and an end-tag.
||This tag must be closed in XML in order to ensure a Well-Formed XML document.
|<META name="keywords" content="XML, SGML, HTML">
||META tags are considered empty elements in XML, and they must close.
||><META name="keywords" content="XML, SGML, HTML"/>|
||Break tags are considered empty elements.
|<IMG src= "coolpictures.html">
||This is an empty element tag.
||<IMG src= "coolpictures.html"/>|