RE: [xsl] How can one get the name of a template or function?

Subject: RE: [xsl] How can one get the name of a template or function?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 11 Feb 2009 16:57:23 -0000
If you're prepared to grovel around the depths of Saxon internals, you can
write an extension function 


public static String getTemplateName(net.sf.saxon.expr XPathContext context)
{
    try {
       return context.getOrigin().getObjectName().getDisplayName();
    } catch (Exception err) {
       return null;
    }
}

and call it without any arguments as xx:getTemplateName(). Not tested. It
will only give the template name if it was invoked using call-template.
Should probably work for function calls as well.

Michael Kay
http://www.saxonica.com/

> -----Original Message-----
> From: Houghton,Andrew [mailto:houghtoa@xxxxxxxx] 
> Sent: 11 February 2009 16:21
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] How can one get the name of a template or function?
> 
> I'm using Saxon as an XSL transform processor.  So I don't 
> mind whether the answer is specific to Saxon or is general to 
> all XSL transform processors.  What I would like to do is to 
> get the name of the current template or function that is 
> currently executing.  I realize that templates don't need to 
> be named, but in my case they will be named since I'm using 
> xsl:call-template on them.  So for example I would like to do 
> something like:
> 
> <xsl:template name="foo" as="item()*">
>   <xsl:variable name="my-name" as="xsd:string" 
> select="get-name()" /> </xsl:template>
> 
> <xsl:function name="bar" as="item()*">
>   <xsl:variable name="my-name" as="xsd:string" 
> select="get-name()" /> </xsl:function>
> 
> Where get-name() is some sort of function that will give me 
> the name of the template or function.  I don't really care if 
> it's a function or a template that I have to call to get this 
> information.  When looking over the list of XPath function I 
> don't see anything useful and I didn't see anything in 
> Saxon's extensions.  So my question is can this be done somehow?
> 
> BTW, I realize that I could do:
> 
> <xsl:template name="foo" as="item()*">
>   <xsl:variable name="my-name" as="xsd:string" 
> select="string('foo')" /> </xsl:template>
> 
> <xsl:function name="bar" as="item()*">
>   <xsl:variable name="my-name" as="xsd:string" 
> select="string('bar')" /> </xsl:function>
> 
> but I would prefer not to do that since when the name 
> attribute changes, I need to keep the variable in sync too.
> 
> 
> Thanks, Andy.

Current Thread