Variables and named templates

A stylesheet often contains a number of templates that produce identical or very similar output. XSLT includes some mechanisms for avoiding such redundancy.

Variables

A 'variable' is a container of information that has a name. Whenever the variable is referenced in the stylesheet, the reference is replaced by the value of the variable.

Variables are declared using the Variable element. The name of the variable is given in the Name attribute, and the value of the variable may be the content of the element. The following example creates a variable called 'Colour', and gives it the value 'red':

<variable name="Colour">red</variable>

Alternatively, the value can be generated from an expression using a Select attribute. The Variable element must then be empty.

Note that variables are not actually 'variable', as they are in programming and scripting languages. The value of a particular variable cannot be changed. However, a variable can be temporarily overridden by another variable with the same name, defined within a sub-structure of the stylesheet. For example, 'global' variables defined within the Stylesheet element can be replaced within a particular Template element. Similarly, the same variable can have different values within each iteration of a For Each element loop (see below).

A variable is used by inserting a variable reference into an attribute. A variable reference is identified by a leading '$' symbol.

In order to insert the value into output text, the variable can be placed in the Select attribute of the Value Of element:

<html:H1>The colour is
         <xsl:value-of select="$Colour"/>.</html:H1>


   The colour is red.

A variable can also be referenced in output elements. In the following example, a variable is used to specify the border width. However, note that in this case it is necessary to enclose the variable in curly brackets, '{' and '}'. Without the brackets, the variable would not be recognized:

<variable name="Border">3pt</variable>
...
...<fo:block border-width="{$Border}">...

The brackets shown above actually enclose any string expression. In order to use curly brackets as normal in an attribute value, they must be escaped by repeating the character. The sequence '{{' represents '{', and '}}' represents '}'. As shown above, string expressions may include variable references, but in addition they may contain literal text (enclosed by quotes), and XPath expressions.

Named templates

When the same formatting is required in a number of places, it is possible to simply reuse the same template. Instead of a Match attribute, a Name attribute is used to give the template a unique identifier. Elsewhere, within other templates, the Call Template element is used to activate the named template, also using a Name attribute to identify the template required:

<template name="CreateHeader">
  <html:hr />
  <html:h2>***** <apply-templates/> *****</html:h2>
  <html:hr />
</template>

<template match="title">
  <call-template name="CreateHeader" />
</template>

<template match="head">
  <call-template name="CreateHeader" />
</template>

However, in such a simple case it is probably easier simply to include all the element names in the Match attribute of a single template.

Template parameters

The named template mechanism is more useful when the action performed by the named template can be modified, by passing parameters to it that override default values. To assist with this, a special type of variable can be used. The Parameter element defines a variable, using the Name attribute to give it an identity, but differs from other variables in that the element content is only the default value:

<param name="Prefix">%%%</param>

The default value can be overridden by a parameter value. Parameters can be passed to a named template by use of the With Parameter element, which names the parameter in its Name attribute, and gives the parameter an override value in its content (or its Select attribute expression):

<with-param name="SecurityLvl">3</with-param>

The With Parameter element is placed within the Call Template element in order to pass the parameter name and value to the template that is being called:

<call-template name="CreateHeader">
  <with-param name="Prefix">%%%</with-param>
  <with-param name="SecurityLvl">3</with-param>
</call-template>

The following example shows that the named template holds the default values for these parameters, how these parameters are used, and what is presented if the parameters shown above are passed to the template:

<template name="CreateHeader">
  <param name="Prefix">*****</param>
  <param name="SecurityLvl">0</param>
  <html:HR />
  <html:H2>
    <value-of select="$Prefix" />
    Security = <value-of select="$SecurityLvl" />-
    <apply-templates/>
    *****</html:H2>
  <html:HR />
</template>


%%%Security = 3-The Header To Present *****

Parameters can be passed, in exactly the same manner, with the Apply Templates element in order to send values to the templates that are activated by the presence of child (or other) elements. Parameters can also be passed to the stylesheet from the XSLT processor (obviously, the With Param element has no role in this scenario):

<stylesheet ...>
  ...
  <param name="ExternalParam1">DEFAULT1</param>
						<param name="ExternalParam2">DEFAULT2</param>
  ...
  <template match="...">
    <apply-templates>
      <with-param name="PASS_DOWN1">X</with-param>
      <with-param name="PASS_DOWN2">Y</with-param>
    </apply-templates>
  </template>
  ...
  <template match="...">
    <param name="PASS_DOWN1">DEFAULT1</param>
    <param name="PASS_DOWN2">DEFAULT2</param>
    ...
  </template>
  ...
</stylesheet>

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

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