xsl-list
[Top] [All Lists]

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

2019-12-07 02:59:24
This can be simplified to:

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

You can also use XSLT syntax:

<xsl:map>
  <xsl:map-entry key="'move'" select="f:move#0"/>
  <xsl:map-entry key="'stop'" select="f:stop#0"/>
</xsl:map>
  

On 7 Dec 2019, at 01:45, Dimitre Novatchev dnovatchev(_at_)gmail(_dot_)com 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

I also like this XSLT/XPath 3 way:

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform 
<http://www.w3.org/1999/XSL/Transform>" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema 
<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(_at_)mitre(_dot_)org <mailto:costello(_at_)mitre(_dot_)org> 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com 
<mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>> 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



XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by 
email <>)
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>