?
Solved

How to use VBscript in xsl?

Posted on 2003-03-06
9
Medium Priority
?
1,680 Views
Last Modified: 2012-06-27
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
Comment
Question by:AnnaCfl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 1

Expert Comment

by:topcat_uk
ID: 8087031
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
 
LVL 28

Expert Comment

by:sybe
ID: 8087473
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
 

Author Comment

by:AnnaCfl
ID: 8087680
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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 28

Accepted Solution

by:
sybe earned 400 total points
ID: 8087783
your function:

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

should be:

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


0
 

Author Comment

by:AnnaCfl
ID: 8087826
Thank you!!!
You saved my weekend.

/Anna
0
 

Author Comment

by:AnnaCfl
ID: 8101680
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
 
LVL 2

Expert Comment

by:syd108
ID: 8382131
Have you solved this problem or do u need help?
syd
0
 

Author Comment

by:AnnaCfl
ID: 8386313
Yes, it´s working now.
I´ve given you your points as you can see.

Thanks again, hava a nice day!

/Anna
0
 

Expert Comment

by:Round_Butte
ID: 26512275
Can I use the Messagebox function in vbscript in XSL?  to trap the user input?  how is it done? John
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question