• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1771
  • Last Modified:

How to use VBscript in xsl?

Hi, as you will understand I'm a beginner in XML, XSL.

What I'm trying to do is:
When a page is called a randomly selected content should be showed. It's just a part of the page that will act this way. We now use asp, xml, xsl to our content manager.

So what I need is to know how I either can include an Asp file in the xsl file in the part where the result from the randomizing takes place. Or how to include a VBscript in the xsl.

From the beginning I tried to include an Asp file (that does the randomizing). But someone told me that's not possible, so than I found a solution for including VBscript in the Xsl.

In the xsl i need to include one VBscript function to randomize the outcome of three different files.

This is how I have tried it now. It doesn't work! :-(

.xsl
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl" language="VBScript">
   <xsl:script><![CDATA[
function somefunction()
   end function
]]></xsl:script>

.
.
. <!-- Here is the place for the randomized content-->
<td width="192" valign="top">
  <xsl:apply-templates select="/page/block [@id='textfooter2']"/>
  <xsl:eval>somefunction</xsl:eval>
</td>

Does anyone have a suggestion how to solve this, or where I could possible find a solution.

As always the deadline was yesterday, so I probably spend the evening and night to try to work this out.

In advance THANK YOU ALL that can help me out!

/Anna
0
AnnaCfl
Asked:
AnnaCfl
1 Solution
 
topcat_ukCommented:
Hi Anna,

There are several ways to do this.

You could use a vbscript function to randomly select which text is to be displayed or you could call an external asp script.

I prefer to call the external script as this can then be reused across many stylesheets.

If the random content you wish to display is in another xml file, you can call an external asp script which randomly selects the data and returns it as a nodelist.

I assume you confident in writing a randomise function and know how to return a nodelist.

Here is an example using the same process to access asp session vars.

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 retreiving Request object variables and setting/retreiving 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
call xslProc.addObject(xasp, "urn:asp-objects")     'adding it to XSL template

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>
0
 
sybeCommented:
I use the following to include VBScript functions in XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-namespace" version="1.0">
<msxsl:script language="VBScript" implements-prefix="user"><![CDATA[
     Function RepeatString(ByVal i, ByVal s)
         RepeatString = REPLACE(SPACE(Cint(i)), " ", s)
     End Function
]]> </msxsl:script>

<xsl:template match="/">


(..some xsl code...)
   <xsl:variable name="level" select="number(level)"/>
   <xsl:value-of select="user:RepeatString($level,'-&#160;')"/>




</xsl:template>
</xsl:stylesheet>
0
 
AnnaCflAuthor Commented:
To sybe.
Thank you for a very easy to use example. To bad for me I don't get it to work. I probably use wrong syntax when I call the function in my xsl.

This is how I've done it.
<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-namespace" version="1.0">
<xsl:output indent="yes" method="html" version="4.0" encoding="ISO-8859-1" doctype-public="-//W3C//DTD HTML 4.0 Transitional//EN"/>

<msxsl:script language="VBScript" implements-prefix="user"><![CDATA[
   
Function Randomize()
        document.write("Test!")
End Function
]]> </msxsl:script>
.
. some xsl
.
<td width="192" valign="top">
  <xsl:apply-templates    select="/page/block@id='textfooter2']"/>
  <xsl:value-of select="user:Randomize()"/>
</td>

Is it the correct syntax to call the function - Should this work? I don't get any errors when I exclude the function call.

I would really preciate an answer to this question as well.

/Anna

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
sybeCommented:
your function:

==================
Function Randomize()
       document.write("Test!")
End Function
==================

should be:

====================
Function Randomize()
       Randomize = "Test!"
End Function
===================


0
 
AnnaCflAuthor Commented:
Thank you!!!
You saved my weekend.

/Anna
0
 
AnnaCflAuthor Commented:
Why isn't my VBscript working in the xsl?
This is the function I'm trying to get working.

Function Randomize()
randomize()
randomNumber=Int(3 * rnd())
if randomNumber="0" then
   document.write("Some text")

elseif randomNumber="1" then
   document.write("Some text")
elseif randomNumber="2" then
    document.write("Some text")
end If    
     
End Function

It's working outside of the xsl but not when it's called in the xsl, how come?

I would be glad if this worked. But I would be even happier if I had a function that callad a text file instead of writing the text in the script. Is this possible when using VBscript in XSl?
0
 
syd108Commented:
Have you solved this problem or do u need help?
syd
0
 
AnnaCflAuthor Commented:
Yes, it´s working now.
I´ve given you your points as you can see.

Thanks again, hava a nice day!

/Anna
0
 
Round_ButteCommented:
Can I use the Messagebox function in vbscript in XSL?  to trap the user input?  how is it done? John
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now