If you do not include the
DEFER attribute for the <SCRIPT> tag, and the function is called from the transformed document, an "Object Expected" error occurs.
In addition, you must enclose the
SCRIPT block in <xsl:comment> tags and a CDATA section. If you do not wrap the script in a CDATA section, the XML parser parses the contents of the
SCRIPT block. Because script can contain reserved characters, such as "<" and ">", the CDATA section protects the parser from potentially invalid XML within the
SCRIPT block.
Sample Code
NOTE: Make sure that you save the following files in the same folder.
- Create a new XML file named Xmldoc.xml, and then paste the following code:
<?xml version="1.0"?>
<nodes>
<node>Hello</node>
<node>World</node>
</nodes>
- Create a new XSL file named Xsldoc.xsl, and then paste the following code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<HTML>
<BODY>
<SCRIPT LANGUAGE="javascript" DEFER="true">
<xsl:comment>
<![CDATA[
function hiLite()
{
alert("hello");
}
]] >
</xsl:comment>
</SCRIPT>
<INPUT id="BUTTON1" name="BUTTON1" onclick="hiLite()" type="button" value="aTest"/>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
IMPORTANT: For the preceding code to work in Internet Explorer, you must delete the space between the bracket (]) and the greater-than symbol (>) in the following line:
It was necessary to include this space to format the code in this article. - Create a new HTML file named Demo.htm, and then paste the following code:
<html>
<head>
<SCRIPT language="JavaScript">
<!-- begin script
function transformDoc()
{
var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
var xsldoc = new ActiveXObject("Microsoft.XMLDOM");
xmldoc.async = false;
xsldoc.async = false;
xmldoc.load('xmldoc.xml');
xsldoc.load('xsldoc.xsl');
if((xmldoc.parseError.errorCode == 0) && (xsldoc.parseError.errorCode ==0))
document.body.innerHTML = xmldoc.transformNode(xsldoc);
else
{
document.body.innerHTML = 'Reason: ' + xmldoc.parseError.reason;
document.body.innerHTML +='<br>' + 'Reason: ' + xsldoc.parseError.reason;
}
}
//-->
</script>
</head>
<body>
<input type=button onclick="transformDoc()" value="Transform">
</body>
</html>
- In Internet Explorer, open Demo.htm, and then click Transform.
- To fire the function that is applied to the document from the transformation, click Test Scripting. A button with lable aTest is displayed.
- To fire the function, click aTest. An alert is displayed.