Wednesday, May 26, 2010

XSL Transform of a Remote XML feed via Javascript - Requires VB.NET

I wanted to use Javascript to display an RSS feed from a remote web server via a client side XSL transform. Javascript has security levels to prevent 'cross browser' scripting attacks, so it won't let you pull in a remote feed. But, I wanted to do it anyway. Call me crazy.



My solution was to get VB.NET to pull in the feed and echo it from my web server, basically acting as a Proxy, and fooling Javascript into thinking the feed is safe.



I created a .ASPX page to pull in the feed, turn it to a string, and output the feed for Javascript to use.




Stupid Firefox wouldn't load an XML transform from a .ASPX page like IE will, so I had I couldn't stream the file dynamically. Instead, I had to write the script to save the stream to a .XML file.



This script needs to write to an .XML file on your server ( called blogger.xml in this example ). You'll need to assign write permission to that file.




<%@Page Language="VB" %>

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>







Third, I created an .xsl stylesheet for the feed and saved it to my server as a .xsl file. The stylesheet I created is for a Blogger Atom Feed:



xmlns='http://www.w3.org/2005/Atom'
xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'
xmlns:georss='http://www.georss.org/georss'
xmlns:thr='http://purl.org/syndication/thread/1.0'
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">












( comments)









Then I used Javascript to do a XSL transform of the feed: NOTE: I spent a good deal of grief on this until I discovered my script wouldn't work for me in a .js file. I put it in the HEAD of my page and it worked fine.






<head onload="outputFeeds()">

1 comment:

Fulltime ASP net developers said...

Awesome! I enjoyed this post1 Thanks