Parsing data from an external source using XSLT or .Net User Control? Options
allison
Posted: Tuesday, August 05, 2008 8:42:42 PM

Rank: Newbie

Joined: 6/25/2008
Posts: 12
Location: California, United States
Hi,
I'm new to Umbraco and the whole .Net and XSLT community. I have been working on converting existing ASP and ASP.NET pages into Umbraco pages. I need a little help with how Umbraco deals with ASP and external sources.

I would like to have my page display stock information from yahoo finance. Currently I've been using ASP to get information back from yahoo and then I'm parsing and formatting that information. Here's the ASP code I'm using to get the information back from yahoo

Code:


<%
Function GetHTML(strURL)
     Dim objXMLHTTP, strReturn
     Set objXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")
     objXMLHTTP.Open "GET", strURL, False
     objXMLHTTP.Send
     strReturn = objXMLHTTP.responseText
     Set objXMLHTTP = Nothing
     GetHTML = strReturn
End Function

response.write(getHTML("http://finance.yahoo.com/d/quotes.txt?s=SYMC&f=sl1d1t1c1ohgv"))

%>



**Please note that this ASP is just getting the information from the page and writing it out. I would like to actually format the information into a table but that seems secondary to actually getting the data at this point

My problem is that I cannot just embed this in an Umbraco page (whether in the pages Content or Template). I would like to be able to continue to get and display this information on my page, but I'm not sure of the best method to do this in Umbraco. Is a User Control the best method or can this be easily done in XSLT? Has anyone done anything similar to this or can someone suggest a solution?

This seems like is should be a common problem, but I have not been able to find anything similar in the forum. I would greatly appreciate any help or suggestions!

Thanks!
Allison

Tutto è bene quello che finisce bene
Dirk
Posted: Tuesday, August 05, 2008 8:55:21 PM

Rank: Umbracoholic

Joined: 9/27/2007
Posts: 1,136
Location: Belgium
Hi allison,

Welcome to umbraco.

Both options you're talking about are viable solutions. Seems like the url you posted returns some sort of comma separated list of values which can be processed either:

- by writing your own custom control using some c#/vb.net code, or
- through the use of xslt (I guess the umbraco xslt extension library contains some functions which can process comma separated list).

I'd go for the latter solution, as it would only require some simple c#/vb.net code, which you can easily put inline in your xslt and as it would be lots of overhead when creating a custom control (and the dll...deployment,...). A first function could return the string from the url requested, a second function would serve to split up the strings to get the values you need. Formatting shouldn't be a problem here.

Hope that helps a bit.

Regards,
/Dirk


level 1 & 2 certified - umbraco MVP 2008/2009 - umbraco blog at netaddicts.be - working on an integrated forum4umbraco
Dirk
Posted: Tuesday, August 05, 2008 9:01:25 PM

Rank: Umbracoholic

Joined: 9/27/2007
Posts: 1,136
Location: Belgium
Just adding some thoughts here:

Is yahoo not capable of returning xml strings. It would greatly simplify the solutions as a xslt extension is already available to grab the xml from the remote source

umbraco.library:GetXmlDocumentByUrl('http://yahoo...'). A quick search for GetXmlDocumentByUrl will certainly get you started

Regards,
/Dirk



level 1 & 2 certified - umbraco MVP 2008/2009 - umbraco blog at netaddicts.be - working on an integrated forum4umbraco
allison
Posted: Wednesday, August 06, 2008 2:23:29 AM

Rank: Newbie

Joined: 6/25/2008
Posts: 12
Location: California, United States
Dirk,

I'm also leaning toward using XSLT, but I'm very new to it. I've been searching, but most of the documentation is on User Controls...can you suggest some good XSLT with inilne function examples or demos?

Thanks for your help :)

Allison

Tutto è bene quello che finisce bene
Dirk
Posted: Wednesday, August 06, 2008 8:24:24 AM

Rank: Umbracoholic

Joined: 9/27/2007
Posts: 1,136
Location: Belgium
Hi Allison,

Maybe this post could get you started. Oh, btw, no need to be an xslt guru. Plenty of examples available to steal with the eyes.

Good luck,
/Dirk


level 1 & 2 certified - umbraco MVP 2008/2009 - umbraco blog at netaddicts.be - working on an integrated forum4umbraco
desmo
Posted: Wednesday, August 06, 2008 11:19:26 AM

Rank: Aficionado

Joined: 7/19/2006
Posts: 174
Location: NYC
If you're interested, there's an Umbraco book on this here.

- Mark
allison
Posted: Wednesday, August 06, 2008 8:05:25 PM

Rank: Newbie

Joined: 6/25/2008
Posts: 12
Location: California, United States
Thanks for the links :)

I've double checked and Yahoo doesnt allow for XML - instead, by using their URL API system, the info you request is returned in CSV format. Is there an easy way to get the returned data (given by the URL) parse it using a delimiter and then format that information?

Thanks again for all your help!
Allison

Tutto è bene quello che finisce bene
drobar
Posted: Wednesday, August 06, 2008 9:48:43 PM

Rank: Umbracoholic

Joined: 9/8/2006
Posts: 1,831
Location: MA, USA
Not with xslt, at least... the GetXmlDocumentByUrl() expects the return to be in valid xml format. If it isn't... it will complain. Your best option would be to either use an AJAX request in javascript or write a .net control to get and parse the Yahoo feed.

There are a lot of fee-based quote feeds that are available in xml format and which allow you to display quote data on your public site with no/little restrictions. Most are very reasonably priced, being much less per year than you'd spend in your time to work with a CSV-only solution I would guess.

With an xml feed it would be an extremely simple matter to get the xml and display it any way you like with an xslt macro.

Let us know what you come up with.

cheers,
doug.

MVP 2007-2009 - Percipient Studios
allison
Posted: Thursday, August 07, 2008 3:00:28 AM

Rank: Newbie

Joined: 6/25/2008
Posts: 12
Location: California, United States
Doug,

Thanks for the clarification with XSLT.

I really would like to avoid paying any kind of fee which is why I originally went with Yahoo. I think I may have found a solution using XSLT and vbscript, but I'm still in the testing phases. If it works out, I'll definitely post it - otherwise I'm sure you'll see me asking some more questions in the very near future.

Thanks everyone for the help!
Allison

Tutto è bene quello che finisce bene
drobar
Posted: Thursday, August 07, 2008 3:22:33 AM

Rank: Umbracoholic

Joined: 9/8/2006
Posts: 1,831
Location: MA, USA
I've used these folks before and had good luck with them. They return xml.

http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=symc

Using this (or a similar) service, your xslt macro would be something like the following. I typed it right in the forum so I might have a typo, but you'll get the idea...

Code:

<xsl:template match="/">
    <xsl:variable name="stockUrl" select="'http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=symc'" />
    <xsl:variable name="stockData" select="umbraco.library:GetXmlDocumentByUrl($stockUrl)" />

    <h4>Raw XML</h4>
    <xsl:copy-of select="$stockData"/>

    <hr/>

    <xsl:for-each select="$stockData/StockQuotes/Stock/child::*">
        <xsl:value-of select="name()" />
        <xsl:value-of select="." />
        <br />
    </xsl:for-each>
</xsl:template>


cheers,
doug.

MVP 2007-2009 - Percipient Studios
allison
Posted: Thursday, August 07, 2008 11:37:47 PM

Rank: Newbie

Joined: 6/25/2008
Posts: 12
Location: California, United States
Ok,

First, thank you Doug for the suggestion - I tested your code and it works great.

Before you had posted that solution, I was working on a solution using XSLT and C#. For the purposes of learning, I was wondering if someone could help me figure out what I'm doing wrong. I keep getting errors when I try to save my file and I'm sure its something stupid that I've overlooked in my ignorance of XSLT.

Here's my XSLT code....

Code:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library"
    xmlns:cd="urn:schemas-connect-digital"
    exclude-result-prefixes="msxml cd umbraco.library">


<xsl:output method="xml" omit-xml-declaration="yes"/>

<xsl:param name="currentPage"/>

<xsl:template match="/">

<!-- start writing XSLT -->

<xsl:variable name="GetPrice" select="cd:GetStckPrice($pageString)" />
<xsl:variable name="GetChange" select="cd:GetStckChange($pageString)" />


            <div class="modHeadBlu">
                    <div class="modHeadTxtLt">Symantec Stock</div>
                    <div class="modHeadBluRt"><!-- displays right corner of header --></div>
            </div>
            <div class="modLevsWrap">

                    <div class="modContBluLev1">
                            <div class="modulePane" style="margin-left:5px">

<div id="symStockModule">
        <table width="100%" cellspacing="0" border="0" cellpadding="0"><tr>

<td  align="left"  colspan="2"><a href="http://finance.yahoo.com/q?s=SYMC&amp;d=e" target="_blank"><b>SYMC</b></a></td></tr>

<tr><td align="left"><strong>Last Trade</strong></td>

<td align="right"><xsl:value-of select="$stockPrice" /></td></tr>

<tr><td align="left">Day's Change</td>

<td align="right"><xsl:value-of select="$stockChange" /></td></tr>
        
        
</table>


    </div>


    
</div>




                            <div class="diviLtBlu7px"><!--x--></div>
                            <div class="floatRt"><span class="note">By Yahoo (Delayed 20 minutes)</span></div>
                    </div>
            </div>



</xsl:template>

<msxml:script language="CSharp" implements-prefix="cd">

<msxml:using namespace="System.IO" />
<msxml:using namespace="System.Collections" />
<msxml:using namespace="System.Web.Services" />
<msxml:using namespace="System.Net" />


public string[] GetQuote()
{
    string url;  //stores url of yahoo quote engine
    string buffer;
    string[] bufferList;
    WebRequest webRequest;
    WebResponse webResponse;
    
    url = "http://quote.yahoo.com/d/quotes.csv?s=SYMC&f=l1c1";
        
    webRequest = HttpWebRequest.Create(url);
    webResponse = webRequest.GetResponse();
    using( StreamReader sr = new StreamReader( webResponse.GetResponseStream() ) )
    {
        buffer = sr.ReadToEnd();
        sr.Close();
    }

    buffer = buffer.Replace( "\"", "" );
    bufferList = buffer.Split( new char[] {','} );
    return bufferList;
}

public string GetPrice()
{
    string price;    //price of stock
    string[] results;

    results = GetQuote();
    price = results[0];

    return price;
}

public string GetChange()
{

    string change;    //change in stock price
    string[] results;

    results = GetQuote();
    change = results[1];

    return change;

}


</msxml:script>



</xsl:stylesheet>



Thanks for everyone's help thus far!
Allison

Tutto è bene quello che finisce bene
drobar
Posted: Friday, August 08, 2008 4:09:56 AM

Rank: Umbracoholic

Joined: 9/8/2006
Posts: 1,831
Location: MA, USA
I usually put the code inside an msxml:script block inside CDATA markers.

cheers,
doug.

MVP 2007-2009 - Percipient Studios
mcculla10
Posted: Wednesday, August 13, 2008 7:39:59 AM
Rank: Enthusiast

Joined: 5/6/2008
Posts: 38
Location: Philippines
I think you need one more additional namespace in the header of your XSLT:

xmlns:msxsl="urn:schemas-microsoft-com:xslt"

I think that's required to run C# or Javascript inside of your XSLT.

Don't forget to exclude it:
exclude-result-prefixes="msxml cd umbraco.library msxsl">

"Nothing is Impossible"
-Umbraco

Boracay Packages, Makati, Manila Office Space
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.