Transforming XML with XSLT using classic ASP
- The XML
- The XSL
- The ASP transform function
- The donor ASP page
- How it Works - The transformation
- Download - Sample Code
The XML
<?xml version="1.0" encoding="iso-8859-1"?> <widgets> <widget name="Company X Pty Ltd" id="1"> <url>www.compx.com.au</url> <email>info@compx.com.au</email> <sitegraphic alt="Widget X">widgetx.jpg</sitegraphic> <descrip>this company sells x widgets</descrip> </widget> <widget name="Company Y Pty Ltd" id="2"> <url>www.compy.com.au</url> <email>sales@compy.com.au</email> <sitegraphic alt="Widget Y">widgety.jpg</sitegraphic> <descrip>Thiscompany sells y widgets</descrip> </widget> </widgets>
The XSL
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
<xsl:output method="html" omit-xml-declaration="yes"/>
<xsl:param name="param1"/>
<xsl:template match="widgets">
<xsl:apply-templates select="widget"/>
</xsl:template>
<xsl:template match="widget">
<div >
<div ><xsl:value-of select="@name" /></div>
<table width="490" border="0" cellspacing="0" cellpadding="2">
<tr>
<td width="60" valign="top">Web</td>
<td width="280" valign="top"><xsl:apply-templates select="url"/></td>
<td width="150" rowspan="4" valign="top"><xsl:apply-templates select="sitegraphic"/>
</td>
</tr>
<tr>
<td valign="top">Email</td>
<td valign="top"><xsl:apply-templates select="email"/></td>
</tr>
<tr>
<td valign="top">About</td>
<td valign="top"><xsl:apply-templates select="descrip"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</div>
<br/><br/>
</xsl:template>
<xsl:template match="url">
<a href="http://{.}"><xsl:value-of select="." /></a>
</xsl:template>
<xsl:template match="email">
<a href="mailto:{.}"><xsl:value-of select="." /></a>
</xsl:template>
<xsl:template match="sitegraphic">
<img src="images/clients/{.}" alt="{@alt}" width="150" height="112" />
</xsl:template>
<xsl:template match="descrip">
<xsl:value-of select="." />
</xsl:template>
</xsl:stylesheet>
The ASP Transform Function
function xsltransform(xmlfile,xslfile,strOptionalParam1,strOptionalParam2)
dim objXML
dim objXSL
dim templates
dim transformer
Set objXML = Server.CreateObject("MSXML2.FreeThreadedDOMDocument.3.0")
objXML.async = false
Set objXSL = Server.CreateObject("MSXML2.FreeThreadedDOMDocument.3.0")
objXSL.async = false
'set the parser properties
objXML.ValidateOnParse = True
objXSL.ValidateOnParse = True
'load the source XML document and check for errors
objXML.load Server.MapPath("/") & "/" & xmlfile
if objXML.parseError.errorCode <> 0 Then
'error found so show error message and stop
Response.End
end if
'load the XSL stylesheet and check for errors
objXSL.load Server.MapPath("/") & "/" & xslfile
if objXSL.parseError.errorCode <> 0 Then
'error found so show error message and stop
Response.End
end if
Set templates = Server.CreateObject("Msxml2.XSLTemplate.3.0")
templates.stylesheet = ObjXSL
Set transformer = templates.createProcessor()
if len(strOptionalparam1) then
transformer.addParameter "param1", strOptionalParam1
end if
if len(strOptionalparam2) then
transformer.addParameter "param2", strOptionalParam2
end if
transformer.input = objXML
transformer.transform()
xsltransform = transformer.output
end function
The donor ASP page
<%@ LANGUAGE="VBSCRIPT"%> <%OPTION EXPLICIT%> <%Response.Buffer = True%> <!--#include virtual="/xsltransform.asp"--> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>XML/XSLT Transform</title> </head> <body> <div> <%xsltransform "/folio.xml","/folio.xsl","","",1%> </div> </body> </html>
How it Works

XML
The xml describes a simple relationship of a list of widgets and some information about those widgets.
- Widget ID
- Widget Name
- Web Site
- Contact Email
- Picture of widget
- Description of widget
The XSL is meant to generate a simple fragment of html (a table) that is rendered into the page by the server.
<xsl:output method="html" omit-xml-declaration="yes"/>
Stops the XML declaration from being rendered into your HTML fragment
<xsl:template match="url">
<a href="http://{.}"><xsl:value-of select="." /></a>
</xsl:template>
It is good to have a template per node in the xml so that you can be very specific on how it is rendered back into html.
For more information on XSLT, try out this tutorial from www.w3schools.com
ASP Transform FunctionI prefer to keep the function in a seperate asp file and include it dynamically. (Write once use many)
The function has allowed for up to two parameters to be passed in. This will allow you to conditionally display data. ( ie just display widget with an ID of 2)
ASP Donor PageThis is a very simple test page. Please use CSS to layout your page.
