Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Using XSLT and JavaScript in the Internet Explorer

The XSLT processor in the Internet Explorer 5.5 is part of the MSXML3 XML parser, and if you access MSXML3 directly, using JavaScript, you don't have to modify the original planets.xml and planets.xsl (Listings 1.1 and 1.2) as you saw in the previous section. You'll see how this works in Chapter 10, but here's a Web page, xslt.html, that uses JavaScript and MSXML3 to transform planets.xml using planets.xsl and displays the results (note that you can adapt this document to use your own XML and XSLT documents without writing any JavaScript; just replace the names planets.xml and planets.xsl with the names of your XML and XSL documents):

Listing 1.5 Microsoft Internet Explorer JavaScript Transformation

<HTML>
  <HEAD>
    <TITLE>XSLT Using JavaScript</TITLE>

    <SCRIPT LANGUAGE="JavaScript">
    <!--

    function xslt()
    {
      var XMLDocument = new ActiveXObject('MSXML2.DOMDocument.3.0');
      var XSLDocument = new ActiveXObject('MSXML2.DOMDocument.3.0');
      var HTMLtarget = document.all['targetDIV'];

      XMLDocument.validateOnParse = true;
      XMLDocument.load('planets.xml');
      if (XMLDocument.parseError.errorCode != 0) {
         HTMLtarget.innerHTML = "Error!"
         return false;
      }

      XSLDocument.validateOnParse = true;
      XSLDocument.load('planets.xsl');
      if (XSLDocument.parseError.errorCode != 0) {
         HTMLtarget.innerHTML = "Error!"
         return false;
      }

      HTMLtarget.innerHTML = XMLDocument.transformNode(XSLDocument);
      return true;
    }

    //-->
    </SCRIPT>
  </HEAD>

  <BODY onload="xslt()">
    <DIV ID="targetDIV">    </DIV>
  </BODY>
</HTML>

This Web page produces the same result you see in Figure 1.3, and it doesso by loading planets.xml and planets.xsl directly and applying the MSXML3 parser to them. These files, planets.xml and planets.xsl, are the same as we've seen throughout this chapter, without the modifications necessary in theprevious topic, where we navigated to planets.xml directly using the Internet Explorer. See Chapter 10 for more information.

Using VBScript

You can also use the Internet Explorer's other scripting language, VBScript, to achieve the same results if you're more comfortable with VBScript.

  • + Share This
  • 🔖 Save To Your Account