Are These Things Really Variables?
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 }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