Click here to Skip to main content
5,790,650 members and growing! (19,209 online)
Email Password   helpLost your password?
Web Development » ASP » XML/XSL     Intermediate

Accessing ASP objects from XSL

By Philip Patrick

Shows an easy way to access ASP built-in objects such as Request from XSL template.
.NET 1.0, NT4, Win2K, WinXP, Windows, .NET, ASP, Dev

Posted: 9 Jan 2003
Updated: 9 Jan 2003
Views: 52,118
Bookmarked: 25 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
14 votes for this Article.
Popularity: 4.13 Rating: 3.60 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
2 votes, 15.4%
3
3 votes, 23.1%
4
8 votes, 61.5%
5

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

    'returns Request object variables

    'QType here represents a collection 

    '   (QueryString, Form or Server(ServerVariables))

    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

    'returns Session object variables

    Public Function GetSessionVariable(Key)
        GetSessionVariable = Session(Key)
    End Function

    'sets Session object variable

    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).

'##### test.asp #####

'Loading XML and XSL somewhere

...
set xslProc = xslt.CreateProcessor()

set xasp = new ASPObjects 'creating our object

'adding it to XSL template

call xslProc.addObject(xasp, "urn:asp-objects")

call xslProc.Transform()
TransformXML = xslProc.output
Set xasp = Nothing 'and don't forget to free memory :)

...

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:

<!--test.xslt-->
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xasp="urn:asp-objects">
    <!--namespace added above-->

    <xsl:template match="/">
      <!--display value of Request.QueryString("hello")-->
      <xsl:value-of 
        select="xasp:GetRequestVariable('hello','querystring')" />

        <!--set value of Session("username")-->
        <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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Philip Patrick


I was born in small town Penza, Russia, in October 13th, 1975 yr. So my mother tongue is Russian. I finished the school there and learned in University, then I came to Israel and since then, I live there (or here *s*)
My profession is a C++ programmer under MS Windows platforms, but my hobby is Web development and ASP programming.

I started interesting in computers and programming somewere in 1990-1991 yrs., when my father brought home our first computer - Sinclair ZX Spectrum (he made it by himself). So I learned Basic and joined the Basic programmers club at my school (me and my friend were the only 2 guys from all school there, lol). After I finished the school (1992yr) I decided to continue my study at University and got specialization Operation Systems and Software Engineer. Although I still like my profession, but I always wanted something new, thus I learned HTML, Javascript and ASP which turned to be my hobby Smile
Occupation: Web Developer
Location: Israel Israel

Other popular ASP articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralHaving trouble declaring vars.membercryptoprogrammer10:34 7 Dec '05  
GeneralPut xml value to ASP function ?!sussJ.Kundu23:58 22 Sep '04  
GeneralRe: Put xml value to ASP function ?!sussJ.kundu0:54 23 Sep '04  
GeneralXSL and ASP ControlsmemberFxbrandon23:21 4 May '04  
Generaledit field in xslmembergok11:48 5 Mar '03  
GeneralRe: edit field in xslmemberPhilip Patrick21:52 5 Mar '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Jan 2003
Editor: Smitha Vijayan
Copyright 2003 by Philip Patrick
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project