Procedural design Implementing a String Replace Function
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 } page 70 In the recursive approach, the function calls itself whenever theres at least one more occurrence of the substring. Each time the function calls itself, the originalString parameter is a little smaller, until eventually weve processed the complete string. Heres 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=containsoriginal, substring xsl:value-of select=substring-beforeoriginal, 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=containsoriginal, substring xsl:value-of select=replacement xsl:when xsl:otherwise xsl:textxsl:text xsl:otherwise xsl:choose xsl:variable xsl:variable name=last xsl:choose xsl:when test=containsoriginal, substring xsl:choose xsl:when test=containssubstring-afteroriginal, substring, substring xsl:call-template name=replace-substring xsl:with-param name=original xsl:value-of select=substring-afteroriginal, 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-afteroriginal, substring xsl:otherwise xsl:choose xsl:when xsl:otherwise xsl:textxsl:text xsl:otherwise xsl:choose xsl:variable xsl:value-of select=concatfirst, 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 weve discussed in this chapter, including branching statements, variables, invoking templates by name, and passing parameters.Parts
» O'Reilly-XSLT-Mastering.XML.Transformati... 2264KB Mar 29 2010 05:03:43 AM
» An XML document must be contained in a single element
» XML declarations Document Type Definitions DTDs and XML Schemas
» Well-formed versus valid documents
» Tags versus elements XML Document Rules
» Namespaces XML Document Rules
» The Extensible Stylesheet Language XSL
» Document Object Model DOM Level 1
» Document Object Model DOM Level 2
» Namespaces in XML XML Standards
» Associating stylesheets with XML documents
» Installing Xalan Getting Started
» Our Sample Document A Sample Stylesheet
» Transforming the XML Document
» Stylesheet Results Transforming Hello World
» Parsing the Stylesheet How a Stylesheet Is Processed
» Parsing the Transformee How a Stylesheet Is Processed
» Lather, Rinse, Repeat How a Stylesheet Is Processed
» The xsl:template for greeting Elements
» Built-in template rule for element and root nodes
» Built-in template rule for modes
» Built-in template rule for text and attribute nodes
» Top-Level Elements Stylesheet Structure
» Other Approaches Stylesheet Structure
» The Hello World Java Program
» Goals of This Chapter Summary
» The Root Node The XPath Data Model
» Element Nodes The XPath Data Model
» Attribute Nodes The XPath Data Model
» Text Nodes The XPath Data Model
» Comment Nodes The XPath Data Model
» Processing Instruction Nodes The XPath Data Model
» Namespace Nodes The XPath Data Model
» Simple Location Paths Location Paths
» Relative and Absolute Expressions
» Selecting attributes Selecting Things Besides Elements with Location Paths
» Selecting the text of an element
» Selecting comments, processing instructions, and namespace nodes
» Using Wildcards Location Paths
» Numbers in predicates Predicates
» Functions in predicates Predicates
» Attribute Value Templates XPath Datatypes
» Output View The XPath View of an XML Document
» The Stylesheet The XPath View of an XML Document
» Summary XPath: A Syntax for Describing Needles and Haystacks
» Converting to boolean values
» Boolean examples The xsl:if Element
» xsl:for-each example The xsl:for-each Element
» How It Works Invoking Templates by Name
» Templates à la Mode Invoking Templates by Name
» Defining a Parameter in a Template
» Microsofts XSLT tools Global Parameters
» Setting global parameters in a Java program
» Are These Things Really Variables?
» Procedural design Implementing a String Replace Function
» Recursive design Implementing a String Replace Function
» Template Design Implementation A Stylesheet That Emulates a for Loop
» The Complete Example A Stylesheet That Emulates a for Loop
» XML Input A Stylesheet That Generates a Stylesheet That Emulates a for Loop
» Template Design A Stylesheet That Generates a Stylesheet That Emulates a for Loop
» Complications A Stylesheet That Generates a Stylesheet That Emulates a for Loop
» Summary Branching and Control Elements
» The ID, IDREF, and IDREFs Datatypes
» An XML Document in Need of Links
» A Stylesheet That Uses the id Function
» Limitations of IDs Generating Links with the id Function
» Defining a key Generating Links with the key Function
» A Slightly More Complicated XML Document in Need of Links
» The key function and the IDREFS datatype
» Solution 1: Replace the IDREFS datatype
» Solution 2: Use the XPath contains function
» Solution 3: Use recursion to process the IDREFS datatype
» Solution 4: Use an extension function
» Advantages of the key Function
» An Unstructured XML Document in Need of Links
» The generate-id Function Generating Links in Unstructured Documents
» Summary Creating Links and Cross-References
» Our First Example Sorting Data with xsl:sort
» Whats the deal with that syntax?
» Attributes The Details on the xsl:sort Element
» Another Example Sorting Data with xsl:sort
» Our First Attempt Grouping Nodes
» A Brute-Force Approach Grouping with xsl:variable
» Summary Sorting and Grouping Elements
» Recursive design An Aside: Doing Math with Recursion
» Generating output to initialize a variable
» Overview Invoking the document Function
» The document Function and Sorting
» Implementing Lookup Tables More Sophisticated Techniques
» Grouping Across Multiple Documents
» Summary Combining XML Documents
» Example: Generating multiple output files
» Example: Using extension functions from multiple processors
» Example: A library of trigonometric functions
» Example: Writing extensions in other languages
» Fallback Processing Extension Elements, Extension Functions, and Fallback Processing
» Extending the Saxon Processor
» Generating JPEG Files from XML Content
» About the Toot-O-Matic Case Study: The Toot-O-Matic
» Make It Easier to Create Tutorials
» Individual Panels Tutorial Layout
» Email Panel Zip File PDF Files
» Individual Panels XML Document Design
» Stylesheets and Modes XSLT Source Code
» Initializing Global Variables XSLT Source Code
» Generating the Main Menu Panel
» Generating the Section Indexes
» Generating the Individual Panels
Show more