Variables and parameters

With the <xsl:variable> element varibles can be defined. When the element is used as a top-level element, its scope of application is global. But if the variable is within the template body, it is a local variable. Global variables are valid even if they are imported into other stylesheets. The desired value, which has to be a string, can be assigned to the variable with the help of the select attribute as well as with the content of the element. The value is called up using the $ character, which has to be put in front of the name of the variable.

This variable concept is very important for the practical work with XSLT because there is a large volume of information in the stylesheets, which can be centrally managed in that way. As an example the font sizes are to mention. In general, the significance of such variables is increasing with the extent of the stylesheet. The use of <xsl:param> basically corresponds to the use of <xsl:variable>. The attributes to be used are identical in both elements. The minor distinctions in the function of both elements will not be explained here.

The stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output encoding="iso-8859-1" version="1.0"/>
   <xsl:variable name="PoemAnnouncement"><b>Poem</b></xsl:variable>      (1)
   <xsl:template match="CollectionOfPoems">
      <html>
         <body>
            <xsl:apply-templates/>
         </body>
      </html>
   </xsl:template>
   <xsl:template match="Author">
      <br/>
      <h4>
         <xsl:apply-templates/>
      </h4>
   </xsl:template>
   <xsl:template match="FirstName">
      <xsl:apply-templates/>
      <xsl:text> </xsl:text>
   </xsl:template>
   <xsl:template match="LastName">
      <xsl:apply-templates/>
   </xsl:template>
   <xsl:template match="Title">
      <h2>
         <xsl:apply-templates/>
      </h2>
   </xsl:template>
   <xsl:template match="Strophe">
      <p>
         <xsl:apply-templates/>
      </p>
   </xsl:template>
   <xsl:template match="Verse">
      <xsl:apply-templates/>
      <br/>
   </xsl:template>
   <xsl:template match="Poem">
   <xsl:variable name="Year">
      <xsl:value-of select="@YearOfPublication"/>                  (2)
   </xsl:variable>
   <br/>
      <xsl:copy-of select="$PoemAnnouncement"/> from the year      (3)
      <xsl:value-of select="$Year"/>
         <xsl:apply-templates/>
   </xsl:template>
</xsl:stylesheet>

(1) The <xsl:variable> element contains the obligatory name attribute, which has the name of the variable as a value. This name can subsequently be called up as often as required. Since, in this example, the <xsl:variable> element is a top-level element, which means it is a child element of the <xsl:stylesheet> element, it is a global variable being valid in the entire stylesheet. The content of the variable – in this case the HTML structure – can be copied to any position by the call.

(2) The example shows a local variable which is only valid within the template in which it is defined. Since the variable contains the select attribute, the value of this attribute is used as a replacement text, which means the value of the select attribute determines the content of the variable. Since the content of the Year variable is plain text – here it is the content of the YearOfPublication attribute – and not structure, the call is done by means of the <xsl:value-of> element. Here too, the $ character has to be put in front of the variable name.

(3) The <xsl:copy-of> element makes it possible to copy deep structures into the result tree. In contrast the <xsl:copy> element is used in order to copy flat structures. Often these elements are important for XSLT stylesheets. It is absolutely necessary to put the $ character in front of the variable name because in this way the processor learns that it is a variable and not an element.

Browser view of the target document:

Browser view: example - variables

Exercise 7

Please use the variable concept to check the strings.

The entire poem shall be outputted with a red background if the first character of the YearOfPublication attribute is a '-'.

Remarks:

The starts-with() function checks if the character string of the first argument starts with the character string of the second argument.
Syntax: boolean starts-with (string, string).

The <xsl:attribute> element inserts an attribute into the target document. The name of the attribute is defined with the help of the name attribute. The value of the attribute is the content of <xsl:attribute>. Often it is used to generate attributes during the operating time. The attribute values are determined with <xsl:value-of> and pasted.
Attributes: name, namespace.

> > to the solution of exercise 7

   

<< back next >>

 


Copyright © dpunkt.verlag GmbH 2004
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "XSL-FO in der Praxis" ("XSL-FO in practice") is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.

dpunkt.verlag GmbH, Ringstraße 19B, 69115 Heidelberg, fon 06221-14830, fax 06221-148399, hallo@dpunkt.de