Aug
14
2010
Apr
21
2010
Transforming XML using classic ASP
ASP.NET makes it very easy to transform XML using XSLT, but it is not as obvious when using classic ASP. Here is one way to accomplish an XML transformation using classic ASP.
First of all we can define a transform function:
function xsltransform(xmlfile,xslfile, strOptionalParam1,strOptionalParam2) dim objXML dim objXSL dim templates dim transformer 'create two document instances 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 xmlfile if objXML.parseError.errorCode <> 0 Then 'error found so stop response.write "Error with transform: XML file" 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 stop response.write "Error with transform: XSL file" response.end end if 'all must be OK, so perform transformation 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
And then transform the XML as follows:
xsltransform(XMLObject, "/XSLTFile.xslt", "", "")
Thanks to Allan Browning at Inov8 Design for this function.
