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 the xalan:ponent element? – JLRishe Commented Sep 18, 2013 at 14:37
Add a ment  | 

1 Answer 1

Reset to default 6
  1. 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.)
  2. See also this related SO question. (Useful, but you've probably already seen.)
  3. As @JLRishe mentioned, add functions="response" to xalan:ponent. (Proper, but seems not to be strictly necessary, at least in this case.)
  4. Move xalan:ponent out of xsl: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