7

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. :) Commented Mar 9, 2011 at 13:54
  • 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. Commented Mar 9, 2011 at 14:01
  • @Michal-Kreuzman: The last link in my answer was not showing -- now corrected. Commented Mar 9, 2011 at 14:22

3 Answers 3

11

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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

1 Comment

4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.