After referring the following two references, I have created a javascript function in XSL template. The javascript is getting called and an alert is appearing. But the value of CountID is coming as empty in the alert though it has value in html.
What change do we need to do to alert the CountID?
Note: This is in an ASP.Net project and I am using Visual Studio 2005 Note: This is an existing project and I am new to XSLT (though I have knowledge on javascript,xml, kendoui, html5, etc.)
REFERENCES
Javascript
<SCRIPT LANGUAGE="javascript" DEFER="true">
var myVar = "<xsl:value-of select="CountID"/>";
<xsl:comment>
function on_change()
{
alert(myVar);
}
</xsl:comment>
</SCRIPT>
Source XML
<ROOT>
<Counts>
<CountID>34rrgt56</CountID>
<PlantCD>01</PlantCD>
<CountType>PI</CountType>
<StatusCD>OP</StatusCD>
<EntryCompleteIND>N</EntryCompleteIND>
<StartedBy>Lijo C</StartedBy>
<CreatedOnDate>05/09/2014 00:19:04</CreatedOnDate>
</Counts>
</ROOT>
Note: This XML is passed to a custom control where it does some more additions. However for this question, this xml should be enough.
Complete XSL Stylesheet
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<SCRIPT LANGUAGE="javascript" DEFER="true">
var myVar = "<xsl:value-of select="CountID"/>";
<xsl:comment>
function on_change()
{
alert(myVar);
}
</xsl:comment>
</SCRIPT>
<div>
<TABLE border="0" cellPadding="0" cellSpacing="0" BGCOLOR="#E5DBE2" width="100%">
<TR>
<TD CLASS="Heading3" ALIGN="LEFT" WIDTH="25%">
<xsl:value-of select="/ROOT/Labels/PhysicalInventoryIDText/." />
</TD>
<TD CLASS="Heading3" ALIGN="LEFT" WIDTH="20%">
<xsl:value-of select="/ROOT/Labels/DateTimeText/." />
<BR/>
<xsl:value-of select="ROOT/Labels/StartedText/." />
</TD>
<TD CLASS="Heading3" ALIGN="LEFT" WIDTH="15%"></TD>
</TR>
</TABLE>
<TABLE style="display:none" name="emailTable" id="emailTable" width="100%" BGCOLOR="#E5DBE2">
<TR>
<TD width="25%" CLASS="Normal" colspan="1" ALIGN="RIGHT">
<B>
<xsl:value-of select="/ROOT/Labels/EmailAddressText/." />
</B>
</TD>
<TD width="75%" ALIGN="LEFT">
<img src="../Images/ArrowInvisible.gif" width="15" height="13" name="imgEmail1" />
<INPUT id="txtEmail1" name="txtEmail1" runat="server" width="100px" maxlength="100" Required="true" onfocus="document.forms[0].imgEmail1.src='../Images/ArrowVisibleDB.gif';" onblur="document.forms[0].imgEmail1.src='../Images/ArrowInvisible.gif';" />
</TD>
</TR>
<TR>
<TD width="25%" CLASS="Normal" colspan="1" ALIGN="RIGHT">
<B>
<xsl:value-of select="/ROOT/Labels/EmailAddressText/." />
</B>
</TD>
<TD width="75%" ALIGN="LEFT">
<img src="../Images/ArrowInvisible.gif" width="15" height="13" name="imgEmail2" />
<INPUT id="txtEmail2" name="txtEmail2" runat="server" width="100px" maxlength="100" Required="true" onfocus="document.forms[0].imgEmail2.src='../Images/ArrowVisibleDB.gif';" onblur="document.forms[0].imgEmail2.src='../Images/ArrowInvisible.gif';" />
</TD>
</TR>
</TABLE>
<TABLE border="0" cellPadding="0" cellSpacing="0" BGCOLOR="#E5DBE2" width="100%">
<xsl:for-each select="ROOT/Counts">
<TR>
<TD CLASS="Normal" ALIGN="LEFT" width="25%">
<xsl:value-of select="CountID"/>
</TD>
<TD CLASS="Normal" ALIGN="LEFT" width="20%" style="font-size:12">
<xsl:value-of select="CreatedOnDate"/>
</TD>
<TD ALIGN="RIGHT" width="15%">
<Input type="button">
<xsl:attribute name="id">
<xsl:value-of select="CountID"/>
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="CountID"/>
</xsl:attribute>
<xsl:attribute name="onclick">
<xsl:text>on_change()</xsl:text>
</xsl:attribute>
<xsl:attribute name="value">Conclude</xsl:attribute>
</Input>
</TD>
</TR>
<xsl:if test="LastError!=''">
<TR>
<TD COLSPAN="3" CLASS="Normal" ALIGN="LEFT" width="100%">
Last Error: <xsl:value-of select="LastError"/>
</TD>
</TR>
</xsl:if>
<TR style="display:none">
<xsl:attribute name="id">
row<xsl:value-of select="CountID" />
</xsl:attribute>
<TD class="NormalError" colspan="4">
<xsl:value-of select="/ROOT/Labels/PIConcludeWarning/." />
<Input type="button" >
<xsl:attribute name="value">
<xsl:value-of select="/ROOT/Labels/YesText/." />
</xsl:attribute>
<xsl:attribute name="onclick">
submitForm(this,this.form,'<xsl:value-of select="CountID"/>')
</xsl:attribute>
</Input>
<xsl:text disable-output-escaping="yes"> </xsl:text>
<Input type="button" >
<xsl:attribute name="value">
<xsl:value-of select="/ROOT/Labels/CancelText/." />
</xsl:attribute>
<xsl:attribute name="onclick">
window.row<xsl:value-of select="CountID" />.style['display'] = 'none';window.emailTable.style['display'] = 'none';
</xsl:attribute>
</Input>
</TD>
</TR>
<TR HEIGHT="1">
<TD BGCOLOR="#FFFFFF" COLSPAN="6" height="1">
<NOBR/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</div>
</xsl:template>
</xsl:stylesheet>
CountIDthe code doingvar myVar = "<xsl:value-of select="CountID"/>";does nothing but assign an empty string. And your other expressions like<xsl:value-of select="/ROOT/Labels/PhysicalInventoryIDText/." />suggest the root element is namedROOT.<Input type="button"><xsl:attribute name="id"><xsl:value-of select="CountID"/></xsl:attribute>/then the<xsl:value-of select="CountID"/>selects a root element namedCountID. If there is no such element you output an empty string. So you need to make sure you use a path selecting the element at its nesting level, for instance<xsl:value-of select="//CountID"/>would select the firstCountIDelement at any level in the input orselect="/ROOT/CountID"would select a child element namedCountIDof the root element namedROOT.select="//CountID"/>"it is working. But I think that would give me wrong result if I have more than one row (though I have one row in my current example)? Do you have any alternative suggestions that will work correctly even when there are multiple rows?