Welcome to MSDN Blogs Sign in | Join | Help

Generating VBScript to read a blog

Sometimes I need to test something using VBScript. A user sends a code snippet and asks why it behaves a certain way. Examining the behavior in VB can help.

 

Below is a VFP code sample which demonstrates VBScript code which uses several objects to read the RSS feed from my blog, do an XSLT transform on it so it displays nicely, and shows it in IE. (See Use a simple XSLT to read the RSS feed from a blog.)

First an XSLT file is created using Text…EndText. This is like a template to transform the XML to HTML. (Since it’s in XML format, literal characters such as “<” or “>” (which are used quite often in HTML!) are substituted with “&lt;” and “&gt;”)

Then it creates a script file called c:\t.vbs which contains the VB Script. The WinHTTPRequest object is used to get the XML RSS feed. The MSXML TransformNode method is used to do the transform. The Scripting.FileSystemObject is used to do File I/O.

Then VFP invokes Windows Scripting Host to run the script using wscript.exe

 

To convert the “&amp;” back to “&”, a VFP COM object is used. Is there an easy way in VB Script to do the VFP STRTRAN function?

 

Some VB Script gotchas:

  • IF requires a THEN on the same line
  • ENDIF needs a space “END IF”
  • If a call returns a value that is an object, the SET command is required
  • Method calls don’t require parentheses for parameters unless the return value is assigned to a value.
  • VFP allows 3 types of string delimiters (single, double quote, and square brackets), allowing easy embedding of quotes. VB only has the single quote.
  • Doing a STRTRAN isn’t trivial in VB
  • STRTOFILE and FILETOSTR are trivial in VFP
  • Generating a text file using TEXTMERGE is simple in VFP

 

 

 

 

cXSLTFile="c:\t.xslt"

cHTMFile="c:\t.htm"

cScriptFile="c:\t.vbs"

cUrl="http://blogs.msdn.com/calvin_hsia/Rss.aspx"

 

TEXT TO cXSLT noshow

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="html"/>

<xsl:template match="/">

<HTML><body>

<title><xsl:value-of select="//rss/channel/title"/></title>

<xsl:for-each select="//rss/channel/item">

<font color="#ff00ff" size="5">

&lt;a href="<xsl:value-of select="link"/>"&gt;<xsl:value-of select="title"/>&lt;/a&gt;

</font>

<p><xsl:value-of select="pubDate"/></p>

<p><xsl:value-of select="description"/></p>

<br/>

</xsl:for-each>

</body></HTML>

</xsl:template>

</xsl:stylesheet>

ENDTEXT

STRTOFILE(cXSLT,cXSLTFile)    && write out the XSLT

 

IF .f. && the VFP way. Try this if you’re having problems

      oHTTP=CREATEOBJECT("winhttp.winhttprequest.5")

      oHTTP.Open("GET",cUrl,.f.)

      oHTTP.Send()

      oXML=CREATEOBJECT("msxml.domdocument")

      oXML.loadXML(oHTTP.ResponseText)

      oXSLT=CREATEOBJECT("msxml.domdocument")

      oXSLT.load(cXSLTFile)

      cTrans=oxml.transformNode(oxslt)    && do the XSLT transform

      cTrans=STRTRAN(cTrans,"&amp;","&")  && convert "&amp;" to "&"

      cTrans=STRTRAN(cTrans,"&gt;",">")

      cTrans=STRTRAN(cTrans,"&lt;","<")

      STRTOFILE(cTrans,cHTMFile)

      oie=CREATEOBJECT("internetexplorer.application")

      oie.visible=1

      oie.navigate(cHTMFILE)

      RETURN

ENDIF

 

 

TEXT TO myvar TEXTMERGE

      SET oHTTP = CREATEOBJECT("winhttp.winhttprequest.5.1")

      oHTTP.Open "GET","<<cURL>>", 0

      oHTTP.Send

 

      SET oXML=CREATEOBJECT("msxml.domdocument")

      oXML.loadXML(oHTTP.ResponseText)

      SET oXSLT=CREATEOBJECT("msxml.domdocument")

      oXSLT.load("<<cXSLTFile>>")

      cTrans=oXML.TransformNode(oXSLT)

 

      SET fs=CREATEOBJECT("scripting.filesystemobject")

      IF fs.FileExists("<<cHTMFile>>") then

            fs.DeleteFile("<<cHTMFile>>")

      END if

      SET h=fs.CreateTextFile("<<cHTMFile>>",0,1)

      SET oVFP=CREATEOBJECT("t1.c1")      ' use VFP to convert "&amp;" to "&"

      cTrans = oVFP.MyEval("strtran(p2,p3,p4)",cTrans,"&amp;","&")

      cTrans = oVFP.MyEval("strtran(p2,p3,p4)",cTrans,"&gt;",">")

      cTrans = oVFP.MyEval("strtran(p2,p3,p4)",cTrans,"&lt;","<")

      h.Write(cTrans)

      h.Close

     

      SET oIE=CREATEOBJECT("internetexplorer.application")

      oIE.Visible=1

      oIE.Navigate("<<cHTMFile>>")

ENDTEXT

 

STRTOFILE(myvar,cScriptFile)

PUBLIC x as wscript.shell

x=CREATEOBJECT("wscript.shell")     && WSH

x.Exec("wscript "+cScriptFile)

 

 

84122

Published Friday, August 05, 2005 11:21 AM by Calvin_Hsia
Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Generating VBScript to read a blog

Friday, August 05, 2005 3:25 PM by Craig Boyd
Calvin,

>>Is there an easy way in VB Script to do the VFP STRTRAN function?

You can use VB's Replace() function. An example of its use can be found at: http://www.example-code.com/vb/stringReplace.asp

As an aside, you can also run VBScript directly in Visual FoxPro using the MS Script control. I wrote a FAQ on this you can look at here: http://www.tek-tips.com/faqs.cfm?fid=4257

# re: Generating VBScript to read a blog

Sunday, August 07, 2005 1:29 AM by Calvin_Hsia
Thanks Craig for the Replace hint and the pointer to the excellent sample of the MS Script control!

# re: Generating VBScript to read a blog

Thursday, August 11, 2005 3:06 PM by Lou Harris
Note that you would need to convert the amphersands after the &lt; and &gt; characters to avoid converting a deliberate &amp;lt; to &lt;

Like so:
<pre>
cTrans = Replace(cTrans,"&gt;",">",1,-1)
cTrans = Replace(cTrans,"&lt;","<",1,-1)
cTrans = Replace(cTrans,"&amp;","&",1,-1)
</pre>

-Lou.

# Webcrawl a blog to retrieve all entries locally: RSS on steroids

Thursday, May 25, 2006 8:32 PM by Calvin Hsia's WebLog
Today’s sample shows how to create a web crawler in the background. This crawler starts with a web page,...

# Handling arbitrary strings in URLs: Escape, InternetCanonicalizeUrl, WinHttpCrackUrl and URI.EscapeUriString

Friday, July 07, 2006 4:40 PM by Calvin Hsia's WebLog
Sometimes a web application might want to put arbitrary strings into a URL, and make it a valid URL....

# Use new XML Features of VB to generate dynamic scripts and text files - Noticias externas

# re: Generating VBScript to read a blog

Monday, October 08, 2007 7:02 PM by Remi

Calvin:

I am trying to pass a variable from my VFP form to xsl, e.g.

  m.pos=5

I want to use m.pos to limit my node search and transformation. Is there a way to use VFP code in XSL to do this kind of function? Thanks.

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker