XML has been developed to facilitate the annotation of information to be shared between computer systems. Because it is intended to be easily generated and parsed by computer systems on diverse platforms, its format is based on character streams rather than internal binary ones. Being character based, it also has the nice property of being readable and editable by humans using standard text editors.
Figure 1.1. An XML structure and the corresponding tree [Wine.xml
]
A simple XML structure (top) and an equivalent tree structure in which the element names
are shown in bold and the attribute names in italics. In the tree, the real
information is the character data which appears in roman font. This shows the relations
between nodes: properties
has wine
as
parent and color
, alcoholic-strength
as children; a sibling of region
is country
.
<?xml version="1.0" encoding="UTF-8"?> <wine name="M" code="00518712" format="1l"> <!-- code is the same as for SAQ.com--> <properties> <color>red</color> <alcoholic-strength>12</alcoholic-strength> </properties> <origin> <country>Italy</country> <region>Abruzzo</region> <producer>Cantina Miglianico SCARL</producer> </origin> <rating stars="2"/> <food-pairing>Cold cuts, <bold>Meatloaf,</bold> Pizza</food-pairing> <food-pairing>Asian spicy pork burger</food-pairing> <price>9.95</price> <year>2004</year> </wine>
![]() |
XML is based on a uniform, simple and yet powerful model of data organization: the generalized tree. Such a tree is defined as either a single element or an element having other trees as its sub-elements called children (see middle of Figure 1.1). This is the same model as the one chosen for the Lisp programming language 50 years ago. This hierarchical model is very simple and allows a simple annotation of the data. As in Lisp, the same tree notation used for data representation is also employed to write programs to transform tree structures into other tree structures. On top of this identity of data and program representation, in XML, the tree notation is also used to denote type information to validate XML data.
As is shown at the top of
Figure 1.1, an arbitrary name between
<
and
>
symbols is given to a node of a tree. This is called a
start-tag.
Everything up until a corresponding
end-tag
(the same tag except that it starts with
</
) forms the content of the node, which can itself be a tree. Such trees
(e.g.
wine
,
properties
and
color
in
Figure 1.1) are called
elements. Elements
can also contain character data and even mix character data and elements (e.g.
food-pairing
). An XML element with no
content can be indicated with an end-tag immediately following a start-tag and can be
abridged as an
empty-element tag:
a start-tag with a terminating /
(see rating
in Figure 1.1).
It is possible to have more than one element of the same name within the
children of an element (see tasting-note
in Figure 1.1).
The ordering between children is important and is referred
as the document order.
Comments
can be added to an XML file by means of a special element that starts with
<!--
and ends with
-->
(see end of the second line of Figure 1.1).
Additional information can be added to an element tag with
attribute
pairs comprising the name of the attribute
(e.g.
format
), an equal sign and the corresponding character string value within
double or single quotes (e.g.
"1l"
or
'1l'
). Attributes can also be added to an empty element (e.g.
rating
). Attribute names within a single start-tag must be unique and there is no
ordering between the different attributes. Attributes within a single element are thus considered
as a table or a dictionary in which the names are the keys and the strings, the corresponding values.
As illustrated in the middle part of Figure 1.1, these notations are equivalent to a tree data structure where each node is labelled with its name and attributes. Character data appears as leaf nodes. An empty element is a node with no sub-tree.
XML has the (well deserved) reputation of being verbose but it must be kept in mind that this notation is primarily aimed at communication between machines for which verbosity is not a problem but uniformity of notation is a real asset. In fact, humans should not be really required to type all these start-tags and end-tags. Indeed, many useful structural XML editors are now available which hide the verbosity, keeping only the important structural information or by displaying embedded tables instead of tags. Figure 1.2 shows alternative views of an XML file.
Figure 1.2. Web browser, summary and grid editor views of an XML file
At the top, the file of Figure 1.1 as displayed in a web browser;
the right pointing triangle at the left of <properties>
and the ellipsis
between start and end tags indicate that this element is hidden by collapsing. By
clicking on it, the right pointing triangle becomes a down pointing triangle and the
tree is displayed in full. The bottom of the figure show alternative views of the
same file available on a commercial XML editor (<oXygen/>) in order to
hide the tags from the user: on the left, a summary view similar to the
one given in a browser but without the tags and on the right a grid editor view
![]() |
![]() |
As has been shown by Lisp over the years, this tree notation is very general and can be used not only to represent data but also its processing. Programs for transforming XML tree structures into other tree structures can be written in XSL (eXtensible Stylesheet Language) stylesheets, which are a declarative notation for XML transformations also written in XML.
An important feature of XML, and one that differs from Lisp, is the a priori type checking that can be performed on the file and the validation that can be performed before processing. XML type information can be provided either through a DTD or a schema which offers a quite powerful and flexible type system. A schema is also written as an XML file which can itself be type checked. An alternative schema notation called RELAX NG will also be presented later in this document.
XML originated from the need for a flexible way of organizing natural language texts and thus its designers used standard representations of the characters—most often Unicode—and standard encodings such as UTF-8 or UTF-16, which will not be discussed here.
XML is also widely used in computing systems to systematize structured data as an alternative to databases. Many relational databases also offer XML specific features for indexing and searching. Because of the portability of its encoding and the fact that XML parsers are freely available, it is also used for many tasks requiring flexible data manipulation to transfer data between systems, as configuration files of programs and for keeping information about other files. This document will not present these applications but will focus on a single one (creating a compact representation of an XML file) that will be used throughout so that one can appreciate the similarities and differences between the XML technologies presented.
Figure 1.3 presents the XML technologies we will describe in this report and their relations. The focus of the whole process is an XML instance document (towards the top of the figure) that contains the data. This XML document can be validated against a specification described either as a Document Type Description (DTD) or a XML Schema, itself another XML file. The validation process will be described in Chapter 3. Once validated, XML data can be used by application programs through specific Application Programming Interfaces (APIs) described in Chapter 8 and Chapter 9. XML data can also be processed by transformations (Chapter 5) written as stylesheets, a special kind of validated XML file, to create new XML, HTML, PDF or text files.
For example, the XML file at the top of Figure 1.1 can be transformed with a stylesheet into an HTML one. The top of Figure 1.4 shows such a possible HTML output, displayed in a web browser shown at the bottom of the figure. This is the kind of tree-to-tree transformation for which XSLT was specifically designed. To better illustrate the power of the more general transformations that XSLT allows, we will show how to obtain a more compact form; it is shown in Figure 1.5 either as a text file (top) or in PDF (bottom) through a transformation using Formatting Objects. This compact notation is similar to that used in the Formal Description of XML [13]; it must be viewed as a programming exercise and not as a compression technique for XML files.
Figure 1.4. HTML source of the compact form and its rendering in a browser.
Representation of the tree in
Figure 1.1
in source HTML and as it appears in a browser window. This HTML output (slighly
reformatted here to fit in the page) was produced by our example stylesheet
compactHTML.xsl
shown in
Example 5.7.
<html xmlns="http://www.w3.org/1999/xhtml"> <head><title>HTML compaction of "Wine.xml"</title></head> <body> <ul><li><b>wine</b> name="M" code="00518712" format="1l" <ul> <li><b>properties</b><ul> <li><b>color</b> red</li> <li><b>alcoholic-strength</b> 12</li> </ul> </li> <li><b>origin</b><ul> <li><b>country</b> Italy</li> <li><b>region</b> Abruzzo</li> <li><b>producer</b> Cantina Miglianico SCARL</li> </ul> </li> <li><b>rating</b> stars="2" </li> <li><b>food-pairing</b><ul> <li>Cold cuts, </li> <li><b>bold</b> Meatloaf,</li> <li> Pizza</li> </ul> </li> <li><b>food-pairing</b> Asian spicy pork burger</li> <li><b>price</b> 9.95</li> <li><b>year</b> 2004</li> </ul> </li></ul> </body> </html>
Figure 1.5. Compact forms in text and in PDF
Compact form of the tree in Figure 1.1 in text and PDF formats. These outputs were produced by the stylesheets of Example 5.9 and Example 5.10.
wine[@name[M] @code[00518712] @format[1l] properties[color[red] alcoholic-strength[12]] origin[country[Italy] region[Abruzzo] producer[Cantina Miglianico SCARL]] rating[@stars[2]] food-pairing[Cold cuts, bold[Meatloaf,] Pizza] food-pairing[Asian spicy pork burger] price[9.95] year[2004]]
![]() |
This document is also an example of the use of these technologies because its source file is itself an XML document validated by a DocBook RELAX NG Schema and processed by two stylesheets: one produces a set of linked HTML files and another produces an XSL-FO file which is then rendered as a PDF file. This process is further described in Appendix C
This chapter has shown that XML is a flexible notation for adding information to natural language text but it is increasingly used in other areas as well. The raw XML is verbose and not very user-friendly but it can be hidden by appropriate tools. Programmers can also rely on freely available XML parsers and validators in order to get a well-organized data structure from an XML file.
This document tries to give an overall impression of some XML techniques and should not be considered a definitive or exhaustive manual. We will describe the main principles and present general rules and the intuition behind some of the technologies. For the sake of simplicity, we will sometimes be making white lies that seasoned XML experts could point out.
[T]he right abstraction [for XML ...] is a labeled tree of elements. Each element has an ordered list of children in which each child is a Unicode string or an element. An element is labeled with a two-part name consisting of a URI and local part. Each element also has an unordered collection of attributes where each attribute has a two-part name, distinct from the name of the other attributes in the collection, and a value, which is a Unicode string. That is the complete abstraction. [...] If you understand this, then you understand XML. |
||
-- James Clark, in [59], pages ix-x. |