Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 301394 - Last Review: June 29, 2004 - Revision: 1.1
How To Create an Generic ASP Page to Test XSL Transformations
This article was previously published under Q301394
On This Page
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 <?xml-stylesheet?> processing instruction in the XML document.
MORE INFORMATION
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("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.
REFERENCES
For additional information, click the article number below
to view the article in the Microsoft Knowledge Base:
258294Â
(http://kbalertz.com/Feedback.aspx?kbNumber=258294/EN-US/
)
How To Offload XSL Transformations to Clients' Browsers
APPLIES TO
- Microsoft XML Parser 2.6
- Microsoft XML Parser 3.0
- Microsoft XML Parser 3.0 Service Pack 1
- Microsoft XML Core Services 4.0
- Microsoft Active Server Pages 4.0
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please