Become able to:
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.
Morever, the tags in HTML are for generic “documents” — the sort that human beings read — with elements such as paragraph, headings, lists, and tables. The document could be about anything, because it’s designed for a human reader, which has general intelligence.
XML documents are intended to be machine-readable (i.e., by a computer, with somewhat less than general intelligence), though they are also human-readable, and can even be styled with CSS (like HTML) to make them more human-friendly.
XML documents tend to be about specific kinds of things. XML is not so much a language as a framework for making languages: each time you invent a new set of XML tags, you’re creating a new language. There are many standard languages based on XML, and these languages are about particular kinds of things, such as:
Wikipedia has an (incomplete) list of XML markup languages.
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:
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. 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).
Although XML is semantically clear, it 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).
We begin an XML document with an xml declaration, optional link to a stylesheet, and usually a DOCTYPE declaration:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet rel="stylesheet" type="text/css" href="URL"?>
<!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.
To write well-formed XML, we must follow a few simple rules. The rules must be followed strictly, or programs that read XML (like your web browser) will report an error.
Begin and end each element explicitly:
<tag> ... </tag>or, if there is no content in the element, with the abbreviated begin/end form:
<tag/>Observe case-sensitivity of tags: <tag> ≠ <TAG>
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>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.)
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>Add an observation (obs element) to the weather XML document. (Right-click to download at least the first file; edit it, and view in your browser.)
An XML document, or a collection of them, could be considered as a database, but it is very different from the relational databases that we have been studying.
Recall that a relational DB is a collection of tables, and each table has named fields (columns) and identifiable records (rows, identified by the primary key). A table is a two-dimensional structure.
In contrast, an XML document structures data like a tree, originating at the root and branching out. A collection of XML documents is a collection of trees, which computer scientists refer to as a “forest” (surprise?).

So an XML “database” stores information in a different structure than a relational database.
An XML document (collection) might be considered a (non-relational) “database,” but by itself it certainly does not have all the features of a DBMS. For example, a DBMS provides a query language, such as SQL or QBE.
However, there are query languages for XML; they include XSLT and XQuery. (Snyder gives examples of using XSLT in chapter 16 for transforming XML into HTML — this is not required reading.)
There are DBMSs for XML, but none of them come anywhere close in popularity to the best-known relational DBMSs (D-B Engines Ranking, retrieved 2016 Mar 9).
Many programming languages can also manipulate XML data through the Document Object Model (DOM).
XML provides a widely used method of encoding information, an interesting alternative to both HTML and relational databases.
Like HTML, it consists of tags. Unlike HTML, XML allows for creating new tag types, and is designed to be machine readable.
Like a relational DBMS, XML stores data. XML data stores are tree-structured, unlike a relational DB which consists of tables. There are query languages for XML, but they are different from the query languages used for a relational DB.
2016 Mar 9.
2015 March 20.