Transforming XML with XSLT using classic ASP


<?xml version="1.0" encoding="iso-8859-1"?>

	<widget name="Company X Pty Ltd" id="1">
		<sitegraphic alt="Widget X">widgetx.jpg</sitegraphic>
		<descrip>this company sells x widgets</descrip>
	<widget name="Company Y Pty Ltd" id="2">
		<sitegraphic alt="Widget Y">widgety.jpg</sitegraphic>
		<descrip>Thiscompany sells y widgets</descrip>


<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="" 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 match="widget">
<div >
<div ><xsl:value-of select="@name" /></div>
<table width="490" border="0" cellspacing="0" cellpadding="2">
    <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 valign="top">Email</td>
    <td valign="top"><xsl:apply-templates select="email"/></td>

    <td valign="top">About</td>
    <td valign="top"><xsl:apply-templates select="descrip"/></td>
    <td> </td>
    <td> </td>


<xsl:template match="url">
<a href="http://{.}"><xsl:value-of select="." /></a>
<xsl:template match="email">
<a href="mailto:{.}"><xsl:value-of select="." /></a>
<xsl:template match="sitegraphic">
<img src="images/clients/{.}" alt="{@alt}" width="150" height="112" />
<xsl:template match="descrip">
<xsl:value-of select="." />

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
	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
	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
	xsltransform = transformer.output
end function

The PHP Transform Function

Below is a comparable example using PHP.

			$proc = new XSLTProcessor();
             $proc->setParameter('', 'param1', $param1);
             $proc->setParameter('', 'param2', $param2);

			//Load XSL document 

			$xsl = new DOMDocument(); 

			//Load XML document 

   			$xml = new DOMDocument;

			if ($xsl && $xml){
   				$theresult =  $proc->transformToXml($xml); 

				$theresult = "

Transform Error

“; } return $theresult; }

The donor ASP page

<%Response.Buffer = True%>
<!--#include virtual="/xsltransform.asp"-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>XML/XSLT Transform</title>

<%xsltransform "/folio.xml","/folio.xsl","","",1%>


How it Works

Flow diagram of XML/XSLT Transformation


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>


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

ASP Transform Function

I 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 Page

This is a very simple test page. Please use CSS to layout your page.