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.
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
symbols is given to a node of a tree. This is called a
Everything up until a corresponding
(the same tag except that it starts with
</) forms the content of the node, which can itself be a tree. Such trees
Figure 1.1) are called
can also contain character data and even mix character data and elements (e.g.
food-pairing). In Lisp (bottom of
Figure 1.1), trees are represented by embedded lists (i.e. identifiers or
lists enclosed between opening and closing parentheses) whose first element is the name of
the node; character data is represented by character strings. An XML element with no
content can be indicated with an end-tag immediately following a start-tag and can be
abridged as an
a start-tag with a terminating
Figure 1.1). Comments
can be added to an XML file by means of a special element that starts with
and ends with
Additional information can be added to an element tag with
pairs comprising the name of the attribute
format), an equal sign and the corresponding character string value within
double or single quotes (e.g.
'1l'). Attributes can also be added to an empty element (e.g.
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. In Lisp, shown in the bottom part of the figure, a tree is represented by a list of identifiers or nested lists enclosed in parentheses. The root of the tree is the first identifier of the list and the children follow in the list. Here we represent attributes as a list of pairs with names indicated by keywords (i.e. identifiers starting with a colon) followed by the corresponding value.
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.1. An XML structure and the corresponding tree [
A simple XML structure (top) and a corresponding Lisp style structure
(bottom). In the middle is shown an equivalent tree structure in which the element names
are shown in bold and the attribute names in italics. The real
information is the character data which appears in roman font. This shows the relations
as children; a sibling of
<?xml version="1.0" encoding="UTF-8"?> <wine name="M" code="00518712" format="1l"> <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> <price>9.95</price> <year>2004</year> </wine>
(wine (:name "M" :code "C00518712" :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) (price 9.95) (year 2004) )
Figure 1.2. Tree, web browser, grid and table views of an XML file
At the top right, the file of
as displayed in Internet Explorer; the
at the left of
indicates that this element is hidden by collapsing. By clicking on it, the
and the tree is displayed in full. The other parts of the figure show alternative views
of the same file available on commercial XML editors in order to
the tags from the user: on the left, the <oXygen/> tree editor view; on the middle
right is an XMLSpy grid view; at the bottom, a table view offered by XMLSpy as a
of the grid 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 charactersmost often Unicodeand 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 ; 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
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
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>HTML compaction of "Wine.xml"</title> </head> <body> <ul><li xmlns=""><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>Cold cuts, <li><b>bold</b> Meatloaf</li>, Pizza</ul> </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
<?xml version="1.0" encoding="utf-8"?> wine[@name[M] @code @format[1l] properties[color[red] alcoholic-strength] origin[country[Italy] region[Abruzzo] producer[Cantina Miglianico SCARL]] rating[@stars] food-pairing[Cold cuts, bold[Meatloaf] , Pizza] price[9.95] year]
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 , pages ix-x.|