Page 2 of 4
For example a more complete record could be shown as:
Life of Pi
Although this looks much more complicated, you can see that it is just the basic principle of using opening and closing tags to enclose data or other tags.
What might surprise you is that if you type this XML into a document and save it under a suitable name pie.xml say then you will be able to load it into a recent and view the structure of the data much more clearly.
Web browsers will attempt to format any file ending in .xml with indenting and coloring to show the tags and structure.
Figure 1 shows exactly what it looks like when loaded into Explorer.
Figure1: Viewing XML
The power of XML
There isn’t much more to XML than tags that surround data and you might well be thinking that this isn’t enough to warrant the fuss?
However, you should be able to see that XML can be used to give structure to any data that you care to imagine. You are free to invent any tags that you need and use them to build structures that your data fits.
The resulting XML file should be understandable by another human and, crucially, it should be processed correctly by any program that understands XML. For example, even though Internet Explorer had no prior knowledge of the structure of our book data record it managed to do a reasonable job of displaying it.
It is worth saying that the majority of XML documents are likely to have been produced automatically by an application and consumed automatically by another application. XML may be human readable but that doesn’t mean that humans are always the source or the intended destination for XML documents.
In most cases XML will lurk in the background, making some desirable facility work without you really being aware of why or how.
To make XML fully practical, however, we do need some additional features. The first is that XML tags can be a little more complicated than just a name.
You can include attributes within a tag to “describe” that data that follows. Attributes are also part of HTML but in this case what you can use is determined. In XML you can invent your own attributes.
For example, you could decide to include a rating for the book in the <Book> tag:
This inclusion of information within the tag as well as data between the tags has lots of advantages. It allows you to make a distinction between the data and additional information or meta-data concerning the real data.
For example in this case it allows you to select which <Book> records to process without altering the data in the body of the record.
Of course it is a matter of opinion whether or not something should be an attribute or just another pair of tags.
For example, the rating could be supplied as:
It can be difficult to decide when to invent a new tag or when to use an attribute but generally:
An element is used when:
- the content is long or variable in length
- order matters (attribute order is ignored)
- the information really is “content"
An attribute is used when:
- the information modifies the element in a way that isn’t naturally part of the content
- when you want to restrict the possible values used
- the information is obviously meta-data.
The ability to invent new tags and attributes is what makes XML extensible.
XML Based Standards
As XML is so extensible you are free to invent your own ways of describing data. However there are big advantages in agreeing to use the same XML tags and attributes to describe the same sort of data.
For example you could sit down tomorrow and invent an XML dialect that describes books in minute detail. You could call your new use for XML XBooks or something similar and announce it to the world.
Of course unless you happen to be a big company or someone with lots of influence the chances that anyone would use your dialect as a standard is quite small.
However, there are lots of standard forms of XML that we have managed to agree on and you can find a list of some of the best known at the end of the article. There is also the phenomenon of "microformats" small chunks of data such as names and addresses stored in obvious and easy to read XML.
The advantages of using a standard form is that we can pool our data. If I enter some data using XBooks and you enter some data using XBooks then, provided we really do use the same format, merging our data should be easy.
However freedom to invent your own XML means that you can deviate form any standard either on purpose or by accident and this spoils the chance to share data.