Passing Parameters
If we invoke a template by name, which is similar to
calling a subroutine, we’ll need to pass parameters to those
templates. We do this with the <xsl:with-param> element. For example,
let’s say we want to call a template named
draw-box, and then pass the parameters startX, startY, endX, and endY to it. Here’s what we’d do:
<xsl:call-template name="draw-box"> <xsl:with-param name="startX" select="50"/> <xsl:with-param name="startY" select="50"/> <xsl:with-param name="endX" select="97"/> <xsl:with-param name="endY" select="144"/> </xsl:call-template>
In this sample, we’ve called the template named draw-box with the four parameters we
mentioned earlier. Notice that up until now, <xsl:call-template> has always been an
empty tag; here, though, the parameters are the content of the
<xsl:call-template> element.
(If you want, you can do the same thing with <xsl:apply-templates>.)
If we’re going to pass parameters to a template, we have to set
up the template so that it expects the parameters we’re passing. To do
this, we’ll use the <xsl:param> element inside the
template. Here are some examples:
<xsl:template name="draw-box">
<xsl:param name="startX"/>
<xsl:param name="startY" select="'0'"/>
<xsl:param name="endX">
10
</xsl:param>
<xsl:param name="endY">
10
</xsl:param>
...
</xsl:template>A couple of notes about the <xsl:param> element:
If you define any
<xsl:param>elements in a template, they must be the first thing in the template.The
<xsl:param>element allows you to define ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access