Displaying XML Data from other Web Sources as HTML using XSL, Part 4By Brandon Monahan
In Part 3 we looked at some of the more advanced abilities of XSL, such as how to iterate through XML nodes and how to display hyperlinks. In this part we'll look at one possible way to use XSL to display the XML generated by iSyndicate.com!
Now we have got the ability to loop through our XML, we can display all kinds of data and make hyperlinks. What more do you need to know? Here is the finished XSL document using all of the information above (by the way, the header, stylesheet, and template tags are all necessary for any XSL document transferring the HTML):
This code will simply loop through the channels displaying the channel name, then inside loop through the headlines in that channel giving the headline name and a link to the news. To see this XSL stylesheet in action be sure to view the live demo!
How do I Make it All Work?
First, take the above XSL code and save it to your server. I saved it as
home_News.xsl. Now that
you have completed that, let's go back to the ASP.
In the above code we had initiated the XML DOM object and loaded the external XML document. Now let's instantiate the XSL DOM object and load our XSL document like such:
Now we are almost finished. All we have to do is write the parsed XML to the page and clean up like such:
That's that. Your XML should be written to the page as a beautifully formatted block of news stories (html). More likely than not, you will get a blank screen. Here is how to fix that.
XML and XSL are really easy to debug. Just grab the file and drag it to your IE 5 browser. If you made an error, it will tell you what you did wrong and where. Make the correction and refresh. Keep doing that until it display your XSL and XML documents correctly. After that, give your ASP page a try again. Hopefully it will work this time.
There are a couple of problems you may run into like not having the proper settings on the server to let your program load a document from another site. If you can't make it run on your server but you are sure you have the code correct give it a try on PWS (PWS doesn't have the features of such complicated security and should run the page) or save the XML file to a local disk and give it a try.
You can view almost this exact code being used in production at www.citiesunited.com or for just the page of HTML that is included on the home page go to www.citiesunited.com/home_News.asp.
Good Luck and Happy Programming! If you have any questions, problems, or what-not, please do feel free to drop me a line at: firstname.lastname@example.org. If you found this article interesting be sure to check out the related articles listed below...
home_News.aspin text format (this ASP page utlizes
home_News.xslto display the syndicated news)