Passing Data between Template Rules Using Local Parameters

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

Passing Data between Template Rules Using Local Parameters

To pass a parameter from one template rule to another, use the <xsl:with-param> element as a child of an <xsl:apply-templates> or <xsl:call-template> element. For example:

<xsl:call-template name="some_named_template">
    <xsl:with-param name="param1" select="@some_attrib"/>
</xsl:call-template>

When the XSLT processor encounters the <xsl:call-template> element in a template rule, the template named some_named_template is instantiated. If this template uses the parameter $param1, a new value (the some_attrib attribute value of the current context node) is passed to that named template.

In the previous example of using XSLT variables, the definition of a variable had to be placed inside the template rule that used it. Because the scope of the variable is limited, its value is inaccessible to other template rules, which might make use of it. To make the value accessible, you can use a named template. The value of the place attribute you want to translate can then be passed into this template.

This approach differs from the variable-based approach in Using Variables as Convenience Constants, in the following ways.

  • The <xsl:choose> block is now enclosed in a named template rule. The name of this rule is placename.
  • There is an <xsl:param> element. This element assigns a string value, "[Unknown Locale]", to the $location parameter as a default. This default is used if a call to this template fails to provide a value.
  • Instead of referencing the @place attribute, you can reference the $location parameter. If the call to the placename template rule supplies a value for the $location parameter (by means of an <xsl:with-param> element), the <xsl:choose> block uses that value.
  • The call to the $placename variable becomes a call to a named template, using the <xsl:with-param> element to pass a parameter.
Note   The value of the place attribute is assigned to the $location variable in the <xsl:with-param> element. If that attribute is empty, or has any value other than "location1", "location2", or "location3", the value returned from the named template is "[Unknown Locale]".

Example

This example demonstrates passing data between template rules using local parameters.

XML File

Use weatherlog.xml, in Initial XSLT Example Using Variables. Make sure that the href attribute references the weatherlogv1.xsl style sheet.

XSLT File

The following file is an adaptation of weatherlogv1.xsl file in Initial XSLT Example Using Variables.

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/TR/REC-html40">

<xsl:template match="/">
   <HTML>
      <HEAD>
         <TITLE>Weather Readings</TITLE>
      </HEAD>
      <BODY>
         <xsl:apply-templates/>
      </BODY>
   </HTML>
</xsl:template>

<!-- Override built-in template rule for text nodes. -->    
<xsl:template match="text()"/>

<xsl:template name="placename">
    <xsl:param name="location" select="'[Unknown Locale]'"/>
    <xsl:choose>
        <xsl:when test="$location='location1'">Midtown</xsl:when>
        <xsl:when test="$location='location2'">Northeast</xsl:when>
        <xsl:when test="$location='location3'">Airport</xsl:when>
        <xsl:otherwise>[Unknown Locale]</xsl:otherwise>
    </xsl:choose>
</xsl:template>    

<xsl:template match="weather">
   <H1>Weather Readings</H1>
   <xsl:for-each select="day">
        <xsl:variable name="average_temp" 
            select="format-number(sum(locale/temp/high) div count(locale), '##0.00')"/>
      <H2>As of <xsl:value-of select="@date"/></H2>
      <xsl:for-each select="locale">
         <H3>
            <xsl:call-template name="placename">
               <xsl:with-param name="location" select="@place"/>
            </xsl:call-template>
         </H3>
      </xsl:for-each>
   </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Formatted Output

See Also

Finished XSLT Example Using Variables