Introduction
Once I started interest in XML/XSL I found that it is impossible to access ASP objects such as Request inside XSL templates. I tried passing them via XSLTProcessor.addObject method, it worked for Session, but couldn't get it working with Request. Yes, I know you can always pre-define all request variables inside XSL template with xsl:param and initialize them from ASP code, but say that you don't know exactly what you will be looking for, or there are too many... Not the best approach I guess. My first thought was to append all variables to XML data file as nodes, but this can slow down your web application. Then I came up with this small class that allows you to get any variable when you actually need it.
The idea
Well, the trick is that even XSL cannot access ASP objects, ASP itself sure can. So I'll just create a simple VBScript class with functions which allows retrieving Request object variables and setting/retrieving Session variables as well. One you've got the idea you can extend it as you want.
Class ASPObjects
Public Function GetRequestVariable(Key, QType)
Select Case lcase(QType)
Case "querystring"
GetRequestVariable = CStr(Request.QueryString(Key).Item)
Case "form"
GetRequestVariable = CStr(Request.Form(Key).Item)
Case "server"
GetRequestVariable = CStr(Request.ServerVariables(Key).Item)
Case Else
GetRequestVariable = CStr(Request(Key).Item)
End Select
End Function
Public Function GetSessionVariable(Key)
GetSessionVariable = Session(Key)
End Function
Public Function SetSessionVariable(Key, Value)
Session(Key) = Value
SetSessionVariable = ""
End Function
End Class
How to use
Now I'll talk how to use the class above in your XSL/XSLT. After creating XSLProcessor in your ASP script, just create an instance of ASPObjects class and add it using method addObject. Remember to create a namespace in your XSL template (I'm using xasp below).
...
set xslProc = xslt.CreateProcessor()
set xasp = new ASPObjects call xslProc.addObject(xasp, "urn:asp-objects")
call xslProc.Transform()
TransformXML = xslProc.output
Set xasp = Nothing ...
Now when we have passed our object to XSL template, let's see how it can be used. First of all add another namespace (urn:asp-objects here) to your xsl:stylesheet, then you can access the functions of our object, like in the example below:
<!---->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xasp="urn:asp-objects">
<!---->
<xsl:template match="/">
<!---->
<xsl:value-of
select="xasp:GetRequestVariable('hello','querystring')" />
<!---->
<xsl:value-of
select="xasp:SetSessionVariable('username','Me!')" />
</xsl:template>
...
</xsl:stylesheet>
That's all actually. I told you it wasn't that hard :). Now you can extend the class so it will work with Response object as well or will be able to get cookies along with other Request parameters or access Server object methods and properties.