1

I have my custom Java function which performs something and I need to put it to XSL so it could perform something on selected nodes. I was using like this:

<msxsl:script implements-prefix="user" language="java">
   <![CDATA[
public String doSomething(String input) { 

    // does something
    return result;
}
]]></msxsl:script>

declaring namespaces:

 ... xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:user="example.com">

and call:

<xsl:variable name="var">
    <xsl:value-of select="user:doSomething(node)"/>
</xsl:variable>

this resulted in "Could not compile stylesheet" error.

Also tried to replace java code with Javascript, error was same.

Do you know how to do this right or other methods how I can insert Java code into XSL and call it?

alalambda
  • 323
  • 3
  • 15
  • The URI `urn:schemas-microsoft-com:xslt` as well as the `ms` prefix in `msxsl` means that this is a non-standard **Microsoft extension** to *their* XSLT processor. Being non-standard means that you cannot use it with other XSLT processors, like the one that comes with Java. – Andreas Mar 14 '17 at 08:22
  • @Andreas, thanks for your comment! Maybe you know Java alternatives? – alalambda Mar 14 '17 at 08:24
  • 1
    Xalan does have similar features: https://xml.apache.org/xalan-j/extensions.html – Andreas Mar 14 '17 at 08:26
  • In the Java world, I would consider moving to XSLT 2.0 and Saxon 9 (available in the open source HE edition from Maven or Sourceforge) as then you probably do not even need any extension functions but can implement their functionality in standard XSLT and XPath 2.0 using `xsl:function`. – Martin Honnen Mar 14 '17 at 09:11

1 Answers1

1

The mechanisms for calling out from XSLT to other languages such as Java or Javascript depend on the XSLT processor you are using, and in general they are not portable across processors. The msxsl:script mechanism is specifically for Microsoft's MSXML.

The only two Java processors in common use nowadays are Xalan and Saxon, and both have mechanisms for calling out to Java code. The mechanisms are similar but differ in many details. But as Martin Honnen points out, extension functions are less likely to be needed with XSLT 2.0 (or 3.0) than with 1.0.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Thanks for your answer! Unfortunately, I only can use 1.0. Using Apache XSL-FO with Xalan. – alalambda Mar 14 '17 at 11:16
  • Don't feel too bad about things just because you are constrained to use 18-year old technology. Some people still have to use COBOL. – Michael Kay Mar 14 '17 at 16:15