Before going any deeper into the subject, there are a few things I'd like to discuss that apply to the variable, param, and with-param elements. To begin with, all three elements have just two attributes:
In addition, all three elements also provide two general ways to define a value:
After you define a variable, you can reference its value by preceding the variable name with a dollar sign, such as in $discount. I'll cover both ways to define a value and discuss the differences.
7.1.1 Defining Default Values for ParametersUnlike a value defined with the variable element, a value defined with the param element can have a default value that you can change. Nevertheless, a parameter is not required to have an explicit, default value; it can just be empty. Said another way, if a parameter does not have an explicitly defined value, the processor will give it a value of a zero-length string. When you define a global parameter on the top level, you can pass in a new value when the transformation is performed that replaces the default value using a mechanism provided by the XSLT processor; and when you define a local parameter in a template, you can pass in a new value from another template by using with-param. You will see examples of how this works in the later sections, Section 7.3 for global variables, and Section 7.4 for local variables. 7.1.2 Defining Values with Expressions and TemplatesAs I mentioned earlier, you can define a variable value with an expression in a select attribute or with a template in element content (as you will soon see section Section 7.1.2.2). However, you cannot define a value using both an expression and a template at the same time. In other words, you cannot use the select attribute together with element content to define a single variable, as they are mutually exclusive. 7.1.2.1 Using the select attribute to define variablesFor example, the following declaration defines a value using an expression in a select attribute: <xsl:variable name="discount" select="0.40 + 0.30"/> The expression adds the numbers 0.40 and 0.30 and the resulting number value of 0.70 is bound to the discount variable. An XSLT processor automatically knows that this variable is a number. Likewise, the following variable would be interpreted containing the number 50: <xsl:variable name="discount" select="50"/> You can also bind a string to a variable explicitly using embedded quotation marks: <xsl:variable name="discount" select="'n/a'"/> Notice the single quotes inside the double quotes in the value of select. This binds the string value n/a to discount. You could also write the declaration in this way, with double quotes inside of single quotes: <xsl:variable name='discount' select='"n/a"'/> Either single or double quotes are fine, but you aren't allowed to mix them (that is, name='discount" is illegal).
Because select contains an expression, you can use arithmetic, functions, even references to other variables, when defining a value for a variable with select. Here's yet another example showing a slightly more complex expression that defines a parameter: <xsl:param name="discount" select="floor($option)+0.05"/> You can also specify a location path in select, as in: <xsl:param name="discount" select="catalog/value"/> This variable would extract the content of the value element for its value. Another possibility is to use the document( ) function in select like this: <xsl:param name="discount" select="document('discount.xml')"/> With this, the value of discount is picked up from the external document discount.xml: <value>0.10</value> Though it is discussed elsewhere in the book, you'll learn more about the document( ) function in Chapter 13. 7.1.2.2 Using result tree fragments to define variablesWhen you define a variable using a template in element content, such content is a result tree fragment. Because it is defined as a template, a result tree fragment can be a node-set consisting of markup, which has its own root element. The following declaration uses a result tree fragment to define a variable: <xsl:variable name="discount"> <xsl:element name="discount">0.10</xsl:element> </xsl:variable> Here element is used to create an element named discount with the content 0.10 for the result tree. The result tree fragment type is defined by XSLT, not by XPath. It is called a temporary tree in XSLT 2.0, and it can be manipulated by an XSLT 2.0 processor in more sophisticated ways than a result tree fragment can be manipulated by an XSLT 1.0 processor. (An XSLT 2.0 temporary tree, however, cannot be manipulated by an XSLT 1.0 processor.) See the section Section 7.5, later in this chapter for a working example. |