A valid document conforms to the XML syntax rules and
follows the guidelines of a Document Type Definition (DTD).
The process of comparing the XML document to the DTD is called validation. This process is performed by a tool called a parser.
Begin the Valid XML Document
To begin a well-formed document, type the XML declaration:
<?xml version="1.0" standalone="no" encode="UTF-8"?>
If you are creating an XML-only document, it will be the first thing in the file.
You must include the version attribute for the XML declaration. The version is currently "1.0." Defining the version lets the browser know that the document that follows is an XML document, using XML 1.0 structure and syntax.
The standalone="no" attribute tells the computer that it must look for a DTD and validate the XML tags.
Create a DOCTYPE Definition
Finally, declare the encoding of the document. You can leave off this attribute and the processor will default to UTF-8.
The second element in a valid XML document is the DOCTYPE definition. This identifies the type of document and DTD in use.
If you look at HTML source files, you'll often see a !DOCTYPE definition, especially if the file was created by a WYSIWYG tool. The DOCTYPE definition points to an HTML DTD.
In a valid XML file, !DOCTYPE tells the program that is processing your XML file two things: the name of the type of document and the name and location of the DTD against which to validate the file's contents.
The DOCTYPE definition looks like this:
<!DOCTYPE type-of-doc SYSTEM/PUBLIC "dtd-name">
This says that you are defining the DOCTYPE.
This is the name of the type of document contained in this file. Typically, this is the same name as the DTD.
SYSTEM tells the processor to look for the private DTD at the following location. PUBLIC tells the processor to look for a public DTD at the following location.
The URL after SYSTEM or PUBLIC is the name of the dtd file. All DTDs end with the extension .dtd.
If you want, instead of pointing to an external DTD, you could place the DTD information within the DOCTYPE definition, making it local to your XML document. You should do this only if you want to define a few simple elements and you want them permanently attached to a particular document.
Remember the Root Element
After the declaration, enter the tag for the root element of your document. This is the top-most element, under which all elements are grouped.
Follow XML Syntax
Now, enter the rest of the your content. Remember to follow XML syntax:
- Remember that capitalization matters;
- Quote all attribute values;
- Close all tags;
- Remember to close empty tags too, like this: