Sunit Katkar's Home Page

 Tutorials:
 

Using ASP for server side creation of HTML by parsing XML and XSL

It is quite straightforward to use ASP and the MS XML Parser object to achieve server side HTMl generation from a XML file and its associated XSL file. The real work of formatting and visual presentation information is all stored in the XSL stylesheet. ASP will just help us to transform the XML into HTML using this carefully crafted XSL style sheet.

A sample ASP file for crearting HTML output from a XML and its associated XSL file

<%@Language="VBscript"%>
<%
  Dim objXMLSourceFile, objXMLSource
  Dim objXSLSourceFile,  objXSLSource


  '------------------------------------------------
  'Set the source of the XML file
  '------------------------------------------------
  objXMLSourceFile = Server.MapPath("myResume.xml")


  '------------------------------------------------
  'Set the source of the XSL file
  '------------------------------------------------
  objXSLSourceFile = Server.MapPath("myResume.xsl")


  '------------------------------------------------
  'Lets create an instance of the MS XML parser.
  'We will then load the XML document into the DOM
  '------------------------------------------------
  Set objXMLSource = Server.CreateObject("Microsoft.XMLDOM")

  '------------------------------------------------
  'We want the document to be loaded fully before
  'the parsing starts so we force it load all at once
  'by setting the async property as false
  '------------------------------------------------
  objXMLSource.async = false

  '------------------------------------------------
  'Load the source xml file
  '------------------------------------------------
  objXMLSource.load objXMLSourceFile

  '------------------------------------------------
  'Lets now create an instance of the MS XML parser
  'and load the XSL document into the DOM
  '------------------------------------------------
  Set objXSLSource = Server.CreateObject("Microsoft.XMLDOM")


  '------------------------------------------------
  'We want the document to be loaded fully before
  'the parsing starts so we force it load all at once
  'by setting the async property as false
  '------------------------------------------------
  objXSLSource.async = false

  '------------------------------------------------
  'Load the source xml file
  '------------------------------------------------
  objXSLSource.load objXSLSourceFile


  '------------------------------------------------
  'write the trasformed document to the browser
  '------------------------------------------------
  Response.Write (objXMLSource.TransformNode(objXSLSource))

  '------------------------------------------------
  'Clean up.. release the XML parser object
  '------------------------------------------------
  Set objXMLSource = Nothing
  Set objXSLSource = Nothing
  
%>

And here is the output of the above code

Click here for output

 

You can apply Cascading Style Sheets (CSS) style sheets within the XSL style sheet for further control over the final HTML rendering.

You can download all the files used in this example by clicking here.

Main |  1 |  2 |  3 |  4