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>
Expected Output:
<input name="myButton" onclick="existingFunction();newFunction(); "/>
QUESTION:
I'm getting the "input" node using xpath/template and adding another function on the
“onclick” attribute. Is it possible? If yes, am I missing something here? My code doesn’t
work. Is there another approach?
Thanks in advance :)
This transformation:
when applied on the provided XML document:
produces the wanted, correct result:
Explanation:
Proper use of templates and of AVT (Attribute Value Templates).