7
votes

I'm looking how to create my own XPath function in XSLT-1.0. For example I have simple XPath expression which I'm using again and again in my XSLT template. I want to create my own XPath function myOwnFunction($var) which calls XPath expression.

Example expression:

normalize-space(substring-after(substring-after($var, '-'), '-'))
3
Good question, +1. See my answer for explanation, particular example and a recommendation. :) - Dimitre Novatchev
Thanks all for answers. XSLT 2.0 is nice to have for me, but I'm struct on 1.0 version in project I'm wokring on. - michal.kreuzman
@Michal-Kreuzman: The last link in my answer was not showing -- now corrected. - Dimitre Novatchev

3 Answers

11
votes

The previous two answers said it all: XSLT 1.0 does not provide the means to create functions that can be referenced from within an XPath expression.

If someone wants such functionality they should start using XSLT 2.0 (and make use of the standard <xsl:function> instruction), or:

  • Use the <func:function> extension element provided by EXSLT. Note that very few XSLT 1.0 processors implement this extension element.

  • Use a particular XSLT processor feature, if such exists. For the .NET platform one can use the XsltContext class, the IXsltContextFunction interface and techniques like this.

Anyway, all this is not at all XSLT programming, so my advice is to start using XSLT 2.0 seriously.

5
votes

If you are stuck with 1.0, you can check if your processor supports EXSLT Functions.

4
votes

XSLT 1.0 does not define this functionality. It was added in XSLT 2.0. You'll either have to use 2.0 or use some implementation-specific means to do this.