Microsoft KB Archive/301394

= How To Create an Generic ASP Page to Test XSL Transformations =

Article ID: 301394

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft XML Parser 2.6
 * Microsoft XML Core Services 4.0
 * Microsoft XML Parser 3.0 Service Pack 1
 * Microsoft XML Core Services 4.0
 * Microsoft Active Server Pages 4.0

-



This article was previously published under Q301394



SUMMARY
When you develop XML-based, server-side Active Server Pages (ASP) applications, it is a common programming requirement to load Extensible Markup Language (XML) and Extensible Stylesheet Language Transformation (XSLT) documents into instances of the Microsoft XML (MSXML) DOMDocument object and use an XSLT style sheet to programmatically transform the XML.

This article demonstrates how to create a generic ASP page that allows you to evaluate the outcome of using different XSLT documents to transform an XML document without adding or modifying an  processing instruction in the XML document.



Step-by-Step Example
  In your favorite HTML editor, create a new ASP page named TransformXml.asp, and paste the following code: <% Dim xmldoc Dim xsldoc

'Use the MSXML 4.0 Version dependent PROGID 'MSXML2.DOMDocument.4.0 if you wish to create 'an instance of the MSXML 4.0 DOMDocument object Set xmldoc = Server.CreateObject(&quot;MSXML2.DOMDocument&quot;) Set xsldoc = Server.CreateObject(&quot;MSXML2.DOMDocument&quot;)

xmldoc.Load Server.MapPath(Request.QueryString(&quot;xml&quot;))

'Check for a successful load of the XML Document. if xmldoc.parseerror.errorcode <> 0 then Response.Write &quot;Error loading XML Document :&quot; & &quot;&quot; Response.Write &quot;&quot; & &quot;&quot; Response.Write &quot;Error Code : &quot; & xmldoc.parseerror.errorcode & &quot;&quot; Response.Write &quot;Reason : &quot; & xmldoc.parseerror.reason & &quot;&quot; Response.End End If

xsldoc.Load Server.MapPath(Request.QueryString(&quot;xsl&quot;))

'Check for a successful load of the XSL Document. if xsldoc.parseerror.errorcode <> 0 then Response.Write &quot;Error loading XSL Document :&quot; & &quot;&quot; Response.Write &quot;&quot; & &quot;&quot; Response.Write &quot;Error Code : &quot; & xsldoc.parseerror.errorcode & &quot;&quot; Response.Write &quot;Reason : &quot; & xsldoc.parseerror.reason & &quot;&quot; Response.End End If

Response.Write xmldoc.TransformNode(xsldoc)

%>                    Save TransformXml.asp in your default Web site (which is usually <drive>:\Inetpub\Wwwroot).</li>  Create a new XML file named Books.xml, and paste the following code: <?xml version=&quot;1.0&quot;?> <Books xmlns=&quot;http://myserver&quot;> <Book> <Title>Beginning XML</Title> <Author>John</Author> </Book> <Book> <Title>Mastering XML</Title> <Author>Peter</Author> </Book> </Books> </li> Save Books.xml in your default Web site.</li>  Create a new XSL file named Books.xsl, and paste the following code: <xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> <xsl:template match=&quot;Books&quot;> <HTML> <BODY> </BODY> </HTML> </xsl:template> </xsl:stylesheet> </li> Save Books.xsl in your default Web site.</li> In Internet Explorer, type the following URL in the Address bar to browse to the ASP page:

http://<MyWebServer>/TransformXml.asp?xml=Books.xml&xsl=Books.xsl

where <MyWebServer> is the name of your Web server. This uses Books.xsl to evaluate the results of transforming Books.xml.</li></ol>

TransformXml.asp takes two input QueryString parameters. The xml QueryString parameter should specify the name of the XML document that is going to be transformed, and the xsl QueryString parameter should specify the name of the XSLT style sheet that will be applied to the XML document. The code in the ASP page loads the specified XML and XSL documents into instances of the MSXML DOMDocument object and programmatically runs the transformation. Any parser errors that are encountered while trying to load the XML or the XSL document are reported accordingly.

To test the outcome of applying another style sheet to the same XML document, modify the xsl QueryString parameter to point to a different style sheet and refresh the page. Similarly, to evaluate XSLT transformations that use other XML and XSL files, modify the xml QueryString and xsl QueryString parameters to point to the files that need to be tested.

<div class="references_section">