Generating reports with XSL and XML
Ok, now XML is like a very small database. You can change it by opening the file up in a text editor such as notepad. No big nasty servers or binary files for you to mess with. Just edit, save, and you are done! It just so happens that my XML file is full of URLs that sent visitors to my own website. I call them referrers.
So what is XSL? XSL stands for extensible style sheet Language. XSL can transform an XML database to another format - usually HTML web pages. I have been spending a lot of time refreshing my memory with XSL and learning a few new things.
My goal was to create an XSL file that would display a list of referrers to my website. Next to each referrer would be the number of times someone was sent to this site. Easy enough - except that my referrers were listed individually for each day with a count of hits. Take the following for example:
2 <?xml-stylesheet type=“text/xsl” href=“../../../../reports/referrers.xsl” ?>
3 <stats type=”referrers”>
4 <stat value=”/2005/02/desperate-actions.html” count=”1″ day=”56″/>
5 <stat value=”http://www.blogazoo.com/surf.php” count=”2″ day=”56″/>
6 <stat value=”http://www.blogexplosion.com/members/surf.php” count=”9″ day=”56″/>
7 <stat value=”http://blogexplosion.com/members/surf.php” count=”3″ day=”56″/>
8 <stat value=”blockedReferrer” count=”1″ day=”56″/>
9 <stat value=”http://www.grocerylists.org/” count=”1″ day=”56″/>
10 <stat value=”http://www.blogexplosion.com/members/surf.php” count=”15″ day=”57″/>
11 <stat value=”http://www.blogazoo.com/surf.php” count=”5″ day=”57″/>
12 <stat value=”/” count=”1″ day=”57″/>
13 <stat value=”/2004_09_01_lewismoten_archive.html” count=”1″ day=”57″/>
14 <stat value=”http://blogexplosion.com/members/surf.php” count=”4″ day=”57″/>
15 </stats>
You’ll notice that http://blogexplosion.com/members/surf.php appears twice for the 56th and 57th day of the year. I needed to add these two numbers up (3 + 4 = 7) and sort by the sum of the urls after they have been grouped. It wasn’t easy, but I finally came up with a solution.
The solution had to do with generating id’s for XSL keys. I won’t go into it here, but if you are interested, the following may be of some help:
2 <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>
3 <xsl:key name=”value_key” match=”@value” use=”.” />
4 <xsl:template match=”stats”>
5 <body>
6 <xsl:apply-templates>
7 <xsl:sort select=”sum(../stat[@value=current()/@value]/@count)” data-type=”number” order=”descending” />
8 <xsl:sort select=”@value”/>
9 </xsl:apply-templates>
10 </body>
11 </xsl:template>
12 <xsl:template match=”/stats/stat”>
13 <xsl:for-each select=”./@value[generate-id()=generate-id(key(’value_key’,.))]”>
14 <xsl:value-of select=”../@value” />
15 <xsl:value-of select=”sum(../../stat[@value=current()]/@count)”/>
16 </xsl:for-each>
17 </xsl:template>
18 </xsl:stylesheet>
On my little adventures, I also had to figure out how to chop a URL down to size if it was too long. All this XSL stuff is getting me tired. Take a look if you are interested. Nothing special unless you are a programmer I guess.
