Using XSLT with SAX


With MSXML 6.0, you can also use the output property of the IXSLProcessor interface to accept objects that implement SAX interfaces. This enables you to fire SAX events from XSLT transformations, and to catch those events in your SAX-based application.

This example shows how to use the SAX writer from within a Web script to accept and write the output of an XSLT processor transformation. The transformation is performed on the Sample XML File (books.xml).

To complete this example, you must create and use the following two additional files.

  • Books.html, an HTML document file used to host and run the sample code. This code will be used in Internet Explorer, inline as client-side script.

  • Transform1.xsl, an XSLT style sheet. This style sheet will be used to transform the contents of the sample file (books.xml), and to present it in the browser as HTML.

Try It! To run this example, copy the HTML and XSLT samples below, and save them under the specified file names. Save the books.xml file in the same folder. Open books.html to view the output.

HTML File (books.html)

<TITLE>Test for XSLT output to SAX XML writer application</TITLE>
<DIV ID="result">Document is loading...</DIV>

   //Create the XSLTemplate object (xslt).
   var xslt = new ActiveXObject("Msxml2.XSLTemplate.6.0");

   //Create and load the stylesheet (transform1.xsl) as a DOMDocument.
   var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.6.0");
   var xslProc;
   xslDoc.async = false;

   //Connect the XSLTemplate object to stylesheet DOMDocument.
   xslt.stylesheet = xslDoc;

   //Create XSLT processor using stylesheet for XSL template.
   xslProc = xslt.createProcessor();

   //Create and load sample XML file (books.xml) as a DOMDocument.
   var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0");
   xmlDoc.async = false;

   //Create SAX writer.
   var xmlWriter = new ActiveXObject("Msxml2.MXXMLWriter.6.0");

   //Assign XML sample file as input of the transform() method.
   xslProc.input = xmlDoc;

   //Use a SAX writer as the output of the transform() method.
   xslProc.output = xmlWriter;

   //Do transformation on the sample XML file.

   //Use SAX writer ouptut to generate inner HTML for page.
   result.innerHTML = xmlWriter.output;


XML stylesheet File (transform1.xsl)

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
<xsl:output method="xml" indent="yes"/>
<xsl:template match="catalog">
   <TITLE>Books catalog - listing for Books.xml</TITLE>
 <BODY><FONT FACE="Verdana">
 <H3>My Books Catalog</H3>
   <xsl:for-each select="book">
      <TD ALIGN="LEFT"><FONT SIZE="2"><B>ID:</B>&#160;&#160;<xsl:value-of select="@id"/><BR/>
      <B>Author:</B>&#160;&#160;<xsl:value-of select="author"/><BR/>
      <B>Title:</B>&#160;&#160;<xsl:value-of select="title"/><BR/>
      <B>Genre:</B>&#160;&#160;<xsl:value-of select="genre"/><BR/>
      <B>Price:</B>&#160;&#160;<xsl:value-of select="price"/><BR/>
      <B>Published on:</B>&#160;&#160;<xsl:value-of select="publish_date"/><BR/>
      <B>Description:</B>&#160;&#160;<xsl:value-of select="description"/></FONT></TD>

Increasing Performance by Using the XSLTemplate Object
output Property
JumpStart for Creating a SAX2 Application