admin管理员组文章数量:1333431
I am trying to call JavaScript inside XSLT but it keeps on failing. I am using the Xalan namespace. I'm calling Java as well and that works no problem, but for some reason the JavaScript doesn't.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="" xmlns:java="" xmlns:xalan="" xmlns:counter="MyCounter" extension-element-prefixes="counter">
<xsl:template match="/">
<xalan:ponent prefix="counter" functions="response">
<xalan:script lang="javascript">
function response(name) {
// Return a string.
return "" + (name);
}
</xalan:script>
</xalan:ponent>
<xsl:value-of select="counter:response('hello')"/>
<xsl:variable name="rightNow" select="java:java.util.Date.new()"/><!-- Get date object -->
<xsl:variable name="formatter" select="java:java.text.SimpleDateFormat.new('MM')"/> <!-- double digit format: append 0 to less than ten -->
<xsl:variable name="formattedMonth" select="java:format($formatter, $rightNow)"/> <!-- format it -->
<p><xsl:value-of select="$formattedMonth"/></p>
</xsl:template>
</xsl:stylesheet>
I get this error in the XML transformer:
<Location of error unknown>java.lang.NoSuchMethodException: For extension function, could not find method java.lang.String.response<ExpressionContext, ]>.
I am trying to call JavaScript inside XSLT but it keeps on failing. I am using the Xalan namespace. I'm calling Java as well and that works no problem, but for some reason the JavaScript doesn't.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3/1999/XSL/Transform" xmlns:java="http://xml.apache/xalan/java" xmlns:xalan="http://xml.apache/xalan" xmlns:counter="MyCounter" extension-element-prefixes="counter">
<xsl:template match="/">
<xalan:ponent prefix="counter" functions="response">
<xalan:script lang="javascript">
function response(name) {
// Return a string.
return "" + (name);
}
</xalan:script>
</xalan:ponent>
<xsl:value-of select="counter:response('hello')"/>
<xsl:variable name="rightNow" select="java:java.util.Date.new()"/><!-- Get date object -->
<xsl:variable name="formatter" select="java:java.text.SimpleDateFormat.new('MM')"/> <!-- double digit format: append 0 to less than ten -->
<xsl:variable name="formattedMonth" select="java:format($formatter, $rightNow)"/> <!-- format it -->
<p><xsl:value-of select="$formattedMonth"/></p>
</xsl:template>
</xsl:stylesheet>
I get this error in the XML transformer:
<Location of error unknown>java.lang.NoSuchMethodException: For extension function, could not find method java.lang.String.response<ExpressionContext, ]>.
Share
Improve this question
edited Dec 29, 2016 at 18:42
TylerH
21.1k78 gold badges79 silver badges112 bronze badges
asked Sep 18, 2013 at 11:11
Johnathan AuJohnathan Au
5,37218 gold badges74 silver badges129 bronze badges
2
-
I think script tag is enough, it probably doesn't require
xalan
prefix<script type="text/javascript">
– Ashok Commented Sep 18, 2013 at 11:18 -
Shouldn't you have
functions="response"
in thexalan:ponent
element? – JLRishe Commented Sep 18, 2013 at 14:37
1 Answer
Reset to default 6- Follow Apache's Xalan-Java JavaScript extension instructions, especially being careful to include js.jar and bsf.jar on your classpath. (Important, but probably not your problem or you would have seen helpful stacktraces.)
- See also this related SO question. (Useful, but you've probably already seen.)
- As @JLRishe mentioned, add
functions="response"
toxalan:ponent
. (Proper, but seems not to be strictly necessary, at least in this case.) - Move
xalan:ponent
out ofxsl:template
. (Critical. This is likely the problem here.)
So, running your code thus modified:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3/1999/XSL/Transform"
xmlns:java="http://xml.apache/xalan/java"
xmlns:xalan="http://xml.apache/xalan"
xmlns:counter="MyCounter"
extension-element-prefixes="counter">
<xalan:ponent prefix="counter" functions="response">
<xalan:script lang="javascript">
function response(name) {
// Return a string.
return "" + (name);
}
</xalan:script>
</xalan:ponent>
<xsl:template match="/">
<xsl:value-of select="counter:response('hello')"/>
<xsl:variable name="rightNow" select="java:java.util.Date.new()"/><!-- Get date object -->
<xsl:variable name="formatter" select="java:java.text.SimpleDateFormat.new('MM')"/> <!-- double digit format: append 0 to less than ten -->
<xsl:variable name="formattedMonth" select="java:format($formatter, $rightNow)"/> <!-- format it -->
<p><xsl:value-of select="$formattedMonth"/></p>
</xsl:template>
</xsl:stylesheet>
Yields the following output as expected:
<?xml version="1.0" encoding="UTF-8"?>hello<p xmlns:xalan="http://xml.apache/xalan" xmlns:java="http://xml.apache/xalan/java">09</p>
本文标签: How to include a call to JavaScript within XSLTStack Overflow
版权声明:本文标题:How to include a call to JavaScript within XSLT? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742304312a2449607.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论