home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeXSLSearch this book

4.6. Using Recursion to Do Most Anything

Writing an XSLT stylesheet is different from programming in other languages. If you didn't believe that before, you probably do now. We'll finish this chapter with a couple of examples that demonstrate how to use recursion to solve the kinds of problems that you're probably used to solving with procedural programming languages.

4.6.1. Implementing a String Replace Function

To demonstrate how to use recursion to solve problems, we'll write a string replace function. This is sometimes useful when you need to escape certain characters or substrings in your output. The stylesheet we'll develop here transforms an XML document into a set of SQL statements that will be executed at a Windows command prompt. We have to do several things:

Put a caret (^) in front of all ampersands (&)
On the Windows NT and Windows 2000 command prompt, the ampersand means that the current command has ended and another is beginning. For example, this command creates a new directory called xslt and changes the current directory to the newly created one:

mkdir xslt & chdir xslt

If we create a SQL statement that contains an ampersand, we'll need to escape the ampersand so it's processed as a literal character, not as an operator. If we insert the value Jones & Son as the value of the company field in a row of the database, we need to change it to Jones ^& Son before we try to run the SQL command.

Put a caret (^)) in front of all vertical bars (|)
The vertical bar is the pipe operator on Windows systems, so we need to escape it if we want it interpreted as literal text instead of an operator.

Replace any single quote (') with two single quotes ('')
This is a requirement of our database system.

4.6.1.2. Recursive design

To implement a string replace function with recursion, we take a modified version of the approach we used here. We build the replaced string in three pieces:

  • Everything up to the first occurrence of the substring we're replacing. If the substring doesn't exist in the main string, then this is the entire string.

  • The replacement substring. If the substring we're replacing doesn't exist in the main string, then this is blank.

  • Everything after the first occurrence of the substring. If the substring doesn't exist in the main string, then this is blank.

The third portion is where we use recursion. If the substring we're replacing occurs in that part of the main string, we call the substring replace function on the last of the string. The key here, as with all recursive functions, is that we have an exit case, a condition in which we don't recurse. If the substring doesn't occur in the last portion of the string, we're done.

Here's the design in pseudocode:

replaceSubstring(originalString, substring, replacementString)
{
  if (contains(originalString, substring))
    firstOfString = substring-before(originalString, substring)
  else
    firstOfString = originalString

  if (contains(originalString, substring))
    middleOfString = replacementString
  else
    middleOfString = ""

  if (contains(originalString, substring))
  {
    if (contains(substring-after(originalString, substring), substring))
     lastOfString = replaceString(substring-after(originalString, substring), 
                                  substring, replacementString)
    else
      lastOfString = substring-after(originalString, substring)
  }
  concat(firstOfString, middleOfString, lastOfString)
}

In the recursive approach, the function calls itself whenever there's at least one more occurrence of the substring. Each time the function calls itself, the originalString parameter is a little smaller, until eventually we've processed the complete string. Here's the complete template:

<xsl:template name="replace-substring">
  <xsl:param name="original"/>
  <xsl:param name="substring"/>
  <xsl:param name="replacement" select="''"/>
  <xsl:variable name="first">
    <xsl:choose>
      <xsl:when test="contains($original, $substring)">
        <xsl:value-of select="substring-before($original, $substring)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$original"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="middle">
    <xsl:choose>
      <xsl:when test="contains($original, $substring)">
        <xsl:value-of select="$replacement"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text></xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="last">
    <xsl:choose>
      <xsl:when test="contains($original, $substring)">
        <xsl:choose>
          <xsl:when test="contains(substring-after($original, $substring), 
                                   $substring)">
            <xsl:call-template name="replace-substring">
              <xsl:with-param name="original">
                <xsl:value-of select="substring-after($original, $substring)"/>
              </xsl:with-param>
              <xsl:with-param name="substring">
                <xsl:value-of select="$substring"/>
              </xsl:with-param>
              <xsl:with-param name="replacement">
                <xsl:value-of select="$replacement"/>
              </xsl:with-param>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="substring-after($original, $substring)"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text></xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:value-of select="concat($first, $middle, $last)"/>
</xsl:template>

This style of programming takes some getting used to, but whatever you want to do can usually be done. Our example here is a good illustration of the techniques we've discussed in this chapter, including branching statements, variables, invoking templates by name, and passing parameters.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.