<xsl:param> | |
Defines the name and value of a parameter to be used by a template. This element can appear as a top-level element or inside the <xsl:template> element. If the <xsl:param> appears as a top-level element, it is a global parameter, visible to all areas of the stylesheet. The value of the parameter can be defined in one of two ways: specified in the select attribute, or defined in an XSLT template inside the <xsl:param> element itself. | |
Category | |
Instruction |
|
Required Attributes | |
|
|
Optional Attributes | |
|
|
Content | |
If the select attribute is used, <xsl:param> should be empty. Otherwise, it contains an XSLT template. |
|
Appears in | |
<xsl:stylesheet> and <xsl:template>. If an <xsl:param> appears as a child of <xsl:stylesheet>, then it is a global parameter visible throughout the stylesheet. XSLT doesn't define the way global parameters are passed to the XSLT processor, so check the documentation for your processor to see how this is done. (See Section 4.4.3 in Chapter 4 for an overview of how to pass parameters to the most popular XSLT processors.) |
|
Defined in | |
XSLT section 11, Variables and Parameters. |
|
Example | |
Here is a stylesheet that defines several <xsl:param> elements, both global and local. Notice that one of the parameters is a node-set; parameters can be of any XPath or XSLT datatype: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:variable name="newline"> <xsl:text> </xsl:text> </xsl:variable> <xsl:param name="favoriteNumber" select="23"/> <xsl:param name="favoriteColor"/> <xsl:template match="/"> <xsl:value-of select="$newline"/> <xsl:value-of select="list/title"/> <xsl:value-of select="$newline"/> <xsl:variable name="listitems" select="list/listitem"/> <xsl:call-template name="processListitems"> <xsl:with-param name="items" select="$listitems"/> <xsl:with-param name="color" select="'yellow'"/> <xsl:with-param name="number" select="$favoriteNumber"/> </xsl:call-template> </xsl:template> <xsl:template name="processListitems"> <xsl:param name="items"/> <xsl:param name="color" select="'blue'"/> <xsl:for-each select="$items"> <xsl:value-of select="position()"/> <xsl:text>. </xsl:text> <xsl:value-of select="."/> <xsl:value-of select="$newline"/> </xsl:for-each> <xsl:value-of select="$newline"/> <xsl:text>Your favorite color is </xsl:text> <xsl:value-of select="$favoriteColor"/> <xsl:text>.</xsl:text> <xsl:value-of select="$newline"/> <xsl:text>The color passed to this template is </xsl:text> <xsl:value-of select="$color"/> <xsl:text>.</xsl:text> <xsl:value-of select="$newline"/> </xsl:template> </xsl:stylesheet> We'll use this stylesheet to transform the following document: <?xml version="1.0"?> <list> <title>A few of my favorite albums</title> <listitem>A Love Supreme</listitem> <listitem>Beat Crazy</listitem> <listitem>Here Come the Warm Jets</listitem> <listitem>Kind of Blue</listitem> <listitem>London Calling</listitem> <listitem>Remain in Light</listitem> <listitem>The Joshua Tree</listitem> <listitem>The Indestructible Beat of Soweto</listitem> </list> Here are the results: A few of my favorite albums 1. A Love Supreme 2. Beat Crazy 3. Here Come the Warm Jets 4. Kind of Blue 5. London Calling 6. Remain in Light 7. The Joshua Tree 8. The Indestructible Beat of Soweto Your favorite color is purple. The color passed to this template is yellow. To generate these results, we passed the value purple to the XSLT processor. With Xalan, the value is passed like this: java org.apache.xalan.xslt.Process -in test4.xml -xsl param.xsl -param favoriteColor purple (The command should be entered on a single line.) See Section 4.4.3 in Chapter 4 for a more complete discussion of global parameters and how they can be set for various XSLT processors. |