Next / Previous / Contents / TCC Help System / NM Tech homepage

4.7. Attribute value templates

Often the value of an output attribute is not a constant but some function of variables in the stylesheet. In this case, use “{braces}” around an XPath expression to produce the desired result.

For example, here is a small template used to wrap some content in an XHTML span element.

<xsl:template name="shout">
  <xsl:param name="shout-class">shouting</xsl:param>
  <span class="{$shout-class}">
    <xsl:apply-templates select="$content"/>
  </span>
</xsl:template>

If the template above is called without parameters, the value of the class attribute will be the default value, “shouting”. If called with an xsl:with-param value, the value of the class attribute will be the value of that parameter. For example, this call:

<xsl:call-template name="shout">
  Danger Will Robinson!
</xsl:call-template>

will produce this result:

<span class="shouting">Danger Will Robinson!</span>

This call, however:

<xsl:call-template name="shout">
  <xsl:with-param name="shout-class" select="'screaming'"/>
  Danger Will Robinson!
</xsl:call-template>

produces:

<span class="screaming">Danger Will Robinson!</span>