将文件路径从XSL传递到JavaScript函数

Passing a file path from XSL to JavaScript Function

本文关键字:JavaScript 函数 XSL 文件 路径      更新时间:2024-02-23

我正在尝试将一个文件夹路径从XSL传递到JavaScript。函数在JavaScript中,该函数在XSL中的HTML按钮的onClick按钮上被调用。路径类似于"C:''ABC''DEF''GH"。在发出警报时,我看到路径被发送为:"CABCDEFGH"。删除所有斜杠。甚至我删除了对OnClick事件的函数调用,只是在那里放了一个带有硬编码路径的警报,还是一样的。它删除了所有斜杠。

<img class="viewcls" src="images/copy.jpg" title="Copy Profile" onclick="fnCopyProfile({$CurlDPID},'{@T}','{SOURCE/I/@DP}')"/>

这里,fnCopyProfile函数的最后一个参数是XPath,其值将是类似C:''ABC''DEF''GH的文件路径。在JS中,它没有斜杠。

即使我在XSL中放入警报,比如:

<img class="viewcls" src="images/copy.jpg" title="Copy Profile" onclick="alert('{SOURCE/I/@DP}');fnCopyProfile({$CurlDPID},'{@T}','{SOURCE/I/@DP}')"/>

然后它也显示了没有斜线的路径。

然而,如果我这样做:

<xsl:value-of select="SOURCE/I/@DP" />

然后它用斜线显示路径,但像这样,我想我们不能将值传递到JS中。

如何将带斜线的确切路径发送到JavaScript。

提前谢谢。

请确保正在转义所有'字符。在JavaScript字符串中使用时,'用于表示控制字符(例如,换行符的'n)。

因此,您需要做的是用''替换所有'字符。

我不知道如何使用您正在使用的内联变量(希望Dimitre能向我们展示)。

然而,你可以这样做。。。

<img class="viewcls" src="images/copy.jpg" title="Copy Profile">
  <xsl:attribute name="onclick">fnCopyProfile(<xsl:value-of select="$CurlDPID"/>,'<xsl:value-of select="@T"/>','<xsl:value-of select="translate(SOURCE/I/@DP,''','''')"/>');</xsl:attribute>
</img>

更新

上述方法无法工作,因为translate通过用单个字符替换单个字符来工作。

如果您使用的是XSLT2.0,那么我相信您可以做到这一点(w3.org参考资料)。。。

<xsl:value-of select="replace(SOURCE/I/@DP,'''',''''''")/>

''的原因是第2个和第3个参数是正则表达式,因此需要'转义。

如果你使用的是XSLT1.0,那么我刚刚通过谷歌找到了这篇文章,它提供了一个"搜索和替换"模板

<xsl:template name="string-replace-all">
  <xsl:param name="text" />
  <xsl:param name="replace" />
  <xsl:param name="by" />
  <xsl:choose>
    <xsl:when test="contains($text, $replace)">
      <xsl:value-of select="substring-before($text,$replace)" />
      <xsl:value-of select="$by" />
      <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text"
        select="substring-after($text,$replace)" />
        <xsl:with-param name="replace" select="$replace" />
        <xsl:with-param name="by" select="$by" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$text" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

你应该可以这样调用它(我把它放进了一个变量中,让它更清楚)。。。

<xsl:variable name="mypath">
  <xsl:call-template name="string-replace-all">
    <xsl:with-param name="text"><xsl:value-of select="SOURCE/I/@DP"/>
    <xsl:with-param name="replace">'</xsl:with-param>
    <xsl:with-param name="by">''</xsl:with-param>
  </xsl:call-template>
</xsl:variable>
<img class="viewcls" src="images/copy.jpg" title="Copy Profile">
  <xsl:attribute name="onclick">fnCopyProfile(<xsl:value-of select="$CurlDPID"/>,'<xsl:value-of select="@T"/>','<xsl:value-of select="$mypath"/>');</xsl:attribute>
</img>