Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Analyze with XSLT, Part 1: Analyze non-XML data with XSLT

Create string parsing routines to convert documents into XML elements

Chuck White (chuck@tumeric.net), XSLT consultant and Web engineer, Freelance Developer
Chuck White, a Studio B author, has been working with XML since before its official inception in February, 1998. He was co-author of Mastering XML Premium Edition (with Linda Burman and the W3C's XML Activity Lead, Liam Quin) and author of Mastering XSLT, both from Sybex Books. His latest books are Developing Killer Web Apps with Dreamweaver MX & C# (also for Sybex Books) and HTML, XHTML, and CSS Bible, 3rd Edition for Wiley, for which he is co-author with Steve Schafer. Chuck is currently working with the XSL Team at eBay as a project consultant and Web engineer.

Summary:  This 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 such documents as weblogs and Web configuration files into XML for improved readability and programmatic access.

View more content in this series

Date:  16 Dec 2003
Level:  Introductory PDF:  A4 and Letter (106 KB | 28 pages)Get Adobe® Reader®

Activity:  6384 views
Comments:  

Introduction

Should I take this tutorial?

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.


What is this tutorial about?

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.


Tools

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.

1 of 12 | Next

Comments



Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=138216
TutorialTitle=Analyze with XSLT, Part 1: Analyze non-XML data with XSLT
publish-date=12162003
author1-email=chuck@tumeric.net
author1-email-cc=dwxed@us.ibm.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.