How to Easily display Digg posts via XSLT using Digg Api

How to Easily display Digg posts via XSLT using Digg Api

0 27890

How to Easily display Digg posts via XSLT using Digg Api

As we know, Digg become very popular last time, so why not use it for our purposes? I decided to investigate Digg a little and learn how possible to draw Digg posts at my own website. And I make own unique method. Hope you will like it.
Reading I find necessary ways to get posts XML results. Just check story.getPopular method. Yes, this is usual to use different reparse methods in PHP, but lets learn something new like XSLT (Extensible Stylesheet Language Transformations).


download in package


Step 1. PHP

Ok, here are all used PHP files:


This file will generate list of Digg stories. As you can see – I already put several samples ($sMethod values) for different methods. So you can try any method. All code commented, so I hope it will easy to understand it.

// $sMethod = '';
$sMethod = '';
// $sMethod = '';
// $sMethod = '';
$sXmlSrc = getDiggStoriesXml($sMethod);
// Load the XML source
$xml = new DOMDocument;
// Load XSLT
$xsl = new DOMDocument;
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
echo '<link media="all" href="css/styles.css" type="text/css" rel="stylesheet">';
echo $proc->transformToXML($xml);
// this function get digg information using caching (not more 1 times per minute)
function getDiggStoriesXml($sUrl = '') {
    // used cache folder
    $sCacheFolder = 'cache/';
    // formatting cache name
    $sFilename = date("YmdHi").'.xml';
    if (! file_exists($sCacheFolder.$sFilename)) {
        //grab the XML and save it to a temp XML file
        $ch = curl_init($sUrl);
        $fp = fopen($sCacheFolder.$sFilename, "w");
        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20080623 Firefox/") ); // makes our request look like it was made by Firefox
    return file_get_contents($sCacheFolder.$sFilename);

Step 2. CSS

Here are used CSS styles.


.news-summary, .news-full, .news-full-confirm {
background:none repeat scroll 0 0 #F3F3F3;
border:1px solid #CCCCCC;
.news-body {
padding-left: 60px;
.news-body a.body {
text-decoration: none;
padding: 4px;
margin: -4px 0 -4px -4px;
color: #555;
position: relative;
z-index: 2;
.news-summary .news-body em {
color: #999;
white-space: nowrap;
.news-body em a {
color: #777;
text-decoration: none;
border-bottom: 1px solid #ddd;
.news-body em a:hover {
color: #000;
.news-digg {
position: absolute;
top: 0.8em;
left: 0;
text-align: center;
font-size: 85%;
margin: 0 0 0 3px;
padding: 0;
list-style: none;
background: url(../img/shade-news.gif) no-repeat;
.digg-count a, .digg-count span  {
display: block;
padding: 10px 0 4px 0;
text-decoration: none;
width: 50px;
min-height: 40px;
color: #93883F;
text-align: center;
.news-summary .digg-count strong {
font-size: 160%;
font-weight: normal;
letter-spacing: -1px;
line-height: 1;
display: block;
color: #736926;
.digg-count img {
position: absolute;
top: 0;
left: 0;
border: none;
.digg-count a:hover, .digg-count a:hover strong {
color: #998D43;
.news-body h3 {
margin: 2px 0 0 0;
font-size: 115%;
letter-spacing: -0.01em;
font-weight: bold;
line-height: 1.1;
.news-body p {
margin: 0.2em 0 0.1em 0;
line-height: 1.3;
.news-body em {
font-style: normal;
font-size: 85%;
color: #666;
} {
display: block;
width: 80px;
height: 80px;
background: #ddecee url(../img/v-default.gif);
border: 1px solid #a5c2e3;
position: absolute;
left: 60px;
top: 8px;
.img-summary {
background: #ddecee url(../img/i-default.gif);
} em {
display: block;
width: 80px;
height: 80px;
text-indent: -2000em;
background: url(../img/v-frame-simple.png);
.img-summary em {
background: url(../img/i-frame-simple.png);
.v .news-body {
.comments {
background:transparent url(../img/i-cmts.gif) no-repeat scroll 0 4px;
padding:4px 6px 4px 18px;

Step 3. XSLT

I hope this is most interesting part of my story, used XSLT rules:


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="">
    <!-- <xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/> -->
    <xsl:template match="stories">
                <xsl:for-each select="story">
                        <xsl:attribute name="class">
                                <xsl:when test="thumbnail">news-summary v img-summary</xsl:when>
                        <div class="news-body">
                                <a target="_blank">
                                    <xsl:attribute name="title"><xsl:value-of select="@link"/></xsl:attribute>
                                    <xsl:attribute name="href">
                                        <xsl:for-each select="shorturl">
                                            <xsl:value-of select="@short_url"/>
                                    <xsl:value-of select="title"/>
                                <xsl:for-each select="thumbnail">
                                    <span style="background-attachment:scroll;background-color:transparent;background-position:0 0;background-repeat:repeat;" class="news-img">
                                    <xsl:attribute name="style">background: transparent url(<xsl:choose>
                                            <xsl:when test="string-length(@src) > 0">
                                                <xsl:value-of select="@src" />
                                        </xsl:choose>) repeat scroll 0% 0%;</xsl:attribute>
                                <a class="body" style="color:#555;">
                                <xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute>
                                <xsl:value-of select="description"/>
                                <em>(Submitted by <a>
                                <xsl:for-each select="user">
                                    <xsl:attribute name="href">
                                            <xsl:when test="string-length(@name) > 0">
                                                <xsl:value-of select="@name" />
                                    <xsl:value-of select="@name" />
                                <div class="news-details">
                                        <span class="tool comments">
                                    <xsl:attribute name="title"><xsl:value-of select="title"/></xsl:attribute>
                                    <xsl:value-of select="@comments"/> Comments</span>
                        <ul class="news-digg">
                                <li class="digg-count">
                                <xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute>
                                <xsl:attribute name="title"><xsl:value-of select="title"/></xsl:attribute>
                                        <strong><xsl:value-of select="@diggs"/></strong> diggs</a>
                <xsl:comment>Copyright © AndrewP</xsl:comment>

Step 4. Images

Also we need several images for our project:


Step 5. Few important things

After reading I found ‘Be Polite, Please!’, so I decode not request Digg api any time when user call it, and I made way using cache system. I started caching results. I created ‘cache’ folder with permission to write (you should create ‘cache’ folder too).
After I put .htaccess file in it with next text:
Options -Indexes

And now check index.php:
$sFilename = date(“YmdHi”).’.xml’;
So I using current year-month-day-hour-minute digits to generate cache filename. Hope you understand my idea :)


Today I told you how to display digg posts at your own website via XSLT transformations. Hope all my code easy to understand and comfortable to use. You can use this material to create own scripts into your startups. Good luck!



Leave a Reply