Tuesday, February 07, 2012
 
Subscribe to the pheedbaq YouTube channel Become a fan of pheedbaq on Facebook Follow pheedbaq on Twitter
 
Blog
  Login     Register
Apr 26

Written by: pheedbaq
4/26/2010 11:06 AM 

After nearly 3 years of having this blog, and all the times I said I would put up some tutorials, I guess it's about time I actually did that :P.  I intend to make my tutorials short and sweet, well, because I'd rather be keeping up with my YouTube channel or adding useful links to this site than doing grammar checks on blog posts, :D programming experience is assumed...

Anyway, XSLT, or eXtensible Stylesheet Language Transformations, is handy for converting any XML-compliant documents into other XML documents.  The perfect example is on this site's front page; you see I have links from GameTrailers, Joystiq, DigitalFoundry, and Gamasutra, all in their own little sections.  Well, you don't really think I copy-paste those links to my site every few minutes, do you?  No, each of those sites provides an RSS feed (Really Simple Syndication) for public use.  RSS is a subset of XML, and as such can be modified by XSL, because an RSS document/feed is an XML document.  Sooooooo, what I'm doing is slurping up the RSS from their site, and using XSL to reformat the data in those feeds for use on my site so that it conforms to my site's look and feel.  Voila, valuable content for my site's users, and that content will now update itself!

So, let's get down to it.  Go to http://www.eurogamer.net/rss/eurogamer_digitalfoundry_feed.rss to see what the RSS feed and its XML look like (view source if necessary).  Below is the bit of code I use to consume the RSS feed from DigitalFoundry and reformat it for my site.  This code is contained in a file that ends with an .xsl extension.  The XSL processor takes the rss feed found at that URL, along with my XSL file, and spits out the reformatted result.  Here's the code:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="rss">
    <ul style="line-height: 90%">
    <xsl:for-each select="channel/item[position()&lt;=5]">
        <li>
        <a target="_blank" style="text-decoration:none">
        <xsl:attribute name="href">
            <xsl:value-of select="link" />
        </xsl:attribute>
        <xsl:value-of select="title"/>
        </a>
        </li>
    </xsl:for-each>
    </ul>
</xsl:template>
</xsl:stylesheet>

 

  • The first two lines ("xml:version" and "xsl:stylesheet version") are required... 'nuff said.  Just always include them :P
  • The third line ("xsl:output method") tells whatever is processing the XSL (internet explorer, firefox, my blog site, whatever ends up handling the .xsl file) that the output should be considered html and should be indented so it's nice n' purdy.
  • "xsl:template match" tells the XSL processor which tag denotes the beginning of the section to work on.  I could have used " match = '/' " instead to denote the entire document.
  • "ul" is defining an HTML tag that I want to use in reformatting the feed.  I won't be going over any more of these HTML tags, as they should be familiar to anyone who's done any amount of work with web pages.
  • "xsl:for-each select..." is telling the XSL processor that for each "channel" and its' subsequent "item" tags, perform these operations.  The "[position()&lt;=5]" could have been left out if I had wanted to process all item tags, but I just want the top 5, which are positions 1 through 5.  Sadly, XSL uses 1-based arrays instead of 0-based arrays... must have been a VB or Basic fan.  Anyway, here we're just testing the current "item" tag to see if its position within the "channel" tag is less than or equal to 5.  The "&lt;" is the HTML code for the < symbol, and the "=" is... please tell me you can guess.  If not, it's time to go back to some intro programming tutorials!  So the full clause is "for each channel and item whose position is less than or equal to 5, do this stuff."
  • "xsl:attribute name='href'" is telling the XSL processor that I want to add an attribute to the preceeding tag.  So I'm adding an href to that anchor tag.  I have to do this because the text for the href link ends up goofing up the XSL processor, so the href attribute needs its own XSL definition.
  • "xsl:value-of select='title'" is pulling out the contents of the "title" tag that resides within the current "item" of the RSS feed, and placing it here, which is as the value of the HTML anchor.
  • The remaining tags are just closing up the related tags above.

 And that's that.

Fin.

Recent Entries Minimize
del.icio.us is pretty awesome.
Other blogs/sites for your consideration.
Migrating old blog posts.
Welcome to the new pheedbaq.com!
My new favorite game-tech site... DigitalFoundry
Comments Minimize
re:
I really appreciate your comments. You're so intelligent and very creative in posting comments. Keep it up searching new things in life.Learning is fun, you gain more knowledge as what others did.have a great day!.<br /><br />Kim<br />www.seapyramid.net<br />Mega Search
re:
I really appreciate your comments. You're so intelligent and very creative in posting comments. Keep it up searching new things in life.Learning is fun, you gain more knowledge as what others did.have a great day!.<br /><br />Kim<br />www.seapyramid.net<br />Mega Search
Re: E3 2007 Portal Developer Walkthrough
I'm impressed. You're truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. I'm saving this for future use.<br />www.profitbysearch.com | PHP Zend Framework Development India
re:
This post has been extremely insightful & useful to increase my knowledge in the field of knowledge & its many facets. Well, I'm so happy that I have found this post because I have been seeking some information about it. Here’s a good resource that is also worth a look. <br /><br />Cholo<br />Technology Guide <br />www.bailerbin.com
re:
This post has been extremely insightful & useful to increase my knowledge in the field of knowledge & its many facets. Well, I'm so happy that I have found this post because I have been seeking some information about it. Here’s a good resource that is also worth a look. <br /><br />Cholo<br />Technology Guide <br />www.bailerbin.com
re:
This post has been extremely insightful & useful to increase my knowledge in the field of knowledge & its many facets. Well, I'm so happy that I have found this post because I have been seeking some information about it. Here’s a good resource that is also worth a look. <br /><br />Beth<br />Technology Guide <br />www.bailerbin.com
re:
I'm impressed. You're truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. I'm saving this for future use.<br /><br />Claire<br />Marks Web<br />www.imarksweb.net
re:
Just wanted to give you a shout from the valley of the sun, that you have a great piece of details, these instructions are very clear and helpful. Thanks for sharing this awesome label. Can’t wait to see what else you have. <br /><br />Joseph<br />Readers Guide<br />www.vssm.org
re:
Just wanted to give you a shout from the valley of the sun, that you have a great piece of details, these instructions are very clear and helpful. Thanks for sharing this awesome label. Can’t wait to see what else you have. <br /><br />Joseph<br />Readers Guide<br />www.vssm.org
re:
I'm impressed. You're truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. I'm saving this for future use.<br /><br />Vivian<br />Marks Web<br />www.imarksweb.net
Home | Blog | Hi-Def Gaming | Retro Gaming | Programming | Game Development | Offsite Content | Del.icio.us Links | Registration
Copyright 2009 by pheedbaq.com