将外部javaScript函数调用到XSLT文件中

问题描述 投票:1回答:1

我试图在XSLT文件中调用外部javaScript函数,当我单击图像元素时将调用该函数。

XSLT文件如下:

 <xsl:template match="link|para//link">
    <xsl:element name="a">
      <xsl:attribute name="href">
        <!--OpenPopupDetailsTexte('<xsl:value-of select="@href"/>', 1);-->
        <!--alert();-->
      </xsl:attribute>
      <xsl:attribute name="title">
        <xsl:choose>
          <xsl:when test="text() = 'n'">Note circulaire</xsl:when>
          <xsl:when test="text() = 'm'">Modification</xsl:when>
          <xsl:when test="text() = 'd'">D&#233;cret d application</xsl:when>
          <xsl:when test="text() = 'ma'">Abrogation</xsl:when>
          <xsl:when test="text() = 't'">Renvoi au texte</xsl:when>
          <xsl:when test="text() = 'a'">Arr&#234;t&#233; minist&#233;riel</xsl:when>
          <xsl:when test="text() = 'mc'">Texte compl&#233;tant cette disposition</xsl:when>
          <xsl:otherwise></xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:element name="img">
        <xsl:attribute name="src">/assets/projets/images/<xsl:value-of select=". "/>.gif</xsl:attribute>
        <xsl:attribute name="border">0</xsl:attribute>
      </xsl:element>
    </xsl:element>
  </xsl:template>

我的外部函数的名称是OpenPopupDetailsTexte。

HTML中的结果是:

<a href="unsafe:&#10;        javascript:OpenPopupDetailsTexte('cgitva_T19_N1', 1)&#10;      " title="Note circulaire"><img src="/assets/projets/images/n.gif" border="0"></a>
javascript html xml angular xslt
1个回答
2
投票

请注意,您没有在XSLT中调用外部JavaScript函数。您只是输出文本,当在浏览器中处理结果输出时,恰好将其解析为Javascript。

无论如何,你需要在xsl:text中包装相关的javascript文本,以防止包含换行符。 (如果同一节点中存在非空白字符,则不会在XSLT中删除空格)

<xsl:element name="a">
  <xsl:attribute name="href">
    <xsl:text>javascript:OpenPopupDetailsTexte('<xsl:text>
    <xsl:value-of select="@href"/>
    <xsl:text>', 1);</xsl:text>
  </xsl:attribute>

或者,更好的是,使用Attribute Value Templates ......

<a href="javascript:OpenPopupDetailsTexte('{@href}', 1);">

因此,花括号中的表达式表示要计算的表达式,而不是字面上的输出。 (另请注意,当名称是静态时,不需要使用xsl:element来创建元素)。

© www.soinside.com 2019 - 2024. All rights reserved.