Defining a Parameter in a Template

page 63 Notice that in the previous sample, we put single quotes around the value blue , but we didnt do it around the value 150 . Without the single quotes around blue , the XSLT processor assumes we want to select all the blue elements in the current context, which is probably not what we want. The XSLT processor is clever enough to realize that the value 150 cant be an XML element name the XML 1.0 Specification says element names cant begin with numbers, so we dont need the single quotes around a numeric value. Try to keep this in mind when youre using parameters. Youll probably forget it at some point, and youll probably go nuts trying to figure out the strange behavior youre getting from the XSLT processor. The second way to define a default value for a parameter is to include content inside the xsl:param element: template name=addTableCell xsl:param name=bgColor xsl:textbluexsl:text xsl:param xsl:param name=width xsl:value-of select=7+8xsl:text0xsl:text xsl:param xsl:param name=content td width={width} bgcolor={bgColor} xsl:apply-templates select=content td template In this example, we used xsl:text and xsl:value-of elements to define the default values of the parameters. Out of sheer perverseness, we defined the value of width as the concatenation of the numeric expression 7+8 , followed by the string 0. This example produces the same results as the previous one.

4.4.2 Passing Parameters

If we invoke a template by name, which is similar to calling a subroutine, well need to pass parameters to those templates. We do this with the xsl:with-param element. For example, lets say we want to call a template named draw-box, and pass the parameters startX , startY , endX , and endY to it. Heres what wed do: xsl:call-template name=draw-box xsl:with-param name=startX select=50 xsl:with-param name=startY select=50 xsl:with-param name=endX select=97 xsl:with-param name=endY select=144 xsl:call-template In this sample, weve called the template named draw-box with the four parameters we mentioned earlier. Notice that up until now, xsl:call-template has always been an empty tag; here, though, the parameters are the content of the xsl:call-template element. If you want, you can do the same thing with xsl:apply-templates . We used the xsl:with- param element with the xsl:call-template element here, but you can also use it with xsl:apply-templates . page 64 If were going to pass parameters to a template, we have to set up the template so that it expects the parameters were passing. To do this, well use the xsl:param element inside the template. Here are some examples: xsl:template name=draw-box xsl:param name=startX xsl:param name=startY select=0 xsl:param name=endX 10 xsl:param xsl:param name=endY 10 xsl:param ... xsl:template A couple of notes about the xsl:param element: • If you define any xsl:param elements in a template, they must be the first thing in the template. • The xsl:param element allows you to define a default value for the parameter. If the calling template doesnt supply a value, the default is used instead. The last three xsl:param elements in our previous example define default values. • The xsl:param element has the same content model as xsl:variable . With no content and no select attribute, the default value of the parameter is an empty string . With a select attribute, the default value of the parameter is the value of the select attribute. If the xsl:param element contains content, the default value of the parameter is the content of the xsl:param element.

4.4.3 Global Parameters

XSLT allows you to define parameters whose scope is the entire stylesheet. You can define default values for these parameters and you can pass values to those parameters externally to the stylesheet. Before we talk about how to pass in values for global parameters, well show you how to create them. Any parameters that are top-level elements any xsl:param elements whose parent is xsl:stylesheet are global parameters. Heres an example: ?xml version=1.0? xsl:stylesheet version=1.0 xmlns:xsl=http:www.w3.org1999XSLTransform xsl:output method=text xsl:param name=startX xsl:param name=baseColor xsl:variable name=newline xsl:text xsl:text xsl:variable xsl:template match= xsl:value-of select=newline xsl:textGlobal parameters examplexsl:text xsl:value-of select=newline xsl:value-of select=newline xsl:textThe value of startX is: xsl:text xsl:value-of select=startX xsl:value-of select=newline xsl:textThe value of baseColor is: xsl:text xsl:value-of select=baseColor xsl:value-of select=newline xsl:template xsl:stylesheet