Are These Things Really Variables?

page 68

4.5.2 Variable Scope

An xsl:variable element is scoped to the element that contains it. If an xsl:variable element is a top-level element its parent is xsl:stylesheet , it is global, and its value is visible everywhere in the stylesheet. You can also use an xsl:variable element to override the value of a global variable locally.

4.6 Using Recursion to Do Most Anything

Writing an XSLT stylesheet is different from programming in other languages. If you didnt believe that before, you probably do now. Well finish this chapter with a couple of examples that demonstrate how to use recursion to solve the kinds of problems that youre 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, well write a string replace function. This is sometimes useful when you need to escape certain characters or substrings in your output. The stylesheet well 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, well need to escape the ampersand so its 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.1 Procedural design

Three functions we could use in our template are concat , substring-before , and substring-after . To replace an ampersand with a caret and an ampersand, this would do the trick: xsl:value-of select=concatsubstring-before., amp;, amp;, substring-after., amp; The obvious problem with this step is that it only replaces the first occurrence of the ampersand. If there are two ampersands, or three, or three hundred, we need to call this method once for each ampersand in the original string. Because of the way variables work, we cant do what wed do in a procedural language: page 69 private static String strChangeString string, String from, String to { String before = , after = ; int index; index = string.indexOffrom; while index = 0 { before = string.substring0, index; after = string.substringindex + from.length; string = before + to + after; index = string.indexOffrom, index + to.length; } return string; }

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 were replacing. If the substring doesnt exist in the main string, then this is the entire string. • The replacement substring. If the substring were replacing doesnt exist in the main string, then this is blank. • Everything after the first occurrence of the substring. If the substring doesnt exist in the main string, then this is blank. The third portion is where we use recursion. If the substring were 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 dont recurse. If the substring doesnt occur in the last portion of the string, were done. Heres the design in pseudocode: replaceSubstringoriginalString, substring, replacementString { if containsoriginalString, substring firstOfString = substring-beforeoriginalString, substring else firstOfString = originalString if containsoriginalString, substring middleOfString = replacementString else middleOfString = if containsoriginalString, substring { if containssubstring-afteroriginalString, substring, substring lastOfString = replaceStringsubstring-afteroriginalString, substring, substring, replacementString else lastOfString = substring-afteroriginalString, substring } concatfirstOfString, middleOfString, lastOfString }