Rules for writing XML

Home » xml » Rules for writing XML
xml No Comments

The rules of writing an XML document

XML has a structure that is regular and predictible, this is what makes it human readable. It is defined by a set of rules. If your document satisfies these rules, itis considered well-formed.

XML declaration

This is the first tag you will find in xml documents, it tells us and more important the applications and databases that will use it the version of xml used. The declarations or prologue (only in some circles) looks like this:

<?xml version=”1.0″ encoding="UTF-8" standalone="no" ?>

The prologue needs to contain the version of XML, but it can also contain the ecoding type, UTF-8 in our example, and standalone which tells us if the document is linked to an external DTD.

A root element is required

Every XML document must contain one, and only one, root element. This root element
contains all the other elements in the document. The only thing outside the root element is the xml declaration <?xml version=”1.0″?>, comments and processing instructions.

Closing tags are required

Every element must have a closing tag or the document will be baddly formated.
The way we close a <tag> is with and </tag> where you wish to close it.

There are elements that don’t need to be closed in that way because they are “self closing”, they are the same as the ones in html ex: <hr/>, &ltimg/>. They are also known as empty elements, because they don’t contain data, they are used to style the document (for viewing purposes) or to add data with their own attributes like the img tag with the src attribute, you could ofcourse create your own self closing tags (in xml, it doesn’t work in html).

Elements must be properly nested

Unlike html and other languages XML is case sensitive. Elements named thing , THING , and Thing are considered entirely separate and unrelated to each other, if your not careful you could make an useless document.

Values must be enclosed in quotation marks

This part is about attributes, they are used to add data to the elements (meta data), <man height=”1.8m”>, the data added needs to be enclosed in quotation marks.

XML documents can get very very long and hard to maintain, this will give you head aches at some point, fortunately for you there are easier ways to do this than going line by line, take a look at this article for a short guide.