Re: [xsl] How to call an XSLT function whose name isn't known till run-time?

Subject: Re: [xsl] How to call an XSLT function whose name isn't known till run-time?
From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 7 Dec 2019 01:44:55 -0000
I also like this XSLT/XPath 3 way:

<xsl:stylesheet version="3.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform";
  xmlns:xs="http://www.w3.org/2001/XMLSchema"; xmlns:f="my:f">
  <xsl:param name="pFunName" select="'move'" as="xs:string"/>

  <xsl:variable name="vFuns" as="map(xs:string, function(*))"
   select="map {
   'move' : function() as xs:string {f:move()},
   'stop' : function() as xs:string {f:stop()}
   }
   "
  />

  <xsl:function name="f:move" as="xs:string">
    Move
  </xsl:function>

  <xsl:function name="f:stop" as="xs:string">
    Stop
  </xsl:function>

  <xsl:template match="/">
    <xsl:value-of select="$vFuns('move')(), $vFuns('stop')()"/>
  </xsl:template>
</xsl:stylesheet>

In this line of the code:

     <xsl:value-of select="$vFuns('move')(), $vFuns('stop')()"/>

we can also pass string variables or expressions that produce string and
not just the literal name of the function.

When this simple example is run (against any XML document -- not used), we
get the expected result:

    Move

    Stop


Cheers,
Dimitre


On Fri, Dec 6, 2019 at 9:20 AM Costello, Roger L. costello@xxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> Hi Folks,
>
> I have an XSLT variable, $function-to-invoke, that holds the name of an
> XSLT function to be invoked. For example, if $function-to-invoke holds the
> string "move" then I  want my XSLT program to invoke the function with that
> name, i.e., invoke f:move(...). Is there a way for XSLT to call a function
> whose name isn't known till run-time?
>
> /Roger

Current Thread