Chapter 9. Named Templates, Parameters, and Variables

Chapter 9. Named Templates, Parameters, and Variables

graphics/chic01.gif

This chapter looks at some powerful features of XSLT, known as templates, stylesheet parameters, and variables. These topics are all related you use parameters with named templates, and variables and parameters are practically the same, except for the way theyre created.

When you give a template a name , you can specifically call it, using the <xsl:call-template> element. A template is applied when it is called, so rather than rely on the default processing of the stylesheet by the XSLT processor, you can determine when a template is applied, as well as which one is applied. For example, you might have multiple templates that would match the same node set, and want to select the template or templates you want to use. Its a little like using modes, but it gives you more control.

When you call a template, you can customize what it does through the use of parameters . For example, you may want the text in the text nodes created by the template to be in a specific language, such as English, German, or French, and so might create a new parameter named language . When you call a named template that youve set up to handle this parameter, you might set language to en, de, or fr, then call the template with the help of the <xsl: with-param > element. In the named template itself, you declare the parameter used by the template, language , with the <xsl:param> element. After its been declared, you are free to refer to the value in the language parameter as $language and use it in XPath expressions. Well see numerous examples showing how this works in this chapter.

Variables are a lot like parameters, except that you create them differently. Also, parameters are generally used with named templates, whereas variables are used more generally , in any kind of XPath expression. As in programming languages, you can store values in variables in XSLT and then refer to them later. Note an important point, however: except in specific circumstances, you cannot change the value stored in a variable. (In fact, some XSLT authors believe its a misnomer to call them variables.)

Variables are useful for holding values that take a lot of processing time to create but that are referred to many times in your stylesheet. Rather than re-create those values each time theyre needed, you can store them in a variable and just refer to the value stored in the variable. As with parameters, you refer to the value in a variable by adding the prefix $. For example, if you have a variable named sandwich , you can refer to the value stored in this variable as $sandwich . As with parameters, you can store data with any of the four XPath data types in variables. Variables are also useful for storing values that are changed later in a template. For example, . usually refers to the context node for the template, but inside an <xsl:for-each> element, . refers to the current node that the element is processing, not the context node for the whole template. To refer to the context node, you can store it in a variable named contextnode before entering the <xsl:for-each> loop, and then refer to it in the loop as $contextnode .

In addition to the four XPath data types, youll also see a data type supported in XSLT 1.0, but not XSLT 1.1: result tree fragments. You create result tree fragments with the <xsl:variable> or <xsl:with-param> , and they can be convenient in some cases, as youll see in this chapter.

Finally, youll also see the <xsl:key> element in this chapter. You first saw this element briefly in Chapter 4, but its time for a closer look here.

Thats all the introduction you need; its time to get to work, starting with variables.



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net