Compartilhar via


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.

Example: Using XSLT with SAX from Script

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)

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

   //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;
   xslDoc.load("transform1.xsl");

   //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;
   xmlDoc.load("books.xml");

   //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.
   xslProc.transform();

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

</SCRIPT>
</BODY>
</HTML>

XML stylesheet File (transform1.xsl)

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="catalog">
<HTML>
 <HEAD>
   <TITLE>Books catalog - listing for Books.xml</TITLE>
 </HEAD>
 <BODY><FONT FACE="Verdana">
 <H3>My Books Catalog</H3>
 <TABLE CELLPADDING="5" CELLSPACING="2">
   <xsl:for-each select="book">
   <TR>
      <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>
   </TR>
   </xsl:for-each>
</TABLE>

</FONT>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

See Also

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