Search code examples
xmlxsltidentityapply-templates

Calling xslt templates on elements generated inside the xslt


So I'm using the identity design pattern for XSLT:

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()[not(@visible='false')]"/>
  </xsl:copy>
</xsl:template>

And I do have many templates matching different nodes. Now what I want to do is generate some code inside one xsl:template and let another xsl:template match the newly generated code. Anyone who have any idea how to do this?


Example of what I want to do:

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

<xsl:template match="stuff">
    <!-- do some stuff -->
    <!-- get this following line parsed by the template over! -->
    <button url="something" name="a button" />
</xsl:template>

Solution

  • You can't do quite what you want to do in the way you are trying, however if the intention is to re-use code and avoid duplicate templates, it is perfectly acceptable for a matching template to be called as a named template, with parameters too.

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

    So, here if it is matching a button element, it will use the url and name attributes as the default values, but if you call it as a named-template, you can pass in your own parameters

    <xsl:template match="stuff"> 
       <!-- do some stuff --> 
       <!-- get this following line parsed by the template over! --> 
       <xsl:call-template name="button">
        <xsl:with-param name="url" select="'something'" /> 
        <xsl:with-param name="name" select="'A button'" /> 
       </xsl:call-template> 
    </xsl:template>