7.1 Defining Variables and Parameters

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:

  1. The required name attribute that binds a name to a value of the variable. This is a QName.

  2. The optional select attribute that can contain an expression that defines the value of the variable.

In addition, all three elements also provide two general ways to define a value:

  1. You can define a value using an expression in the select attribute.

  2. You can also define a value using something called a result tree fragment with a template in the content of the element. As discussed in Chapter 4, a result tree fragment is an XSLT 1.0 datatype that defines a fragment of text or markup.

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.

By the way, you can't use circular definitions when defining a variable. This means that you can't define a variable by referencing itself.


Empty Values

A variable can contain just an empty string. For example, if you define a parameter with no apparent value, as in:

<xsl:param name="discount"/>

the value of discount is initially defined as an empty string. You could also write an empty definition as:

<xsl:param name="discount" select="''"/>

I'm showing you how to do this with param because param may be defined with a default value that you can later change. If you use variable to define an empty value, you can't change it because such variables are immutable. You may want to use an empty variable, but I can't think of a lot of use cases for it. Any empty value makes sense for use with param and with-param, as you will see in Section 7.4, later in this chapter.


7.1.1 Defining Default Values for Parameters

Unlike 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 Templates

As 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 variables

For 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).

If you enclose the value of select in just double or single quotes, without any internal quotes, the value is interpreted as a node-set and not as a string. Because / is not a legal XML name character, n/a would be interpreted as a location path the element n with a child a probably not what you are after.


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 variables

When 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.



Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

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