Working with Parameters

 
xslt for dummies
Chapter 8 - Variables in XSLT: A Breed Apart
XSLT For Dummies
by Richard Wagner
Hungry Minds 2002
  

XSLT packs another handy device called a parameter , which is a close cousin to a variable. Parameters follow the same general behavior of variables, but they have the added flexibility of being able to change their values at the time that the XSLT stylesheet is processed .

How parameters resemble variables

A parameter is defined in XSLT by using the xsl:param element and follows the same syntax rules of variables. Therefore, to define a content-based parameter, the name attribute of xsl:param specifies the parameter name and the text between the start and end tags provides its value.

 <xsl:param name="style">Normal</xsl:param> 

Or, to define a select attribute parameter, put the value inside the select attribute of xsl:param .

 <xsl:param name="style" select="'Normal'"/> 

Think of parameters as a superset of variables because they do everything that variables doand more. For each of the preceding examples in this chapter, you can substitute xsl:param in place of xsl:variable and achieve the exact same results.

How parameters are different from variables

Parameters can be used in place of variables, but their main purpose is to provide a way to override default values at run-time, both from inside and outside of XSLT stylesheets.

Overriding parameters with the with-param element

To override the value of a parameter within a stylesheet, use the xsl:with-param element. This instruction is added to a template rule, mirroring the variable and parameter elements.

 <xsl:with-param name="style">Enhanced</xsl:with-param> 

Or you can add it like this:

 <xsl:with-param name="style" select="'Enhanced'"/> 

The typical use of xsl:with-param is to override a parameter thats been defined in a named template. To help you understand this, look at an example. Begin with the following XML fragment.

 <film name="Field of Dreams"> <director>Phil Alden Robinson</director> <writer>W.P.Kinsella, Phil Alden Robinson</writer> <year>1988</year> <runtime>107</runtime> <sound>Dolby</sound> <genre>Drama</genre> <score>9.8</score> <mpaa>PG</mpaa> </film> 

My objective is to generate an HTML document that transforms the values of the director , writer , and year elements and turns them into links ( a elements), giving each link a unique URL.

 <p><a href="legend.html#director">Phil Alden Robinson</a></p> <p><a href="legend.html#writer">W.P.Kinsella, Phil Alden Robinson</a></p> <p><a href="legend.html#year">1988</a></p> 

Parameters are well suited for handling such a task because they allow you to change their value during the transformation.

Start by creating a named template called labels that contains a parameter value called legendUrl . The labels template defines the a element and the spot where the legendUrl parameter is plugged in.

 <!-- Labels named template --> <xsl:template name="labels"> <xsl:param name="legendUrl">legend.html#main</xsl:param> <a href="{$legendUrl}"><xsl:apply-templates/></a> </xsl:template> 

After you define the labels template, add template rules for the director , writer , and year elements. For each of these, use an xsl:call-template instruction to call the labels template. Nestled comfortably inside of xsl:call-template is an xsl:with-param element that sends a new value to the legendUrl parameter. This value is then used as the href value in the resulting HTML.

 <!-- Director --> <xsl:template match="director"> <xsl:call-template name="labels"> <xsl:with-param name="legendUrl">legend.html#director</xsl:with- param> </xsl:call-template> </xsl:template> <!-- Writer --> <xsl:template match="writer"> <xsl:call-template name="labels"> <xsl:with-param name="legendUrl">legend.html#writer</xsl:with- param> </xsl:call-template> </xsl:template> <!-- Year --> <xsl:template match="year"> <xsl:call-template name="labels"> <xsl:with-param name="legendUrl">legend.html#year</xsl:with-param> </xsl:call-template> </xsl:template> <!-- Remove these elements from our results document --> <xsl:template match="sound"/> <xsl:template match="genre"/> <xsl:template match="mpaa"/> <xsl:template match="runtime"/> <xsl:template match="score"/> 

See Figure 8-2 for the formatted results of the transformation.


Figure 8-2: Using parameters allows values to be updated during transformation.

Overriding parameters from the XSLT processor

An invisible boundary for your XSLT work area throughout this book is the stylesheet itself; all the XSLT elements and instructions are contained inside the stylesheet document. However, parameters have the ability to be changed from The Great Beyondoutside the stylesheetby passing in a new value from the XSLT processor. Exactly how you do this depends on the specific XSLT processor that you use.

For example, by using the SAXON processor, you can specify a parameter name-value pair as a command line parameter.

 saxon xmlfile.xml xslfile.xsl param=value 

If you want to pass in a value for a parameter called myfontsize , the command line is

 saxon films.xml param2.xsl myfontsize=2 

The XSLT processor passes the value of 2 to the myfontsize parameter, overriding any default values that were originally provided in the param2.xsl stylesheet.

 Tip   Some XSLT processors may use a different method to pass parameter values to a stylesheet, but they all give you an identical result in the output document.

Much of the power of parameters lies in their ability to be changed from the outside. In so doing, parameters give you freedom to use values in your transformation that may not be known at the time when youre actually writing the XSLT stylesheet. Using parameters also enables you to run new transformations without changing the underlying XSLT stylesheet one iota.

  
 
 
2000-2002    Feedback


XSLT For Dummies
XSLT for Dummies
ISBN: 0764536516
EAN: 2147483647
Year: 2002
Pages: 148

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