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 <?xml-stylesheet?> 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 6.0 Version dependent PROGID
'MSXML2.DOMDocument.6.0 if you wish to create
'an instance of the MSXML 6.0 DOMDocument object
Set xmldoc = Server.CreateObject("MSXML2.DOMDocument")
Set xsldoc = Server.CreateObject("MSXML2.DOMDocument")
xmldoc.Load Server.MapPath(Request.QueryString("xml"))
'Check for a successful load of the XML Document.
if xmldoc.parseerror.errorcode <> 0 then
Response.Write "Error loading XML Document :" & "<BR>"
Response.Write "----------------------------" & "<BR>"
Response.Write "Error Code : " & xmldoc.parseerror.errorcode & "<BR>"
Response.Write "Reason : " & xmldoc.parseerror.reason & "<BR>"
Response.End
End If
xsldoc.Load Server.MapPath(Request.QueryString("xsl"))
'Check for a successful load of the XSL Document.
if xsldoc.parseerror.errorcode <> 0 then
Response.Write "Error loading XSL Document :" & "<BR>"
Response.Write "----------------------------" & "<BR>"
Response.Write "Error Code : " & xsldoc.parseerror.errorcode & "<BR>"
Response.Write "Reason : " & xsldoc.parseerror.reason & "<BR>"
Response.End
End If
Response.Write xmldoc.TransformNode(xsldoc)
%>
- Save TransformXml.asp in your default Web site (which is usually <drive>:\Inetpub\Wwwroot).
- Create a new XML file named Books.xml, and paste the following code:
<?xml version="1.0"?>
<Books xmlns="http://myserver">
<Book>
<Title>Beginning XML</Title>
<Author>John</Author>
</Book>
<Book>
<Title>Mastering XML</Title>
<Author>Peter</Author>
</Book>
</Books>
- Save Books.xml in your default Web site.
- Create a new XSL file named Books.xsl, and paste the following code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Books">
<HTML>
<BODY>
<TABLE BORDER="2">
<TR>
<TD>Title</TD>
<TD>Author</TD>
</TR>
<xsl:for-each select="Book">
<TR>
<TD><xsl:value-of select="Title"/></TD>
<TD><xsl:value-of select="Author"/></TD>
</TR>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
- Save Books.xsl in your default Web site.
- 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.
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.
For more information, click the following article number to view the article in the Microsoft Knowledge Base:
258294
(http://support.microsoft.com/kb/258294/
)
How To Offload XSL Transformations to Clients' Browsers