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
xsl:functionway to implement functions than to rely on Javascript.xalan:component/xalan:scriptextensions: xml.apache.org/xalan-j/extensions_xsltc.html#java_ext. You should be able to call into Java, however.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.