XSLT to for XML History files

Developers
pike
2007-06-08
2012-10-11
  • pike

    pike - 2007-06-08

    Hi

    AWStats has the option to generate its history files as XML. That enables you to transform it for other uses, like graphing your stats with flash/xml or creating ODS spreadsheets.

    The xslt example provided with awstats is fairly simple. Can we gather some more elaborate examples ?

    *-pike

     
    • Laurent Destailleur (Eldy)

      Can I provide your xslt example inside next version of AWStats ?
      If yes, tell me "yes" to this following email address:
      eldy@users.sourceforge.net

       
      • pike

        pike - 2007-11-04

        Hi Eldi

        YES - replied this to the mail address too

        perhaps providing you apply a little bugfix to awstats.pl,
        to fix the first two columns of the first ten lines of the
        "sider" section in xml :-)

        in
        line 3837 (in my version)
        replace
        "${xmlrb}$newkey ".int($url_p{$}||0)
        with
        "${xmlrb}$newkey${xmlrs}".int($url_p{$}||0)

        thanks!
        *-pike

         
    • pike

      pike - 2007-11-04

      Hi

      In addition to the textreport posted earlier, I've made an xslt template
      that transforms the xml history database into a summary xmlfile - actually
      much like the textreport, but in xml

      http://pike.kw.nl/files/software/pike/recent/xslt/awstats-history2summary.xsl

      I now generate the textreport from this xml summary, using

      http://pike.kw.nl/files/software/pike/recent/xslt/awstats-summary2textreport.xsl

      i've also used the output of the summary.xml to create xml to generate
      graphs of statistics spanning several months, with flash, using
      http://www.maani.us/xml_charts/

      sorry for the ad - but its free and easy to use with awstats.

      *-pike

       
    • pike

      pike - 2007-06-08

      Here's one :-) I hope the indentation et al survives the forum ..

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xsl:output method="text"/>

      <!--
      This xsl parses a awstats xml "history file" and generates a small
      plaintext report, nicely suited for cron and mail. some decisions have been
      made regarding what to display - pages or hits, etc. You are free to tweak.

      Commissioned by LabforCulture
      *pike@labforculture.org 20060708
      
      usage:  
      mail -s stats yourboss@yoursite &lt; xsltproc thisfile.xsl database.xml
      
      build for AWSTATS DATA FILE 6.5 (build 1.857)
      

      -->

      &lt;xsl:template match=&quot;/&quot;&gt;
      
      &lt;xsl:for-each select=&quot;/xml/section[@id='general']/table/tr&quot;&gt;
      
          &lt;xsl:text&gt;&amp;#xa;----------------&lt;/xsl:text&gt;
      
          &lt;xsl:for-each select=&quot;td[text()='FirstTime']&quot;&gt;
              &lt;xsl:text&gt;&amp;#xa;Period         &lt;/xsl:text&gt;
      
              &lt;xsl:for-each select=&quot;./following-sibling::node()&quot;&gt;
                  &lt;xsl:value-of select=&quot;concat(': ',substring(.,1,4),'/',substring(.,5,2),'/',substring(.,7,2))&quot; /&gt;
              &lt;/xsl:for-each&gt;
              &lt;xsl:for-each select=&quot;../../tr/td[text()='LastTime']/following-sibling::node()&quot;&gt;
                  &lt;xsl:value-of select=&quot;concat(' - ',substring(.,1,4),'/',substring(.,5,2),'/',substring(.,7,2))&quot; /&gt;
                  &lt;/xsl:for-each&gt;
              &lt;/xsl:for-each&gt;
      
              &lt;xsl:for-each select=&quot;td[text()='TotalUnique']/following-sibling::node()&quot;&gt;
              &lt;xsl:text&gt;&amp;#xa;Total unique visitors  : &lt;/xsl:text&gt;
      
              &lt;xsl:value-of select=&quot;.&quot; /&gt;
          &lt;/xsl:for-each&gt;
      
          &lt;xsl:for-each select=&quot;td[text()='TotalVisits']/following-sibling::node()&quot;&gt;
              &lt;xsl:text&gt;&amp;#xa;Total visits       : &lt;/xsl:text&gt;
              &lt;xsl:value-of select=&quot;.&quot; /&gt;
          &lt;/xsl:for-each&gt;
      
          &lt;/xsl:for-each&gt;
      
          &lt;xsl:for-each  select=&quot;/xml/section[@id='time']/table&quot;&gt;
      
          &lt;xsl:text&gt;&amp;#xa;Total viewed pages : &lt;/xsl:text&gt;&lt;xsl:value-of select=&quot;sum(tr/td[2])&quot; /&gt;
          &lt;xsl:text&gt;&amp;#xa;Total viewed hits  : &lt;/xsl:text&gt;&lt;xsl:value-of select=&quot;sum(tr/td[3])&quot; /&gt;
          &lt;xsl:text&gt;&amp;#xa;Total not viewed pages : &lt;/xsl:text&gt;&lt;xsl:value-of select=&quot;sum(tr/td[5])&quot; /&gt;
          &lt;xsl:text&gt;&amp;#xa;Total not viewed hits  : &lt;/xsl:text&gt;&lt;xsl:value-of select=&quot;sum(tr/td[6])&quot; /&gt;
      
          &lt;/xsl:for-each&gt;
      
          &lt;xsl:for-each  select=&quot;/xml/section[@id='misc']/table&quot;&gt;
      
              &lt;xsl:for-each select=&quot;tr/td[text()='AddToFavourites']/following-sibling::node()[2]&quot;&gt;
                  &lt;xsl:text&gt;&amp;#xa;Added to favourites (?)    : &lt;/xsl:text&gt;
                  &lt;xsl:value-of select=&quot;.&quot; /&gt;
              &lt;/xsl:for-each&gt;
          &lt;/xsl:for-each&gt;
      
          &lt;xsl:text&gt;&amp;#xa;----------------&lt;/xsl:text&gt;
      
          &lt;xsl:for-each  select=&quot;/xml/section[@id='session']/table&quot;&gt;
              &lt;xsl:text&gt;&amp;#xa;Visit duration:&lt;/xsl:text&gt;
      
              &lt;xsl:for-each select=&quot;tr/td[text()='0s-30s']/following-sibling::node()&quot;&gt;
                  &lt;xsl:text&gt;&amp;#xa;   0s-30s      : &lt;/xsl:text&gt;
                  &lt;xsl:value-of select=&quot;.&quot; /&gt;
              &lt;/xsl:for-each&gt;
              &lt;xsl:for-each select=&quot;tr/td[text()='30s-2mn']/following-sibling::node()&quot;&gt;
                  &lt;xsl:text&gt;&amp;#xa;   30s-2mn     : &lt;/xsl:text&gt;
                  &lt;xsl:value-of select=&quot;.&quot; /&gt;
      
              &lt;/xsl:for-each&gt;
              &lt;xsl:for-each select=&quot;tr/td[text()='2mn-5mn']/following-sibling::node()&quot;&gt;
                  &lt;xsl:text&gt;&amp;#xa;   2mn-5mn     : &lt;/xsl:text&gt;
                  &lt;xsl:value-of select=&quot;.&quot; /&gt;
              &lt;/xsl:for-each&gt;
              &lt;xsl:for-each select=&quot;tr/td[text()='5mn-15mn']/following-sibling::node()&quot;&gt;
                  &lt;xsl:text&gt;&amp;#xa;   5mn-15mn    : &lt;/xsl:text&gt;
      
                  &lt;xsl:value-of select=&quot;.&quot; /&gt;
              &lt;/xsl:for-each&gt;
              &lt;xsl:for-each select=&quot;tr/td[text()='15mn-30mn']/following-sibling::node()&quot;&gt;
                  &lt;xsl:text&gt;&amp;#xa;   15mn-30mn   : &lt;/xsl:text&gt;
                  &lt;xsl:value-of select=&quot;.&quot; /&gt;
              &lt;/xsl:for-each&gt;       
              &lt;xsl:for-each select=&quot;tr/td[text()='30mn-1h']/following-sibling::node()&quot;&gt;
                  &lt;xsl:text&gt;&amp;#xa;   30mn-1h     : &lt;/xsl:text&gt;
      
                  &lt;xsl:value-of select=&quot;.&quot; /&gt;
              &lt;/xsl:for-each&gt;   
              &lt;xsl:for-each select=&quot;tr/td[text()='1h+']/following-sibling::node()&quot;&gt;
                  &lt;xsl:text&gt;&amp;#xa;   1h+     : &lt;/xsl:text&gt;
                  &lt;xsl:value-of select=&quot;.&quot; /&gt;
              &lt;/xsl:for-each&gt;
      
          &lt;/xsl:for-each&gt;
      
          &lt;xsl:text&gt;&amp;#xa;----------------&lt;/xsl:text&gt;
      
          &lt;xsl:for-each  select=&quot;/xml/section[@id='domain']/table&quot;&gt;
      
              &lt;xsl:text&gt;&amp;#xa;Countries top 5:&lt;/xsl:text&gt;
              &lt;xsl:for-each select=&quot;tr&quot;&gt;
                  &lt;xsl:sort select=&quot;td[2]&quot; data-type=&quot;number&quot; order=&quot;descending&quot;/&gt;
                  &lt;xsl:if test=&quot;position()&amp;lt;=5&quot;&gt;
                      &lt;xsl:text&gt;&amp;#xa;   &lt;/xsl:text&gt;
                      &lt;xsl:value-of select=&quot;td[1]&quot; /&gt;
                      &lt;xsl:text&gt;        : &lt;/xsl:text&gt;
      
                      &lt;xsl:value-of select=&quot;td[2]&quot; /&gt;
                      &lt;xsl:text&gt; pages&lt;/xsl:text&gt;
                  &lt;/xsl:if&gt;
              &lt;/xsl:for-each&gt;
      
          &lt;/xsl:for-each&gt;
      
          &lt;xsl:text&gt;&amp;#xa;----------------&lt;/xsl:text&gt;
      

      <!--
      awstats xml output is seriously bugged here in my version.
      the first 10 rows of sider has 5 columns, the rest has 4 columns ..
      the second columns content is added as plaintext inside the first column !

      i will wait for a fix. meanwhile, the numbers outputted
      here are dead wrong.
      
          &lt;xsl:for-each  select=&quot;/xml/section[@id='sider']/table&quot;&gt;
      
              &lt;xsl:text&gt;&amp;#xa;Entry pages top 10 (bugged):&lt;/xsl:text&gt;
              &lt;xsl:for-each select=&quot;tr&quot;&gt;
                  &lt;xsl:sort select=&quot;td[4]&quot; data-type=&quot;number&quot; order=&quot;descending&quot;/&gt;
                  &lt;xsl:if test=&quot;position()&amp;lt;=10&quot;&gt;
                      &lt;xsl:text&gt;&amp;#xa;       &lt;/xsl:text&gt;
                      &lt;xsl:value-of select=&quot;td[4]&quot; /&gt;
                      &lt;xsl:text&gt;    : &lt;/xsl:text&gt;
                      &lt;xsl:value-of select=&quot;td[1]&quot; /&gt;
                  &lt;/xsl:if&gt;
              &lt;/xsl:for-each&gt;
      
              &lt;xsl:text&gt;&amp;#xa;Exit pages top 10 (bugged):&lt;/xsl:text&gt;           
              &lt;xsl:for-each select=&quot;tr[position()&amp;lt;=10]&quot;&gt;
                  &lt;xsl:sort select=&quot;td[5]&quot; data-type=&quot;number&quot; order=&quot;descending&quot;/&gt;
                  &lt;xsl:if test=&quot;position()&amp;lt;=10&quot;&gt;
                      &lt;xsl:text&gt;&amp;#xa;       &lt;/xsl:text&gt;
                      &lt;xsl:value-of select=&quot;td[5]&quot; /&gt;
                      &lt;xsl:text&gt;    : &lt;/xsl:text&gt;
                      &lt;xsl:value-of select=&quot;td[1]&quot; /&gt;
                  &lt;/xsl:if&gt;
              &lt;/xsl:for-each&gt;
      
          &lt;/xsl:for-each&gt;
      

      -->

      &lt;xsl:for-each  select=&quot;/xml/section[@id='origin']/table&quot;&gt;
          &lt;xsl:text&gt;&amp;#xa;Users arrived via:&lt;/xsl:text&gt;
          &lt;xsl:for-each select=&quot;tr&quot;&gt;
              &lt;xsl:sort select=&quot;td[2]&quot; data-type=&quot;number&quot; order=&quot;descending&quot;/&gt;
              &lt;xsl:if test=&quot;td[1]='From0'&quot;&gt;
                  &lt;xsl:text&gt;&amp;#xa;   Typed in / from bookmarks       : &lt;/xsl:text&gt;
                  &lt;xsl:value-of select=&quot;td[2]&quot; /&gt;&lt;xsl:text&gt; pages&lt;/xsl:text&gt;
      
              &lt;/xsl:if&gt;
              &lt;xsl:if test=&quot;td[1]='From1'&quot;&gt;
                  &lt;xsl:text&gt;&amp;#xa;   Unknown                 : &lt;/xsl:text&gt;
                  &lt;xsl:value-of select=&quot;td[2]&quot; /&gt;&lt;xsl:text&gt; pages&lt;/xsl:text&gt;
              &lt;/xsl:if&gt;
              &lt;xsl:if test=&quot;td[1]='From2'&quot;&gt;
                  &lt;xsl:text&gt;&amp;#xa;   Linked from an Internet Search Engine   : &lt;/xsl:text&gt;
      
                  &lt;xsl:value-of select=&quot;td[2]&quot; /&gt;&lt;xsl:text&gt; pages&lt;/xsl:text&gt;
              &lt;/xsl:if&gt;
              &lt;xsl:if test=&quot;td[1]='From3'&quot;&gt;
                  &lt;xsl:text&gt;&amp;#xa;   Linked from an external page        : &lt;/xsl:text&gt;
                  &lt;xsl:value-of select=&quot;td[2]&quot; /&gt;&lt;xsl:text&gt; pages&lt;/xsl:text&gt;
              &lt;/xsl:if&gt;
      
              &lt;xsl:if test=&quot;td[1]='From4'&quot;&gt;
                  &lt;xsl:text&gt;&amp;#xa;   Linked from an internal page        : &lt;/xsl:text&gt;
                  &lt;xsl:value-of select=&quot;td[2]&quot; /&gt;&lt;xsl:text&gt; pages&lt;/xsl:text&gt;
              &lt;/xsl:if&gt;
              &lt;xsl:if test=&quot;td[1]='From5'&quot;&gt;
                  &lt;xsl:text&gt;&amp;#xa;   Linked from newsgroups          : &lt;/xsl:text&gt;
      
                  &lt;xsl:value-of select=&quot;td[2]&quot; /&gt;&lt;xsl:text&gt; pages&lt;/xsl:text&gt;
              &lt;/xsl:if&gt;
              &lt;/xsl:for-each&gt;
      &lt;/xsl:for-each&gt;
      
      &lt;xsl:text&gt;&amp;#xa;----------------&lt;/xsl:text&gt;
      
      &lt;xsl:for-each  select=&quot;/xml/section[@id='searchwords']/table&quot;&gt;
          &lt;xsl:text&gt;&amp;#xa;Top search phrases:&lt;/xsl:text&gt;   
          &lt;xsl:for-each select=&quot;tr[position()&amp;lt;=10]&quot;&gt;
      
              &lt;xsl:text&gt;&amp;#xa;   &lt;/xsl:text&gt;
              &lt;xsl:value-of select=&quot;td[1]&quot; /&gt;
              &lt;xsl:text&gt; - &lt;/xsl:text&gt;
              &lt;xsl:value-of select=&quot;td[2]&quot; /&gt;
              &lt;xsl:text&gt; hits&lt;/xsl:text&gt;
          &lt;/xsl:for-each&gt;
      
      &lt;/xsl:for-each&gt;
      
      &lt;xsl:text&gt;&amp;#xa;----------------&lt;/xsl:text&gt;
      
      &lt;xsl:for-each  select=&quot;/xml/section[@id='sereferrals']/table&quot;&gt;
          &lt;xsl:text&gt;&amp;#xa;Robots/spiders:&lt;/xsl:text&gt;   
          &lt;xsl:for-each select=&quot;tr&quot;&gt;
              &lt;xsl:sort select=&quot;td[3]&quot; data-type=&quot;number&quot; order=&quot;descending&quot;/&gt;
              &lt;xsl:if test=&quot;position()&amp;lt;=10&quot;&gt;
                  &lt;xsl:text&gt;&amp;#xa;   &lt;/xsl:text&gt;
      
                  &lt;xsl:value-of select=&quot;td[1]&quot; /&gt;
                  &lt;xsl:text&gt; - &lt;/xsl:text&gt;
                  &lt;xsl:value-of select=&quot;td[3]&quot; /&gt;
                  &lt;xsl:text&gt; hits&lt;/xsl:text&gt;
              &lt;/xsl:if&gt;
          &lt;/xsl:for-each&gt;
      &lt;/xsl:for-each&gt;
      
      &lt;xsl:text&gt;&amp;#xa;----------------&amp;#xa;&lt;/xsl:text&gt;
      
      &lt;/xsl:template&gt;
      

      </xsl:stylesheet>

       
    • pike

      pike - 2007-06-08

      Oh boy, that is really UGLY !
      Download it here:

      http://pike.kw.nl/files/software/pike/recent/xslt/awstats-textreport.xsl

      $2c,
      *pike

       

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks