SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Building Smart Pages with ASP, XML and XSL : Applying XSL on the Server

The builk of the hard work in our ASP script is handled by the TransformDocument and OutputDocument functions. We are going to start with the simpler OutputDocument function. Given an XML file, this function sets the document type to "text/xml" so the client knows it is receiving XML, opens the document, and finally sends it to the client.

function outputDocument(sFile)
  Dim defaultPath,objFSO, objFile
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  if objFSO.FileExists(server.mapPath(".") + "/" + sFile) Then
    response.contentType="text/xml"
    Set objFile = objFSO.OpenTextFile(server.mapPath(".") + "/" + sFile)
    outputDocument = objFile.readAll
    objFile.close
  else
    outputDocument = "XML File is Missing"
  end if
end function

An alternative to manually retrieving the file is to redirect the user to the XML file. We chose not to redirect so to keep a single URL. When you redirect, you are actually returning a code that tells the client to request a different page. This requires an additional round-trip and also exposes an alternative URL to the user which would only be valid in Internet Explorer 5.0 (or XML/ XSL enabled browsers).

The next function is the TransformDocument function. This function performs the same work IE5 provides but on the server. This function takes an XML and XSL file name. The XML and XSL file are loaded in memory on the server and then the XSL file is applied to the HTML to transform it into HTML. The generated HTML is then returned to the client. In this case, the client has no knowledge that your pages originated in XML.

function TransformDocument(srcXML, srcXSL) 
  Dim sourceFile, styleFile, source
  sourceFile = Server.MapPath(srcXML)
  styleFile = Server.MapPath(srcXSL)
  ' Load the XML 
  set source = Server.CreateObject("Microsoft.XMLDOM")
  source.async = false
  source.load sourceFile
  ' Load the XSL
  set  style = Server.CreateObject("Microsoft.XMLDOM")
  style.async = false
  style.load styleFile
  
  if (source.parseError.errorCode <> 0) then
    result = reportParseError(source.parseError)
  elseif (style.parseError.errorCode <> 0) then
    result = reportParseError(style.parseError)
  else
    on error resume next
    result = source.transformNode(style)
    if (err.number<>0) then
      result = reportRuntimeError(exception)
    end if
  end if
  TransformDocument = result
End Function

If an error occurs, we call the reportParseError or reportRunTimeError functions. Since these are fairly simple functions that just output the error, we do not cover them here (you can view them by downloading the sample files).

Before running off and deploying this, if you have a heavily trafficked site, you should be aware of the following performance consideration: This solution does all the transformations immediately upon request. Since the document's and transformations do not dynamically change, a better approach is to build a document management system that performs and caches all the transformations on your server. This avoids the cost of having to load, parse, and transform the document on every request.

In our next article in this series, we are going to extend our demonstration with additional interactivy. You will see how to provide an article summary and the detailed article from the same URL and same XML document by applying different transformations.

Discuss and Rate this Article

Page 1:Building Smart Pages with ASP, XML and XSL
Page 2:The ASP Script
Page 3:Applying XSL on the Server