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 profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

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:  11844 views
Comments:  

Making your string search routines and string parsers re-usable templates

Creating a generic template

Making search routines and string parsing templates reusable is a crucial part of using your resources wisely. No matter what size your organization is, having a library of well-documented, re-usable templates is a great time-saver.

In fact, the MindMap team loved the br-replace template (which you saw earlier in the tutorial) so much that they dropped it into their directory of generic XSLT templates and now use it nearly every day. Generic templates act as function libraries and should be made in as generic a way as possible so they can be reused. Consider the template below -- you don't define any of the matching nodes in this initial template; a good generic template should be able to stand on its own, callable by any file. This isn't always possible, but it's always desirable. You should be able to use this template with any string of text that has breaks in it:

<xsl:template name="links">
  <xsl:param name="String" />
<xsl:choose>
   <xsl:when test="contains($String,',')">
     <a href="#id{substring-before($String,',')}">
       <xsl:value-of 
             select="substring-before($String,',')" />
     </a>
     <xsl:text></xsl:text>
     <xsl:call-template name="links">
       <xsl:with-param name="String" 
             select="substring-after($String,',')" />
     </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
     <a href="#id{$String}">
       <xsl:value-of select="$String" />
      </a>
     <xsl:text></xsl:text>
   </xsl:otherwise>
  </xsl:choose>
  </xsl:template>


Calling a generic template

If you're entering text into a text editor, name the file links.xsl or download it from the source package, available in Tools. Then, create a new XSLT file named call-links.xsl, and enter the following code:

<xsl:stylesheet version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" />
  <xsl:include href="links.xsl" />
  <xsl:template match="/">
    <html>
    <head>
      <title>Links</title>
    </head>
    <xsl:apply-templates select="WebConfig" />
    </html>
  </xsl:template>

  <xsl:template match="WebConfig">
    <body>
      <xsl:call-template name="links">
        <xsl:with-param name="String" select="." />
      </xsl:call-template>
    </body>
  </xsl:template>
  ...

Note that not all the code is included here and as the code stands it is not yet well formed. You'll need to either finish it yourself or download the file. As you can see, only the XSLT document doing the calling contains any path information. You can try this on just about any XML document with lots of text. You don't need to change the named template, just the parameters on the calling template.

6 of 12 | Previous | Next

Comments



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