Global Parameters

XSLT allows you to define parameters whose scope is the entire stylesheet. You can define default values for these parameters, and you can pass values to those parameters externally to the stylesheet. Before we talk about how to pass in values for global parameters, we’ll show you how to create them. Any parameters that are top-level elements (any <xsl:param> elements whose parent is <xsl:stylesheet>) are global parameters. Here’s an example:

<?xml version="1.0"?>
<!-- params.xsl -->
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <xsl:param name="startX"/>
  <xsl:param name="baseColor"/>

  <xsl:template match="/">
    <xsl:text>&#xA;Global parameters example&#xA;&#xA;</xsl:text>

    <xsl:text>The value of startX is: </xsl:text>
    <xsl:value-of select="$startX"/>
    <xsl:text>&#xA;The value of baseColor is: </xsl:text>
    <xsl:value-of select="$baseColor"/>
    <xsl:text>&#xA;</xsl:text>
  </xsl:template>

</xsl:stylesheet>

How you pass values for global parameters depends on the XSLT processor you’re using. We’ll go through some examples here for all the usual suspects. Let’s say we want to pass the numeric value 50 as the value for startX, and the string value magenta as the default value for baseColor. The following list describes the commands you’d use to do that:

Xalan

To pass global parameters to Xalan, you can define them on the Xalan command line:

java org.apache.xalan.xslt.Process -in blank.xml -xsl params.xsl -param startX ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.