0

XML:

<CONTROLS>
        <BUTTON>
               <input name="myButton" onclick="existingFunction();"/>
        </BUTTON>
        <LABEL>
               Text ME
        </LABEL>

</CONTROLS>

XSLT:

    <xsl:template match="/">
       <xsl:apply-templates select="CONTROLS/BUTTON/input"/>
    </xsl:template>  

    <xsl:template match="input">
        <xsl:variable name="existingOnclickFunction" select="/@onclick"/>
        <xsl:copy>
          <xsl:attribute name="onclick">
             <xsl:value-ofselect="$existingOnclickFunction"/>newFunction();
           </xsl:attribute>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
       </xsl:template>

   <!--Identity template copies content forward -->
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>

预期输出:

<input name="myButton" onclick="existingFunction();newFunction(); "/>

问题:

 I'm getting the "input" node using xpath/template and adding another function on the 

“点击”属性。是否可以?如果是的话,我在这里错过了什么吗?我的代码不起作用。还有另一种方法吗?

 Thanks in advance :)
4

1 回答 1

1

这种转变

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="input">
   <input onclick="{@onclick}newFunction();">
      <xsl:copy-of select="@*[not(name()='onclick')]"/>
   </input>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<CONTROLS>
    <BUTTON>
        <input name="myButton" onclick="existingFunction();"/>
    </BUTTON>
    <LABEL>Text ME</LABEL>
</CONTROLS>

产生想要的正确结果

<input onclick="existingFunction();newFunction();" name="myButton"/>

说明

正确使用模板和AVT(属性值模板)。

于 2012-12-12T04:13:07.967 回答