Introduction
This tutorial is the first of multi-part series that details the benefits of using XSL Transformations (XSLT) through the example of a fictional research team known as MindMap. The tutorial explores how to create string parsing routines in XSLT so that you can tokenize straight, non-XML text, thus turning that text into a series of XML elements. Specifically, this tutorial examines how to convert documents such as weblogs and Web configuration files into XML for improved readability and programmatic access.
Developers should have a basic understanding of XML in general (you can get a basic grounding in XML itself through the Introduction to XML tutorial, http://www.ibm.com/developerworks/edu/x-dw-xmlintro-i.html), and be familiar with basic XSLT tasks. The more knowledge you have of XSLT, the better, but you should be able to follow the tutorial with a less than intermediate-level knowledge of XSLT. If you don't know XSLT at all, try the Create multi-purpose Web content with XSLT tutorial first (http://www.ibm.com/developerworks/edu/x-dw-xwebxslt-i.html).
In addition, you'll certainly find the last section on regular expressions using XSLT 2.0 easier to follow if you have some background in regular expressions, but this isn't a prerequisite for successfully completing the tutorial.
Welcome to the MindMap Research Team's explorations into advanced XSLT. XSLT 1.0 is much more powerful than a lot of people realize, and developers often use JavaScript to do things XSLT can do effectively. The MindMap Research Team uses XSLT as part of its analytical toolbox for interpreting data related to cognitive processes. The goal is to maximize the team's flexibility by leveraging the ability to change algorithms and methods without having to recompile applications. The MindMap team contracts with universities to provide a variety of highly customized software-based analytical tools, and as such it deals with several aspects of the analytical process, from data acquisition to visualization to tying the data in to other resources. This tutorial examines how to turn a basic text file into XML and manipulate its data. Future tutorials will examine more complex scenarios, including detailed analysis, conversion of data to SVG, and binding data to Web services.
To turn a text file into XML, the MindMap team considered two alternatives. First they considered Perl, but because they preferred a pure XML solution they chose XSLT. They were using XSLT anyway to do the analysis that you'll be learning about in future tutorials in this series. This tutorial demonstrates how, like the MindMap team, you can roll your own tokenizer. A tokenizer is simply a software routine that lets you create XML elements out of non-XML documents. (For example, you can create a tokenizer using Perl.) In this tutorial, you'll use XSLT to accomplish the following:
- Pass log or configuration data into an XML document
- Convert a comma-delimited text file to XML elements
- Build string search routines using XPath 1.0 functions
- Parse string data to display specific substring-based information
- Turn your string search routines and string parsers into re-usable templates
- Find and use generic string templates, as well as extensions to EXSLT that enable you to perform calculations on nodes and process strings with less code than basic XSLT
- Use regular expressions through XPath 2.0
As an added bonus, I've included some general XSLT tricks of the trade in the tutorial summary section.
Make sure you install and test the following tools before beginning the tutorial:
- An XML parser (http://xml.coverpages.org/publicSW.html#xmlToolsTOC) and an XSLT processor (http://www.xml.com/pub/a/2001/03/28/xsltmark/results.html). This tutorial assumes you have and know how to use each of these.
- A text editor that supports UTF-8 (http://www.cl.cam.ac.uk/~mgk25/unicode.html) or an XML editor (http://www.garshol.priv.no/download/xmltools/cat_ix.html#SC_XMLEditors).
- For XSLT 2.0 examples, Saxon (http://saxon.sourceforge.net/), an XSLT 2.0-compliant processor (http://www.w3.org/TR/xslt20/).
- Optionally, a Perl interpreter for running the initial example (http://ftp.linux.cz/pub/perl/ports/).
- Finally, you'll want to download dataxsltsourcecode.zip to obtain the files referred to throughout this tutorial. You should download them before starting the tutorial so that you can follow along more easily, because you'll often encounter code fragments taken from longer files and it helps to view those fragments from the context of the overall file.

