Re: [xsl] passing template names

Subject: Re: [xsl] passing template names
From: George Cristian Bina <george@xxxxxxxxxxxxx>
Date: Fri, 07 Jul 2006 15:36:03 +0300
Hi again,

It was an error in the sample I sent earlier:
document('')/*/xsl:template[@x:*[local-name()=$target]]
should be
document('')/*/xsl:template/@x:*[local-name()=$target]

Here it is a better example:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:template name="template1">!!!!In template1!!!!</xsl:template>

  <xsl:template match="/">
    <xsl:call-template name="caller">
      <xsl:with-param name="target">template1</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="template1"/>
  </xsl:template>

  <xsl:template match="@x:template1" x:template1=""
    xmlns:x="http://example.com/myTemplates";>
    <xsl:param name="contextNode" select="."/>
    <xsl:for-each select="$contextNode[1]">
      <xsl:call-template name="template1"/>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="caller" xmlns:x="http://example.com/myTemplates";>
    <xsl:param name="target"/>
    <xsl:variable name="context" select="."/>
    <xsl:for-each
      select="document('')/*/xsl:template/@x:*[local-name()=$target]">
      <xsl:apply-templates select="self::node()">
        <xsl:with-param name="contextNode" select="$context"/>
      </xsl:apply-templates>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

Basically you need to add the caller template and for each template that you want to call dynamically add proxy template like <xsl:template match="@x:template1" x:template1=""...>

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com


George Cristian Bina wrote:
Hi Karl,

I think you can use something like below:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:x="http://example.com/myTemplates";>


  <xsl:template match="@x:template1" x:template1="">
    <xsl:param name="contextNode"/>
    <xsl:for-each select="$contextNode">
      In template1!
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="@x:template2" x:template2="">
    <xsl:param name="contextNode"/>
    <xsl:for-each select="$contextNode">
      In template1!
    </xsl:for-each>
  </xsl:template>

<xsl:template match="/*">
<xsl:variable name="target" select="'template1'"/>
<xsl:apply-templates select="document('')/*/xsl:template[@x:*[local-name()=$target]]">
<xsl:with-param name="contextNode" select="."/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>


I believe FXSL also has something similar, you might check that as well.

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com


Karl wrote:
I am in a situation where in a particular section need to call
different template dynamically based on which template invokes it.
Though I can use <xsl:choose> or <xsl:if> to do that, i wish i pass
the template name as a param value.

 e.g.
<xsl:call-template name="A">
 <xsl:with-param name="TemplateName">xx_template</xsl:with-param>
</xsl:apply-templates>
...

<xsl:call-template name="A">
 <xsl:with-param name="TemplateName">yy_template</xsl:with-param>
</xsl:apply-templates>

and then

<xsl:template name="A">
 <xsl:param name="TemplateName"/>
 ....
 <xsl:call-template name="{$TemplateName}"/>
 ..
</xsl:template>

I doesnt work. Is it valid? If not, are there any better method to
achieve similar result. Currently, iam  passing numbers in param and
use <xsl:choose> to call different templates .
Thanks
karl


___________________________________________________________ All New Yahoo! Mail  Tired of Vi@gr@! come-ons? Let our SpamGuard protect you. http://uk.docs.yahoo.com/nowyoucan.html

Current Thread