Currently, the default XML parser for Internet Explorer is MSXML 2.0 or MSXML 3.0, depending on the version of Internet Explorer. With the help of the xmslinst.exe utility, you can change the default parser to MSXML 2.6 or MSXML 3.0. However, this can often cause unintended side-effects for some applications. Therefore, such a practice is not enabled for MSXML versions 4.0 and later.
Until Internet Explorer ships with MSXML 4.0 or later as its default XML parser, the MSXML 4.0 and later features are available in Internet Explorer only via scripting, when an XML DOM object is instantiated using the MSXML4-specific ProgID. This means that you cannot use <?xml-stylesheet type="text/xsl" href="Products.xsl"?>
in an XML document to invoke MSXML 4.0 functionality.
However, you can write either server-side or client-side script to use MSXML 5.0 instead. In this SDK, we provide two ways to do this:
The following examples illustrate each method. You can use the code in these examples to test other samples in this SDK. Just change the XML and XSLT file names as appropriate.
When the following HTML page is loaded, the init()
function is invoked. This function loads the hello.xml and hello.xsl files into XML DOM objects: srcTree
and xsltTree
, respectively. It then applies the XSLT transformation by calling the transformNode
method on the scrTree
XML DOM object.
You can use this HTML page to test many of the other XSLT examples in this SDK, as well. You might want to use this approach if the output of the transformation is HTML. Otherwise, use the other approach, Initiate XSLT from a Command Prompt.
For hello.xml and hello.xsl, see Hello, World! (XSLT). The following is the code for the HTML page, hello.htm.
hello.htm
<HTML> <HEAD> <TITLE>sample</TITLE> <SCRIPT language = "javascript"> function init() { var srcTree = new ActiveXObject("Msxml2.DOMDocument.5.0"); srcTree.async=false; // You can substitute other XML file names here. srcTree.load("
hello.xml
");
var xsltTree= new ActiveXObject("Msxml2.DOMDocument.5.0");
xsltTree.async = false;
// You can substitute other XSLT file names here.
xsltTree.load("
hello.xsl
");
resTree.innerHTML = srcTree.transformNode(xsltTree);
}
</SCRIPT>
</HEAD>
<BODY onload = "init()" >
<div id="resTree"></div>
</BODY>
</HTML>
Note MSXML 5.0 is used here, with the help of the version dependent ProgID of the DOM, Msxml2.DOMDocument.5.0
. When you use this method to start the transformation, any XSLT style sheet that is already embedded in the source document is ignored. For more information about GUIDs and ProgIDs, see GUID and ProgID Information.
Try It!
You can use the following JScript file to test XSLT samples quickly from a command prompt. This approach will work for transformations with any type of output.
xsltTest.js
var oArgs = WScript.Arguments; if (oArgs.length == 0) { WScript.Echo ("Usage : cscript xslt.js xml xsl"); WScript.Quit(); } xmlFile = oArgs(0) + ".xml"; xslFile = oArgs(1) + ".xsl"; var xsl = new ActiveXObject("MSXML2.DOMDOCUMENT.5.0"); var xml = new ActiveXObject("MSXML2.DOMDocument.5.0"); xml.validateOnParse = false; xml.async = false; xml.load(xmlFile); if (xml.parseError.errorCode != 0) WScript.Echo ("XML Parse Error : " + xml.parseError.reason); xsl.async = false; xsl.load(xslFile); if (xsl.parseError.errorCode != 0) WScript.Echo ("XSL Parse Error : " + xsl.parseError.reason); //WScript.Echo (xml.transformNode(xsl.documentElement)); try { WScript.Echo (xml.transformNode(xsl.documentElement)); } catch(err) { WScript.Echo ("Transformation Error : " + err.number + "*" + err.description); }
To test samples with xsltTest.js
xsltTest sampleXML sampleXSLT
Notice the absence of file extensions on the arguments.