简体   繁体   English

如何在XSLT中包含对JavaScript的调用?

[英]How to include a call to JavaScript within XSLT?

I am trying to call JavaScript inside XSLT but it keeps on failing. 我正在尝试在XSLT中调用JavaScript,但是它一直失败。 I am using the Xalan namespace. 我正在使用Xalan命名空间。 I'm calling Java as well and that works no problem, but for some reason the JavaScript doesn't. 我也正在调用Java,但这没问题,但是由于某种原因JavaScript却没有。

<?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: 我在XML转换器中收到此错误:

<Location of error unknown>java.lang.NoSuchMethodException: For extension function, could not find method java.lang.String.response<ExpressionContext, ]>.
  1. Follow Apache's Xalan-Java JavaScript extension instructions , especially being careful to include js.jar and bsf.jar on your classpath. 请遵循Apache的Xalan-Java JavaScript扩展说明 ,尤其要注意在类路径中包含js.jarbsf.jar ( Important, but probably not your problem or you would have seen helpful stacktraces. ) 重要,但可能不是您的问题,否则您会看到有用的堆栈跟踪。
  2. See also this related SO question . 另请参阅此相关的SO问题 ( Useful, but you've probably already seen. ) 有用,但是您可能已经看过。
  3. As @JLRishe mentioned, add functions="response" to xalan:component . 如@JLRishe所述,将functions="response"添加到xalan:component ( Proper, but seems not to be strictly necessary, at least in this case. ) 正确,但至少在这种情况下似乎并非绝对必要。
  4. Move xalan:component out of xsl:template . xalan:component移出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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM