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
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
}
}