传递值<xsl:value-of>转换为java脚本函数

Passing a value of <xsl:value-of> to a java script function

本文关键字:转换 函数 java gt 脚本 lt xsl value-of      更新时间:2023-09-26

我想做这份工作:

我有一个xml文件,我想用xslt将其转换为HTML文件。它看起来像这样:

<article id="3526">
    <name>Orange</name>
    <preis stueckpreis="true">15.97</preis>
    <lieferant>Fa.k</lieferant>
  </article>

我想在HTML文件上显示lieferant。如果用户点击该名称,则应出现警报并穿鞋preis

我不知道,如何将preis的值传递到java脚本代码中。我试着写一个非常简单的代码,只显示带有javascript警报的lieferant,但我做不到。你能帮我解决这个问题吗:

 <msxsl:script language="JScript" implements-prefix="user">
          function simfunc(msg)
          {
            alert(msg);
          }
        </xsl:script>
        </head>
      <xsl:for-each select="//artikel">
        <div>
          <p id='p1'  >
            <xsl:value-of select="user:simfunc(lieferant)"/>
          </p>     
        </div>
        <br/>
      </xsl:for-each>

您必须明白,虽然一些XSLT处理器(如Microsoft的MSXML)支持在XSLT内部使用JScript中实现的扩展函数,但您只需访问JScript引擎实现的对象和方法即可。alert不是JScript函数,它是在浏览器内部向JScript公开的函数。

因此,在XSLT的JScript扩展函数中没有可用的alert,您可以在http://msdn.microsoft.com/en-us/library/hbxc2t98%28v=vs.84%29.aspx.

举个例子,http://home.arcor.de/martin.honnen/xslt/test2013072701.xml是引用XSLT1.0样式表的XML文档,Mozilla使用某些EXSLT扩展函数,IE使用在JScript中实现的扩展函数。

样式表http://home.arcor.de/martin.honnen/xslt/test2013072701.xsl如下所示:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:user="http://example.com/user"
  xmlns:ms="urn:schemas-microsoft-com:xslt"
  xmlns:regexp="http://exslt.org/regular-expressions"
  extension-element-prefixes="ms"
  exclude-result-prefixes="user ms regexp"
  version="1.0">
<xsl:output method="html" version="4.01" encoding="UTF-8"/>
<ms:script language="JScript" implements-prefix="user">
          function simfunc(msg)
          {
            return msg.replace(/^Fa./, '');
          }
</ms:script>
<xsl:template match="/">
  <html lang="de">
    <head>
      <title>Beispiel</title>
    </head>
    <body>
      <h1>Beispiel</h1>
      <xsl:for-each select="//artikel">
        <div>
          <p id="{generate-id()}">
            <xsl:choose>
              <xsl:when test="function-available('regexp:replace')">
                <xsl:value-of select="regexp:replace(lieferant, '^Fa'.', '', '')"/>
              </xsl:when>
              <xsl:when test="function-available('user:simfunc')">
                <xsl:value-of select="user:simfunc(string(lieferant))"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="lieferant"/>
              </xsl:otherwise>
            </xsl:choose>
          </p>     
        </div>
      </xsl:for-each>
    </body>
  </html>
</xsl:template>
</xsl:stylesheet>