简体   繁体   English

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

[英]Call external javaScript function into XSLT file

I am trying to call an external javaScript function in XSLT file,the function will be called when i click on the image element. 我正在尝试在XSLT文件中调用外部javaScript函数,当我单击图像元素时将调用该函数。

The XSLT file is as bellow: 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>

the name of my external function is OpenPopupDetailsTexte . 我的外部函数的名称是OpenPopupDetailsTexte

the results in HTML is : 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>

Note that you are not calling an external javascript function in your XSLT. 请注意,您没有在XSLT中调用外部javascript函数。 You are simply outputting text which will just happen to be parsed as Javascript when the resultant output is processed in a browser. 您只是在输出文本,当在浏览器中处理结果输出时,这些文本恰好会被解析为Javascript。

Anyway, you need to wrap the relevant javascript text in xsl:text to prevent line breaks being included. 无论如何,您需要将相关的javascript文本包装在xsl:text以防止包含换行符。 (Whitespace won't be stripped in XSLT if there are non-whitespace characters in the same node) (如果同一节点中存在非空格字符,则不会在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>

Or, better still, use Attribute Value Templates ... 或者,更好的是,使用属性值模板 ...

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

So, the expressions in curly braces represent an expression to be evaluated, rather than output literally. 因此,花括号中的表达式表示要求值的表达式,而不是按字面意思输出。 (Also note there is no need to use xsl:element to create the element, when the name is going to be static). (还请注意,当名称变为静态时,无需使用xsl:element创建元素)。

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

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