Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT call-template with dynamic QName?

Tags:

I have searched all around to find a solution to my problem, but i just got more questions...

consider the following XML:

<dynamicStuff>
      <dyn id="name1">...</dyn>
      <dyn id="name2">...</dyn>
      <dyn id="name3">...</dyn>
      <dyn id="name4">...</dyn> 
</dynamicStuff>

and suppose the I have an XSLT file as follows:

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

     <xsl:template name="name1">    
         ...
     </xsl:template>

     <xsl:template name="name2">    
         ...
     </xsl:template>

     <xsl:template name="name3">    
         ...
     </xsl:template>

     <xsl:template name="name4">    
         ...
     </xsl:template>

</xsl:stylesheet>

What I want to do is from a SECOND XSLT file dynamically determine which template to call with something like this:

<xsl:variable name="templateName">
     <xsl:value-of select="dyn/@id"/>
</xsl:variable>

<xsl:call-template name="$templateName"/>

sadly its not working, believe me when I say that I have tried a lot of different stuff, though it sounds so simple it does not work either...

Am I missing something?

Edit:

I have successfully done the following:

<xsl:template name="staticName">
    <xsl:param name="id" />

    <xsl:if test="$id = 'name1'">....</xsl:if>
    <xsl:if test="$id = 'name2'">....</xsl:if>
    ...
</xsl:template>

Calling in this way:

<xsl:call-template name="staticName">
     <xsl:with-param name="id" select="@id"/>
</xsl:call-template>

Needles to say how inconvenient this is... first of all my code will be bound to that staticName (imagine I need to do this call in a dozen files)... second I will have a bunch of (un)related content inside the same template when it could be more separated... a nightmare to upgrade the system u.u

It does what I want but not in the way I need...

Thanks in advance for any light on this matter!