0

Iam currently using XSL to transform news-sites into a special XML-Format.

Some things that involve a lot of logic and parsing however are increasingly getting more tedious to do.

Is it possible to write javascript functions directly into an XSL file and then use said functions inside my transformations? Because that would make my life so much easier.

Thanks a lot for your help

  • 1
    Here is an answer [here](https://stackoverflow.com/a/7444912/8412711) using microsoft xslt processor. – gimperman Aug 03 '17 at 15:57
  • Are you running server-side or client-side? The answer is basically: (a) some XSLT processors support this, others don't, and (b) if you use an XSLT 2.0 processor, most of the need for it disappears because it's just as easy to write most things in XSLT native functions. – Michael Kay Aug 03 '17 at 17:40

1 Answers1

2

While some processors support writing extension functions in other languages like JScript or Javascript you then often only have access to the core language (i.e. ECMAScript 3) and no host or DOM support like you might be used to when using Javascript in the browser. So whether there is support for that at all depends on your chosen XSLT processor, whether that makes life so much easier is even less likely. These days you might want to check whether you can't move to XSLT 3.0 with support for user-defined functions and the rich type system/data model and function library provided. Saxon 9.8 (available in the open-source HE on Maven and Sourceforge) and Altova XML Spy and Raptor already support XSLT 3.0. Even XSLT 2.0 existing since 2007 allows user-defined functions written in XSLT and has regular expression support (the only thing that ECMAScript might give you as a benefit in XSLT 1.0) with the functions matches, tokenize and the element xsl:analyze-string.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110