To read the article online, visit http://www.4GuysFromRolla.com/webtech/060700-2.4.shtml

Displaying XML Data from other Web Sources as HTML using XSL, Part 4

By Brandon Monahan


  • Read Part 1
  • Read Part 2
  • Read Part 3

  • 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):

    <?xml version='1.0'?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
    <xsl:template match="/">
    
    <xsl:for-each select="newsfeed/channel">
      <table>
        <tr>
          <td colspan="2">
            <font face="arial" size="1">
            <b>
            <xsl:value-of select="@title"/>
            </b>
            </font>
          </td>
        </tr>
    
      <xsl:for-each select="headline">
        <tr>
           <td valign="top">
             -
           </td>				
           <td>
             <font face="arial" size="1">
             <A>        
             <xsl:attribute name="href">
             <xsl:value-of select="@href"/>
             </xsl:attribute>
             <xsl:value-of />
             </A>
             </font>
           </td>
        </tr>
      </xsl:for-each>
      </table>
    </xsl:for-each>
    
    </xsl:template>
    </xsl:stylesheet>
    

    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.

    <%
      set xml = Server.CreateObject("Microsoft.XMLDOM")
      xml.load("http://headlines.isyndicate.com/pages/cui/news.xml")
    %>
    

    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:

    <%
      set xsl = Server.CreateObject("Microsoft.XMLDOM")
      xsl.load(Server.MapPath("home_News.xsl"))
    %>
    

    Now we are almost finished. All we have to do is write the parsed XML to the page and clean up like such:

    <%
      Response.Write(xml.transformNode(xsl))
      xsl = nothing
      xml = nothing
    %>
    

    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.

    Debugging
    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.

    Final Product
    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: bwm4@yahoo.com. If you found this article interesting be sure to check out the related articles listed below...

    Brandon Monahan


    Attachments:

  • Download home_News.xsl
  • Download the source to home_News.asp in text format (this ASP page utlizes home_News.xsl to display the syndicated news)
  • View the live demo!

    Related Articles

  • Getting Started with XML
  • XML Links and Information


  • Article Information
    Article Title: Displaying XML Data from other Web Sources as HTML using XSL, Part 4
    Article Author: Brandon Monahan
    Published Date: Wednesday, June 07, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/060700-2.4.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers