Struggling with XML? Can't figure out the difference between an entity and a namespace? Fear not - our XML series has all the answers. This introductory article discusses the origins and design goals of XML, the basic rules of XML markup, and how to use elements and attributes in an XML document.
OK, enough with the background - let's get our hands dirty. Consider the following XML document:
<?xml version="1.0"?>
<review>
<genre>Action</genre>
<title>X-Men</title>
<cast>
<person>Hugh
Jackman</person>
<person>Patrick Stewart</person>
<person>Ian
McKellen</person>
<person>Famke Janssen</person>
</cast>
<director>Bryan
Singer</director>
<duration>104</duration>
<year>2000</year>
<body>Every
once in a while, Hollywood takes a comic-book hero, shoots him
on celluloid, slaps
in a few whiz-bang special effects and stands back to
see the reaction. Sometimes
the results are memorable
(<title>Superman</title>, <title>Spiderman</title>,
<title>Flash
Gordon</title>) and sometimes disastrous (<title>Spawn</title>,
<title>The
Avengers</title>). Luckily, <title>X-Men</title> falls
into the former
category - it's a clever, well-directed film that should please
both
comic-book aficionados and their less well-read cousins.</body>
<rating>4</rating>
</review>
As you can see, an XML document, like an HTML document, is simply an ASCII text
file. This specific text file contains a recipe, broken up into different sections; each section is further "marked up" with descriptive tags to precisely identify the type of data contained within it.
An XML document may be either "well-formed" or "valid".
A well-formed document is one which meets the specifications laid down in the XML recommendation - that it, it follows the rules for element and attribute names, contains all essential declarations, and has properly-nested elements.
A valid document is one which, in addition to being well-formed, adheres to the rules laid out in a document type definition (DTD) or XML Schema. By imposing some structure on an XML document, a DTD makes it possible for documents to conform to some standard rules, and for applications to avoid nasty surprises in the form of incompatible or invalid data.
DTDs are essential when managing a large number of XML documents, as they immediately make it possible to apply a standard set of rules to different documents and thereby demand conformance to a common standard. However, for smaller, simpler documents, a DTD can often be overkill, adding substantially to download and processing time.
This article copyright Melonfire 2001. All rights reserved.