XML stands for eXtensible Markup Language and it is a structured document standard. It is structured, as it allows content to be defined within a context, for example, the content of a header is different to the content in the body which is again different from the content of the footer.
An XML document can be used either as a presentational tool, or hundreds of other formats including e-commerce transactions, mathematical equations, object meta-data, configuration settings and much more.
XML is similar to HTML in that content is contained within tags, but unlike HTML where there is a predefined and fixed number of tags and formats, XML provides the facility to define your own tags and structural relationships between them.
Structure of an XML Document
All XML documents must have a XML declaration and optionally an XML namespace.
<?xml version="1.0" encoding="utf-8" ?> <Zoo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> </Zoo>
Where Zoo is the name of the top-level structure tag. It can be anything you want, but in this example, we are going to represent a Zoo in XML. Notice that the Zoo tag has a closing tag as well as an opening tag. The open tag also has attributes called xmlns:xsd and xmlns:xsi that specify additional information about the tag.
The rest of the document will depend on what you want to do with it. You can have a header, body and footer section, each with additional tags within. You can have a list of part numbers or style codes. In this example, we are creating a Zoo and we need animals.
<?xml version="1.0" encoding="utf-8" ?> <Zoo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <animal type="Antelope"> <PictureFile>....antelope.jpg</PictureFile> <Age>0</Age> <Weight>0</Weight> <Description>Antelope range in size from the tiny royal antelope, which stands about 25 cm (about 10 in) high at the shoulder, to the giant eland, sometimes about 1.8 m (about 5.9 ft) in height and weighing up to about 680 kg (about 1500 lb).</Description> </animal> <animal type="Elephant"> <PictureFile>....elephant.jpg</PictureFile> <Age>0</Age> <Weight>0</Weight> <Description>Over the past 40 million years, more than 600 species of elephants have roamed the earth. Today only two species are alive; the African elephant and the Asian elephant. Climate fluctuations over the millennia and resulting vegetation changes caused the extinction of many elephant species, but human impact has also taken its toll.</Description> </animal> <animal type="Lion"> <PictureFile>....lion.jpg</PictureFile> <Age>0</Age> <Weight>0</Weight> <Description>Lions rival tigers for the title of biggest cat. In fact, lions and tigers are so similar in their physical features that without their distinctively coloured fur, even scientists have trouble telling them apart. Male lions weigh between 150 and 250 kg (330 and 550 lb) and stand about 123 cm (about 48 in) tall at the shoulder.</Description> </animal> </Zoo>
In this example, we have a repeating element called animal which has an attribute called type. Each animal element has further tags, which contain information about the animal. You can see here how the XML creates a structure: the picture belongs to an animal, which belongs to the zoo.
XML can be used to represent data as simple as this, or much more complex documents such as e-commerce XML (cXML) which contains orders, invoices and credits with many item lines, order headers, totals, supplier details and so on.
You can open XML documents from within a web browser. It will present the document in a nice indented representation of the source file and you can expand or collapses the tags to better understand the structure. In a later tutorial, we will look at how you can present this data to a user in a user-friendlier manner using XML Stylesheets. You can also convert it to text, or any format you can think of.