Variables

A variable is a named object that holds a value. A number of references can be made to the variable wherever that value is needed, and each reference is replaced by the value when the stylesheet is processed.

Use of a variable could be considered whenever the same value appears in two or more places in the stylesheet, and there is no reason to believe that the values in each place will ever be different from each other. For example, if important text is always presented on a red background, and there are a number of places where, for this purpose, the colour red is specified in the stylesheet, then the CSS instruction 'background-color:red' could be replaced with the variable name 'important'. There are two clear benefits. First, the value is replaced by a meaningful name ('important') that clarifies the purpose of the instruction. The stylesheet is therefore more legible, and is easier to maintain. Second, if the value needs to be changed, with a variable the edit is made in a single place (where the variable is defined), reducing the possibility of errors arising from forgetting to change one of the values, or from miskeying one of the updates.

Attribute definitions

The first way to define a variable is using the Variable element. The Name attribute holds the name of the variable. In the following example, a variable called 'important' is being defined:

<variable
						name="important" ...>
  ...
</variable>

Note that, unlike variables in programming languages, these variables do not actually 'vary', in the sense that their values cannot change while the stylesheet is in use. However, it is possible to declare a new variable, with the same name but holding a different value, that overrides the original definition within a specific part of the stylesheet. A definition made within a template overrides a global definition. But it is not possible to override a variable that is already defined within a template, or a global variable with the another global variable of the same name. The first two definitions in the following example are therefore valid, but the third and fourth are not:

<variable name="blue" ...>
<!-- GLOBAL DEFINITION APPLIES HERE -->
<template...>
  ... <!-- GLOBAL DEFINITION APPLIES HERE -->
  <variable name="blue" ...>
  ... <!-- OVERRIDE DEFINITION APPLIES HERE -->
  <variable name="blue" ...> <!-- ILLEGAL -->
</template>
<!-- GLOBAL DEFINITION APPLIES HERE -->
<variable name="blue" ...> <!-- ILLEGAL -->

Simple variables

A variable can hold a simple text string. For example, the string 'background-color:red' can be stored in a variable called 'important'. This is done by entering the value into the Variable element:

<variable name="important">background-color:red</variable>

Variable references

The variable is used by referring to the variable name, and the name is replaced by the variable value when the stylesheet is processed. A reference to a variable is identified using a '$' prefix. For example, '$important' refers to the 'important' variable. Such references can appear wherever an XPath expression is allowed.

To insert a string attribute value into the middle of a text string, the Value Of element can be used. The variable name is placed in the Select attribute:

The style '<value-of select="$important"/>' is used for
important text.

The result is:

The style 'background-color:red' is used for important text.

At first sight, this approach may appear to be invalid. The value of the Select attribute should be an expression, but in the example above the replacement value for the variable reference is the string 'background-color:red', which is clearly not an expression. However, a variable reference within this element has an implied 'string('…')' function around it, so that the example below (using no variable) is equivalent:

The style '<xsl:value-of select="string('background-
color:red')"/>' is used for important text.

Variables can be referenced from most instruction elements, but cannot be used in the Match and Use attributes.

Variables in output attributes

The approach described above could be used to insert values into output element attributes, as in the following example where the important style definition is inserted into the STYLE attribute:

<xsl:template match="note[@type='3']">
  <P>
    <xsl:attribute name="STYLE">
      <xsl:value-of select="$important"/>
    </xsl:attribute>
    <xsl:apply-templates/>
  </P>
</xsl:template>

   <P STYLE="background-color:red">...</P>

However, there is a simpler way to achieve this effect. Variable values can also be inserted directly into output attribute values, using an expression:

<xsl:template match="note[@type='3']">
  <P STYLE='{$important}'>
    <xsl:apply-templates/>
  </P>
</xsl:template>

Result-tree fragments

Variables can also contain result-tree fragments. These are XML fragments that conform to the output format. For example, a copyright notice in HTML format may be enclosed within a paragraph element, and contain bold text.

When the value consists of an output element, as in the following example, it is considered to be a result-tree fragment:

<xsl:variable name="copyright">
  <P>(c) <B>ACME</B> Corp</P>
</xsl:variable>

This distinction is critical, because the way in which the value is output differs. When the value is a string, the Value Of element is used to reference it (discarding any embedded markup). When the value is a result-tree fragment, the Copy Of element should be used instead:

<xsl:template match="book"
  <HTML>
    <xsl:apply-templates/>
    <xsl:copy-of select="$copyright"/>
  </HTML>
</xsl:template>

In theory, the Copy Of element should work for simple strings as well as result-tree fragments, but it has been known to fail for simple text strings in some XSLT processors.

Expression values

An attribute value can be the result of an expression. The Select attribute is used to hold the expression. In this scenario, the Attribute element should be empty, as it is not possible for an attribute to have two values at the same time.

One use for this technique is to set flags that switch on and switch off parts of a stylesheet as desired. For example, consider a variable called 'debug', which holds an expression of either 'true()' or 'false()'. When the variable is given the value 'true()', all the debug paragraphs are to be output; when set to false, they are to be suppressed. This variable can be referenced in If tests:

<xsl:variable name="debug" select="true()"/>

<xsl:template match="para">
  <xsl:if test="$debug">
    <P>SOURCE WAS 'para' ELEMENT</P>
  </xsl:test>
  <P><xsl:apply-templates/></P>
</xsl:template>

However, in other circumstances it is important to understand that the expression is actually applied once the value has been copied over the references, not when the variable is defined. This means that context is important.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset