如何在 XSLT 中包含对 JavaScript 的调用? [英] How to include a call to JavaScript within XSLT?

查看:32
本文介绍了如何在 XSLT 中包含对 JavaScript 的调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 XSLT 中调用 JavaScript,但它一直失败.我正在使用 Xalan 命名空间.我也在调用 Java,这没问题,但由于某种原因 JavaScript 没有.

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> 

我在 XML 转换器中收到此错误:

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. 遵循 Apache 的 Xalan-Java JavaScript 扩展说明,尤其要小心在类路径中包含 js.jarbsf.jar.(很重要,但可能不是您的问题,否则您会看到有用的堆栈跟踪.)
  2. 另见这个相关的 SO 问题.(很有用,但您可能已经看过了.)
  3. 正如@JLRishe 提到的,将 functions="response" 添加到 xalan:component.(正确,但似乎不是绝对必要的,至少在这种情况下是这样.)
  4. xalan:component 移出 xsl:template.(严重.这可能是这里的问题.)
  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.)

因此,运行您修改后的代码:

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

按预期产生以下输出:

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

这篇关于如何在 XSLT 中包含对 JavaScript 的调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆