XML

Learning Objectives

Become able to:

Comments

XML is like HTML, but more general, in that you can invent your own tags. With HTML, there is a fixed set of tags; you cannot add to it.

XML structures data (or documents) like a tree, originating at the root and branching out.

Writing XML

We begin an XML document with an xml declaration and usually a DOCTYPE declaration:

<?xml version="1.0" encoding="UTF-8"?>
<DOCTYPE mydoctype>

We can insert a link to a CSS stylesheet, as in HTML. If we view an XML document in a web browser and it is not linked to a stylesheet, the browser shows us the top-level elements and provides controls for us to expand or contract them.

Syntax Rules

To write well-formed XML, we must:

  1. Begin and end each element explicitly:

    <tag> ... </tag>

    or, if there is no content in the element, with the abbreviated begin/end form:

    <tag/>
  2. Observe case-sensitivity of tags: <tag>  ≠  <TAG>

  3. Nest tags properly, so that elements do not overlap.

    OK:

    <a><b></b></a>

    Wrong: the b element begins within a but a ends before b:

    <a><b></a></b>
  4. Write exactly one top-level element, called the root or document element; everything else is contained within this. (In XHTML, the html element is the root.)

  5. Enclose all attribute values in single or double quotation marks.

    OK:

    <a href="http://example.com/">example</a>

    Wrong:

    <a href=http://example.com/>example</a>

Origins and Motivation

HTML is the language of the web, invented by Tim Berners-Lee. After a while, Berners-Lee got an idea which he called the semantic web: he wanted to make it easier for computer programs to make sense of the data on the web (that is, to interpret for people in ways that make sense), and to do that, suggested these changes:

  1. Separate style from content in HTML—leading to the idea of CSS stylesheets, instead of style embedded in the HTML elements themselves.
  2. Self-describing data—leading to the idea of tags that describe the meaning of the data (for example, “this is the price” rather than in document terms, “this is a table data element”).

Berners-Lee went back to Standard Generalized Markup Languagte (SGML), which had been the generic basis for HTML, made some simplifications, and the result was eXtensible Markup Language (XML).

Here’s an example to show how XML could be better than HTML for some weather data. Suppose we have this table in a web page:

Time Temperature Pressure Humidity Wind
1:00 28.6 30.34 63% NNE, 6.8
2:00 30.4 30.29 59% NE, 7.5

A program to analyze this data would have to pull it out of all these HTML tags:

<table>
  <tr><th>Time</th><th>Temperature</th><th>Pressure</th><th>Humidity</th>
      <th>Wind</th>
  </tr> 
  <tr><td>1:00</td><td>28.6</td><td>30.34</td><td>63%</td><td>NNE, 6.8</td>
  </tr>
  <tr><td>2:00</td><td>30.4</td><td>30.29</td><td>59%</td><td>NE, 7.5</td>
  </tr>
  ...
</table>

The tags don’t say what the data means; for that, we have to look at the column headings.

Here’s an XML version:

<weather-obs>
  <obs><time>1:00</time><temp>28.6</temp><press>30.34</press>
    <humidity>63%</humidity>
    <wind>
      <dir>NNE</dir>
      <speed>6.8</speed>
    </wind>
  </obs>
  <obs><time>2:00</time><temp>30.4</temp><press>30.29</press>
    <humidity>59%</humidity>
    <wind>
      <dir>NE</dir>
      <speed>7.5</speed>
    </wind>
  </obs>
  ...
</weather-obs>

SGML, originally designed as a document markup language, was now transformed into a data markup language, XML. The result, although semantically clear, is somewhat verbose, with all of those tags. Douglas Crockford has argued in favor of lighter data markup languages such as JSON (JavaScript Object Notation).

Nevertheless, whether on or off the web, XML has become widely used as a format both for data storage (e.g., “office” documents) and data interchange. An XML application is a particular language designed within the XML framework. Early examples of XML applications include MathML, SVG (Scalar Vector Graphics, for drawings), and MusicML (for music notation).

An XML document might be considered a “database,” but by itself it certainly does not have all the features of a DBMS. There are query languages for XML; they include XSLT and XQuery. Many programming languages can also manipulate XML data through the Document Object Model (DOM).