XML: Looking at the Forest Instead of the Trees

Guy Lapalme
Professor
Département d'informatique et de recherche opérationnelle Université de Montréal

CP C.P. 6128, Succ. Centre-Ville
Montréal, Québec
Canada H3C 3J7
lapalme@iro.umontreal.ca
http://www.iro.umontreal.ca/~lapalme/ForestInsteadOfTheTrees/

July 24, 2013

Abstract

This tutorial gives a high-level overview of the main principles underlying some XML technologies: DTD, XML Schema, RELAX NG, Schematron, XPath, XSL stylesheets, Formatting Objects, DOM, SAX and StAX models of processing. They are presented from the point of view of the computer scientist, without the hype too often associated with them. We do not give a detailed description but we focus on the relations between the main ideas of XML and other computer language technologies. A single compact pretty-print example is used throughout the text to illustrate the processing of an XML structure with XML technologies or with Java programs. We also show how to create an XML document by programming in Java, in Ruby and in E4X (Ecmascript for XML).

The source code of the example XML files and the programs are available either at the companion web site of this document or by clicking on the file name within brackets at the start of the caption of each example. The source file will then be shown in a web page or in a new tab of the browser. Should the page be interpreted by the browser, the reader can ask for the source of the page.


Acknowledgements

The writing of this XML tutorial started in the Fall of 2002 during my sabbatical at the Université de Grenoble and at Xerox Research Centre Europe. I wish to thank Gilles Sérasset, Christian Boitet, Pierre Isabelle and Marc Dymetman for many fruitful discussions.

Since then, the document has been improved (at least it has increased in the number of pages...) after having used it in teaching undergraduate and graduate courses at the Université de Montréal: IFT3225 and IFT6281. I especially thank Fabrizio Gotti for his careful proofreading and many insightful comments.

In 2007, I decided to start practicing what I preach by using XML technologies for the organization of the manuscript of this tutorial. Fabrizio Gotti converted the original LaTeX files to DocBook so that now PDF and HTML versions can be produced from a single set of XML source files.

Table of Contents

1. Introduction
2. Instance Document
2.1. Namespaces
3. Document Validation
3.1. Document Type Definition (DTD)
3.1.1. Associating an Instance File with a DTD
3.2. Schema
3.2.1. Simple Types
3.2.2. Complex Types
3.2.3. Namespaces in Schemas
3.2.4. Overview of the XML Schemas
3.3. RELAX NG
3.4. Schematron
3.5. Associating an Instance File with a Schema
3.6. Additional Information on XML Schema
4. XPath
4.1. XPath expression components
4.2. XPath functions
4.3. XPath examples
4.4. Additional Information on XPath
5. Document Transformation
5.1. XSL Transformations
5.2. Transformation in HTML
5.2.1. Table
5.2.2. Computing New Information
5.2.3. Bulleted Lists
5.3. Transformation into a Compact Textual Form
5.4. Transformation into PDF with XSL-FO
5.4.1. XSL-FO Input to the Renderer
5.4.2. From the Instance Document to the XSL-FO file
5.5. Transformation with a CSS
5.6. Associating an Instance File to a Stylesheet
5.7. Additional Information on XSL
6. Document Query
6.1. XQuery output in HTML
6.1.1. Table
6.1.2. Computing New Information
6.1.3. Bulleted Lists
6.2. Transformation into a Compact Textual Form with XQuery
6.3. Querying an instance file
6.4. Additional Information on XQuery
7. RDF : Resource Description Framework
7.1. Triples in RDF/XML
7.2. RDF Schema
7.3. RDF queries
7.4. Convert XML to RDF/XML
7.5. RDF versus XML
7.6. Additional Information on RDF
8. Document Processing by Programming in Java
8.1. Document Object Model (DOM)
8.2. Simple API for XML (SAX)
8.3. Stream API for XML (StAX)
8.4. Showing an Interactive Tree View
8.4.1. Building a JTree with DOM
8.4.2. Building a JTree with SAX
8.4.3. Building a JTree with StAX
8.5. Additional Information on Programming Models
9. Document Creation by Programming in Java
9.1. Creating a DOM Document
9.2. Creating a Document with SAX Events
9.3. Creating a Document with StAX streaming
9.4. Additional Information on XML Document Creation
10. Alternative approaches
10.1. XML processing with Ruby
10.1.1. DOM parsing using Ruby
10.1.2. SAX parsing using Ruby
10.1.3. Creating an XML document using Ruby
10.2. XML processing with Python
10.2.1. DOM parsing using Python
10.2.2. SAX parsing using Python
10.2.3. StAX parsing using Python
10.2.4. Creating an XML document using Python
10.2.5. Other means of dealing with XML documents using Python
10.3. XML processing with PHP
10.3.1. DOM parsing using PHP
10.3.2. SAX parsing using PHP
10.3.3. StAX parsing using PHP
10.3.4. Creating an XML document using PHP
10.3.5. Other means of dealing with XML documents using PHP
10.4. XML processing with E4X
10.4.1. DOM parsing using E4X
10.4.2. Creating an XML document using E4X
10.5. XML alternative notations
10.5.1. JSON
10.5.2. YAML
10.6. Additional information on alternative approaches
11. Conclusion
Bibliography
A. Some XML Related Technologies and Systems
B. Quick Reference Tables
B.1. Regular expression
B.2. DTD
B.3. XML Schema
B.4. RELAX NG
B.5. Schematron
B.6. XSLT
C. XML Production of this Document
D. Instance documents
Index

List of Figures

1.1. An XML structure and the corresponding tree [Wine.xml]
1.2. Tree, web browser, grid and table views of an XML file
1.3. Relations between some XML technologies
1.4. HTML source of the compact form and its rendering in a browser.
1.5. Compact forms in text and in PDF
3.1. Graphical view of the schema for the cellar book
3.2. Graphical view of the schema for the wine catalog
3.3. Built-in datatypes for XML Schema
4.1. Some XPath axes
5.1. Web browser rendering of Example 5.1 produced by running Example 5.2 on Example 2.3
5.2. HTML rendering of Example 5.3
5.3. HTML rendering of Example 5.6
5.4. Three pages of PDF output of compaction by Formatting Objects
5.5. Outline of the XSL-FO file produced by running Example 5.10 on Example 2.2
5.6. CSS formatting of Example 2.2
7.1. Semantic Stack
7.2. Selected information from the cellar-book
7.3. Conversion scheme from XML to RDF
8.1. JTree display of Example 2.2
10.1. E4X expression examples
C.1. Overview of organization of the DocBook XML source files of this document

List of Tables

3.1. DTD syntax reminder
3.2. XML Schema syntax reminder
3.3. RELAX NG Compact syntax reminder
3.4. Schematron syntax reminder
4.1. A selection of XPath functions
5.1. XSLT syntax reminder
7.1. RDF Triples
7.2. Comparison of Turtle brackets with triples
B.1. Regular expression syntax

List of Examples

2.1. Outline of CellarBook.xml including WineCatalog.xml in a different namespace
2.2. Excerpt of CellarBook.xml, the XML instance document holding the content of the cellar
2.3. Excerpt of WineCatalog.xml
2.4. [NamespaceExample.xml] A simplistic example of declaration and use of namespaces
3.1. [CellarBook.dtd] DTD for the cellar book, validation of the instance file in Example 2.2
3.2. [WineCatalog.dtd] DTD to validate the wine catalog, included in Example 3.1
3.3. [CellarBook.xsd] XML Schema for the cellar book, validation of the instance file in Example 2.2.
3.4. [WineCatalog.xsd] XML Schema file to validate the instance document shown in Example 2.3.
3.5. Outline of CellarBook.xsd importing Example 3.4 in a different namespace.
3.6. [CellarBook.rnc] RELAX NG Compact schema for the cellar book to validate Example 2.2.
3.7. [WineCatalog.rnc] RELAX NG Compact schema for the wine catalog to validate Example 2.3
3.8. [CellarBook.sch] ISO-Schematron for the cellar book to validate Example 2.2.
4.1. XPath expression examples
5.1. HTML tabular output of the red wines of the catalog produced by Example 5.2
5.2. [WineCatalog.xsl] XSLT stylesheet to select the red wines in the catalog
5.3. HTML output by the XSLT code is shown in Example 5.4
5.4. [CellarBook.xsl] XSLT stylesheet to produce information about the cellar
5.5. [built-in-template-rules.xsl] Built-in template rules for XSLT.
5.6. Extract of the HTML output produced by the transformation of Example 5.4 on Example 2.2
5.7. [compactHTML.xsl] XSLT transformation to produce a bulleted list from the cellar book
5.8. Text compaction of the cellar book of Example 2.2
5.9. [compact.xsl]: Stylesheet used to transform Example 2.2 into Example 5.8
5.10. [compactFO.xsl] Transformation of the cellar book into a colored nested blocks representation
5.11. [compact.css] for displaying the content of the cellar book
6.1. [WineCatalog.xq] XQuery script to select the red wines in the catalog
6.2. [CellarBook.xq] XQuery script to produce information about the cellar
6.3. [compactHTML.xq] XQuery transformation to produce a bulleted list from the cellar book
6.4. [compact.xq]: Stylesheet used to transform Example 2.2 into Example 5.8
7.1. [CBWC-RDF-S.rdf] Subject, Predicate and Object triples for the cellar book in RDF/XML.
7.2. [CBWC-RDF-S.ttl] The Turtle version of Example 7.1
7.3. [CBWC-RDF-S.rq] SPARQL queries on Example 7.2
7.4. [XML2RDF.xsl] Generic stylesheet to convert an XML file to RDF
7.5. Excerpt of CellarBook.rdf holding the content of the cellar
7.6. [CellarBook.ttl] The Turtle version of Example 7.5
7.7. [CellarBook.rq] a title
8.1. [DOMCompact.java] Text compaction of the cellar book with Java using the DOM model
8.2. [CompactErrorHandler.java] Error handler of the DOM parsing of Example 8.1
8.3. [SAXCompact.java] Text compaction of the cellar book with Java using the SAX model
8.4. [CompactHandler.java] SAX handler for text compacting an XML file such as Example 2.2
8.5. [StAXCompact.java] Text compaction of the cellar book with Java using the SAX model
8.6. [TreeViewer.java]: JTree building with DOM and StAX Processing of an XML file
8.7. [JTreeHandler.java]: JTree building with SAX Processing of an XML file
9.1. [CompactTokenizer.java]: Specialized stream tokenizer that ignores blank tokens
9.2. [DOMExpand.java]: Compact form parsing to create a DOM XML document
9.3. [SAXExpand.java]: XML document creation using SAX events
9.4. [CompactReader.java]: Compact form parsing to generate SAX events
9.5. [StAXExpand.java]: XML document creation using the StAX approach
10.1. [DOMCompact.rb] Text compaction of the cellar book with ruby using the DOM model
10.2. [SAXCompact.rb] Text compaction of the cellar book with ruby using the SAX model
10.3. [CompactHandler.rb] Ruby SAX handler for text compacting Example 2.2
10.4. [CompactTokenizer.rb] Specialized string scanner that returns tokens of compact form.
10.5. [DOMExpand.rb] Ruby compact form parsing to create an XML document
10.6. [DOMCompact.py] Text compaction of the cellar book with Python using the DOM model
10.7. [SAXCompact.py] Text compaction of the cellar book with python using the SAX model
10.8. [CompactHandler.py] Python SAX handler for text compacting Example 2.2
10.9. [XMLStreamReader.py] Text compaction of the cellar book with Python using the SAX model
10.10. [StAXCompact.py] Text compaction of the cellar book with Python using the StAX model
10.11. [CompactTokenizer.py] Specialized string scanner that returns tokens of compact form.
10.12. [DOMExpand.py] Python compact form parsing to create an XML document
10.13. [ETCompact.py] Python compaction of an XML file using ElementTree nodes.
10.14. [ETExpand.py] Python compact form parsing to create a ElementTree document
10.15. [DOMCompact.php] Text compaction of the cellar book with php using the DOM model
10.16. [compactHTML.php] HTML compaction of the cellar book with PHP using the DOM model
10.17. [SAXCompact.php] Text compaction of the cellar book with php using the SAX model
10.18. [CompactHandler.php] PHP SAX handler for text compacting Example 2.2
10.19. [StAXCompact.php] Text compaction of the cellar book with PHP using the SAX model
10.20. [CompactTokenizer.php] Specialized string scanner that returns tokens of compact form.
10.21. [DOMExpand.php] PHP compact form parsing to create an XML document
10.22. [XSLcompact.php] PHP compaction of an XML file using an XSLT stylesheet.
10.23. [SimpleXMLPath.php] SimpleXML file loading followed by PHP SimpleXML expressions
10.24. [SimpleXMLCompact.php] PHP compaction of an XML file using a SimpleXML.
10.25. [SimpleXMLExpand.php] PHP compact form parsing to create a SimpleXMLElement document
10.26. [DOMCompact.as] Text compaction of the cellar book with E4X using the DOM model
10.27. [CompactTokenizer.as] E4X specialized string scanner that returns tokens of compact form
10.28. [DOMExpand.as] E4X compact form parsing in order to create an XML document
10.29. [WineList.xml] A small wine list in XML
10.30. [WineList.json] A small wine list in JSON
10.31. [xml2json.xsl] XSLT stylesheet to convert an XML file into JSON.
10.32. [WineList.yaml] YAML version of Example 10.29
C.1. [DBTemplates.xml] Examples of the most common uses of DocBook 5 elements in this document.
D.1. XML content of the file describing the cellar book
D.2. XML content of the file describing the wine catalog

The writing of this XML tutorial started in the Fall of 2002 during my sabbatical at the Université de Grenoble and at Xerox Research Centre Europe. I wish to thank Gilles Sérasset, Christian Boitet, Pierre Isabelle and Marc Dymetman for many fruitful discussions.

Since then, the document has been improved (at least it has increased in the number of pages...) after having used it in teaching undergraduate and graduate courses at the Université de Montréal: IFT3225 and IFT6281. I especially thank Fabrizio Gotti for his careful proofreading and many insightful comments.

In 2007, I decided to start practicing what I preach by using XML technologies for the organization of the manuscript of this tutorial. Fabrizio Gotti converted the original LaTeX files to DocBook so that now PDF and HTML versions can be produced from a single set of XML source files.