xsl:for-each example The xsl:for-each Element
4.3 Invoking Templates by Name
Up to this point, weve always used XSLTs xsl:apply-templates element to invoke other templates. You can think of this as a limited form of polymorphism; a single instruction is invoked a number of times, and the XSLT processor uses each node in the node-set to determine which xsl:template to invoke. Most of the time, this is what we want. However, sometimes we want to invoke a particular template. XSLT allows us to do this with the xsl:call-template element.4.3.1 How It Works
To invoke a template by name, two things have to happen: • The template you want to invoke has to have a name . • You use the xsl:call-template element to invoke the named template. Heres how to do this. Say we have a template named createMasthead that creates the masthead of a web page. Whenever we create an HTML page for our web site, we want to invoke the createMasthead template to create the masthead. Heres what our stylesheet would look like: xsl:template name=createMasthead -- interesting stuff that generates the masthead goes here -- xsl:template ... xsl:template match= html head titlexsl:value-of select=titletitle head body xsl:call-template name=createMasthead ... Named templates are extremely useful for defining commonly used markup. For example, say youre using an XSLT stylesheet to create web pages with a particular look and feel. You can write named templates that create the header, footer, navigation areas, or other items that define how your web page will look. Every time you need to create a web page, simply use xsl:call-template to invoke those templates and create the look and feel you want. Even better, if you put those named templates in a separate stylesheet and import the stylesheet with either xsl:import or xsl:include , you can create a set of stylesheets that generate the look and feel of the web site you want. If you decide to redesign your web site, redesign the stylesheets that define the common graphical and layout elements. Change those stylesheets, regenerate your web site, and voila You will see an instantly updated web site. See Chapter 9 for an example.4.3.2 Templates à la Mode
The XSLT xsl:template element has a mode attribute that lets you process the same set of nodes several times. For example, we might want to process h1 elements one way when we generate a table of contents, and another way when we process the document as a whole. We could use the mode attribute to define different templates for different purposes: xsl:template match=h1 mode=build-toc -- Template to process the h1 element for table of contents -- xsl:template page 62 xsl:template match=h1 mode=process-text -- Template to process the h1 element along with the rest -- -- of the document -- xsl:template We can then start applying templates with the mode attribute: xsl:template match= html body h1Table of Contentsh1 ul xsl:apply-templates select=h1 mode=build-toc ul xsl:apply-templates select= mode=process-text body html xsl:template This style of coding makes maintenance much easier; if the table of contents isnt generated correctly, the templates with mode=build-toc are the obvious place to start debugging. See Chapter 9 for a more detailed discussion of the mode attribute.4.4 Parameters
The XSLT xsl:param and xsl:with-param elements allow you to pass parameters to a template. You can pass templates with either the call-template element or the apply- templates element; well discuss the details in this section.4.4.1 Defining a Parameter in a Template
To define a parameter in a template, use the xsl:param element. Heres an example of a template that defines two parameters: xsl:template name=calcuateArea xsl:param name=width xsl:param name=height xsl:value-of select=width height xsl:template Conceptually, this is a lot like writing code in a traditional programming language, isnt it? Our template here defines two parameters, width and height , and outputs their product. If you want, you can define a default value for a parameter. There are two ways to define a default value; the simplest is to use a select attribute on the xsl:param element: template name=addTableCell xsl:param name=bgColor select=blue xsl:param name=width select=150 xsl:param name=content td width={width} bgcolor={bgColor} xsl:apply-templates select=content td template In this example, the default values of the parameters bgColor and width are blue and 150 , respectively. If we invoke this template without specifying values for these parameters, the default values are used. Also notice that we generated the values of the width and bgcolor attributes of the HTML td tag with attribute value templates, the values in curly braces. For more information, see Section 3.3 in Chapter 3 .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