This article will teach you how to create your first well-formed XML document. I will go through the various rules for creating a well-formed document, beginning with the XML declaration. By the end of the article you will have a firm grasp on how to create well-formed documents in XML.
In our previous article we looked at the basic architecture of an XML document. We learned to create root, parent, and child structures, and put this into practice by creating a taxonomy of fields necessary for a music store.
Part of this article will rehash what we learned in our last article, adding more depth and understanding. First, however, we will look at the XML declaration, as promised above.
In XML documents, the first thing that you are required to input is the declaration. With this statement, you declare that the XML document is compliant. This is the first line that you will see in your XML document. Here is how it should be written:
Or, if you are using XML version 1.1, you would write:
The processor will garner certain information from the above declaration. First, It will learn that the document is an XML document and second, it will know upon which version of XML the document is built.
You can also add the standalone attribute and a value to the declaration. This informs the processor that your document does not rely on any other document to work (if the value is set to yes, mind you). If you set your attribute to yes in this manner, you can still call on style sheets, DTDs, schemas, etc. You are simply telling the processor that it will have to look no further than this document for actual content.
To add the standalone attribute, you write the declaration like this:
<?xml version="1.0" standalone="yes"?>
You may also want to indicate which encoding you will be using. You can do this by specifying your encoding attribute. There are different encoding attributes from which to choose, but for now we will use UTF 8, or the standard 8-bit ASCII character-set. Here is how you would write the declaration including the encoding attribute:
The XML declaration is a part of what is known as the XML prolog, and can include more data than we have seen here. We will cover these other attributes in a later article however. For now, be satisfied with these basics.