How to include a call to JavaScript within XSLT?

旧时模样 提交于 2020-01-04 13:25:23

问题


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.org/1999/XSL/Transform" xmlns:java="http://xml.apache.org/xalan/java" xmlns:xalan="http://xml.apache.org/xalan" xmlns:counter="MyCounter" extension-element-prefixes="counter">
<xsl:template match="/">
    <xalan:component prefix="counter" functions="response">
        <xalan:script lang="javascript">

          function response(name) {
            // Return a string.
            return "" + (name);
          }

        </xalan:script>
     </xalan:component>

    <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, ]>.

回答1:


  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:component. (Proper, but seems not to be strictly necessary, at least in this case.)
  4. Move xalan:component 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.org/1999/XSL/Transform"
                xmlns:java="http://xml.apache.org/xalan/java"
                xmlns:xalan="http://xml.apache.org/xalan"
                xmlns:counter="MyCounter"
                extension-element-prefixes="counter">

  <xalan:component prefix="counter" functions="response">
    <xalan:script lang="javascript">

      function response(name) {
        // Return a string.
        return "" + (name);
      }

    </xalan:script>
  </xalan:component>

  <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.org/xalan" xmlns:java="http://xml.apache.org/xalan/java">09</p>


来源:https://stackoverflow.com/questions/18870682/how-to-include-a-call-to-javascript-within-xslt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!