tags:

views:

471

answers:

2

If I have a template as follows, which is used to create a button:

<xsl:template match="button" name="button">
  <a class="button" href="{@href}">
    <xsl:value-of select="@name"/>
  </a>
</xsl:template>

I want to be able to use that button in another template, like this:

<xsl:template match="createForm">
  ...
  <button name="Create" href="/create"/>
</xsl:template>

However, this will just output the button tag as-is. I would like it to be processed through the existing button template. How can this be achieved?

--

Thanks David M for your answer. Here is what I have now for the button template:

<xsl:template match="button" name="button">
  <xsl:param name="name" select="@name"/>
  <xsl:param name="href" select="@href"/>
  <a class="button" href="{$href}">
    <xsl:value-of select="$name"/>
  </a>
</xsl:template>

The createForm template now looks like this:

<xsl:template match="createForm">
  ...
  <xsl:call-template name="button">
    <xsl:with-param name="name" select="'Create'"/>
  </xsl:call-template>
</xsl:template>
+4  A: 

Try using this (off the top of my head):

<xsl:call-template name="button">
    <xsl:with-param name="name" value="Create" />
    <xsl:with-param name="href" value="/create" />
</xsl:call-template>

You'll also need to declare your two parameters within your button template using <xsl:param ...>.

David M
note that the default value is related to the context node - those aren't string literals. In the given example, I would use value="@name" and value="@href" respectively.
Marc Gravell
@Marc - but the "context node" you refer to was within the XSLT, not the file being processed ... this was just the wrong approach to calling a template wasn't it?
David M
yah .. we can use params .. its a correct answer..
infant programmer
+1  A: 

As long as you use <xsl:include ... /> or <xsl:import ... />, you should be able to use either of:

<xsl:apply-templates select="button"/> <!-- or your own selector -->

(which assumes there are button elements under the context node)

or <xsl:call-template/> using the name

Marc Gravell