0

I’m trying to call javascript function in my XSL file.

My XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" version="1.0" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:jscript="http://www.url.com" exclude-result-prefixes="msxsl jscript">
.
.
.
<html>
    <head>
    .
    .
    .
        <msxsl:script language="JScript" implements-prefix="jscript">
            <![CDATA[
            function testFnc(){
                return "test";
                }
            }]]>
        </msxsl:script>
    </head>
    .
    .
    .
</html>
.
.
.
<xsl:value-of select="jscript:testFnc()"/>

In Java file, when I try to create Transformer:

Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xslUrl));

I get error:

ERROR: 'Cannot find class 'www.url.com'.'>

FATAL ERROR: 'Cannot find external method 'www.url.com.testFnc' (must be public).'

javax.xml.transform.TransformerConfigurationException: Cannot find external method 'www.url.com.testFnc' (must be public).

Without this code

<xsl:value-of select="jscript:testFnc()"/>

everything worked fine.

In Java 7 was similar problem with build-in Xalan: Xalan Java extensions 'Cannot find class' error on JRE 7

I was wondering is this problem is stil present in Java 8.

I also tried something like this: http://xml.apache.org/xalan-j/extensions.html#basic-pattern How to include a call to JavaScript within XSLT?

but results was the same.

Thanks in advance

5
  • The example you have shown relies on a proprietary extension to XSLT 1 introduced by Microsoft in its XSLT processors and I think also supported by some others (Altova, XmlPrime) to be compatible with Microsoft. As far as I know Xalan never supported that. As for the link, perhaps ask a second question with all the details on that. But in general these days in the Java world I would rather consider XSLT 3 with Saxon 9 and all the built-in XPath 3.1 function in addition to the xsl:function way to implement functions than to rely on Javascript. Commented Nov 8, 2019 at 13:51
  • As for the example in stackoverflow.com/a/18875807/252228, I think that will work with the interpretor version of Xalan from Apache, I think the built-in XSLT processor in Java 8 is XSLTC, the compiler version of Xalan, and that doesn't support the xalan:component/xalan:script extensions: xml.apache.org/xalan-j/extensions_xsltc.html#java_ext. You should be able to call into Java, however. Commented Nov 8, 2019 at 14:00
  • Thanks for your replay Martin. I will consider, if I can switch to XSLT 3. But if I'm forced to stay on XSLT 1.0, is it possible to use javascript (not necessarily with Xalan processor )? Commented Nov 8, 2019 at 14:04
  • Well, yes, where supported, but you need to understand that it usually only means you have access to the core object and functions the used Javascript engine provides, like Date, RegExp. You don't have any access to APIs like window or document or anything you use with Javascript in the browser. So for XSLT 1 with XPath 1, where you have no date/dateTime data type and no support for regular expression based string functions, pure J(ava)Script offers some additional features. In XSLT/XPath 2 and later it usually doesn't. Commented Nov 8, 2019 at 14:10
  • Thanks for your help Martin. I was able to call Java function in xslt. I posted answer. Commented Nov 14, 2019 at 7:39

1 Answer 1

0

Thanks to Martin Honnen help I was able to use my own Java class in xslt code. You can find more information in documentation: http://xml.apache.org/xalan-j/extensions_xsltc.html#java_ext

xslt:

<xmlns:java_util="http://xml.apache.org/xalan/java/com.example.java.util" exclude-result-prefixes="java_util">
<!-- Java function call -->
<xsl:value-of select="java_util:DateUtil.formatDateTime(.)"/>

Java class:

package com.example.java.util;

public class DateUtil {
    public static String formatDateTime(String date){
    //implementation
}
}
Sign up to request clarification or add additional context in comments.

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.