簡體   English   中英

如何在XSLT中包含對JavaScript的調用?

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

我正在嘗試在XSLT中調用JavaScript,但是它一直失敗。 我正在使用Xalan命名空間。 我也正在調用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> 

我在XML轉換器中收到此錯誤:

<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 嚴重。這可能是這里的問題。

因此,運行修改后的代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM