--- /dev/null
+#!/bin/sh
+
+TVFILE=tv.xml
+XSLT=xml2sql.xslt
+SQLFILE=tv.sql
+DB=test.sqlite
+
+echo "Converting xmltv data to SQL..." >&2
+saxonb-xslt -xsl:$XSLT -s:$TVFILE -dtd:off >$SQLFILE
+echo "Reading data into database..." >&2
+rm $DB; sqlite3 $DB <$SQLFILE
+
+echo "Fetching available dates..." >&2
+DATES=$( sqlite3 $DB 'select distinct datum from programme order by datum asc' )
+
+echo "<html>"
+echo "<h2>Aantal kanalen per provider</h2>"
+
+echo "Fetching number of channels per provider..." >&2
+echo "<table>"
+sqlite3 -html -header $DB 'select source as provider,count(*) as aantal from channel group by source;'
+echo "</table>"
+
+SQL='SELECT source'
+for datum in $DATES
+do
+ SQL="$SQL , count(case when programme.datum=\"$datum\" then 1 end) as \`$datum\`"
+done
+SQL="$SQL from programme"
+SQL="$SQL group by source"
+
+echo "Fetching number of programmes per provider per day..." >&2
+echo "<h2>Aantal programmas in de gids per provider per dag</h2>"
+echo "<table>"
+sqlite3 -html -header $DB "$SQL"
+echo "</table>"
+
+SQL='SELECT p.source, c.name'
+for datum in $DATES
+do
+ SQL="$SQL , count(case when p.datum=\"$datum\" then 1 end) as \`$datum\`"
+done
+SQL="$SQL FROM programme p JOIN channel c ON c.id=p.channel"
+SQL="$SQL group by c.name,c.source"
+SQL="$SQL order by c.name"
+
+echo "Fetching number of programmes per provider, per channel, per day..." >&2
+echo "<h2>Aantal programmas in de gids per provider per kanaal per dag</h2>"
+echo "<table>"
+sqlite3 -html -header $DB "$SQL"
+echo "</table>"
+
+SQL='SELECT p.source, c.name'
+for datum in $DATES
+do
+ SQL="$SQL , count(case when p.datum=\"$datum\" then 1 end) as \`$datum\`"
+done
+SQL="$SQL FROM programme p JOIN channel c ON c.id=p.channel"
+SQL="$SQL group by c.name,c.source"
+SQL="$SQL order by c.source"
+
+echo "Fetching number of programmes per channel, per provider, per day..." >&2
+echo "<h2>Aantal programmas in de gids per kanaal per provider per dag</h2>"
+echo "<table>"
+sqlite3 -html -header $DB "$SQL"
+echo "</table>"
+
+echo "</html>"
--- /dev/null
+<?xml version="1.0"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+xmlns:xs="http://www.w3.org/2001/XMLSchema"
+xmlns:fn="http://www.w3.org/2005/xpath-functions"
+version="2.0" >
+<xsl:output method="text" omit-xml-declaration="yes" />
+
+<xsl:variable name="sources" as="element()*">
+ <Item>tvgids.nl</Item>
+ <Item>rtl.nl</Item>
+ <Item>horizon.tv</Item>
+ <Item>ziggogids.nl</Item>
+</xsl:variable>
+
+<xsl:variable name="today" select="current-date()" />
+<xsl:variable name="day" select="xs:duration('P1D')" />
+
+<xsl:variable name="dates" as="element()*">
+ <xsl:for-each select="0 to 21">
+ <date><xsl:value-of select="$today + .*xs:dayTimeDuration('P1D')"/></date>
+ </xsl:for-each>
+</xsl:variable>
+
+<xsl:template match="/">
+ <xsl:text>BEGIN TRANSACTION; </xsl:text>
+ <xsl:text>CREATE TABLE channel (id varchar(30), source varchar(20), name varchar(255)); </xsl:text>
+ <xsl:for-each select="tv/channel">
+ <xsl:text>INSERT INTO channel (id,source,name) VALUES ("</xsl:text>
+ <xsl:value-of select="@id"/>
+ <xsl:text>","</xsl:text>
+ <xsl:value-of select="fn:substring-after(@id,'.')"/>
+ <xsl:text>","</xsl:text>
+ <xsl:value-of select="display-name"/>
+ <xsl:text>"); </xsl:text>
+ </xsl:for-each>
+<!--
+ <xsl:for-each select="tv">
+ <xsl:text>Total channels: </xsl:text>
+ <xsl:value-of select="count(channel)"/>
+ <xsl:text> </xsl:text>
+
+ <xsl:for-each select="$sources">
+ <xsl:variable name="source" select="."/>
+ <xsl:text>Number of </xsl:text>
+ <xsl:value-of select="."/>
+ <xsl:text> channels: </xsl:text>
+ <xsl:value-of select="count(//channel[fn:ends-with(@id, $source)])"/>
+ <xsl:text> </xsl:text>
+ </xsl:for-each>
+
+ <xsl:for-each select="$sources">
+ <xsl:variable name="source" select="."/>
+ <xsl:text>Source: </xsl:text>
+ <xsl:value-of select="$source"/>
+ <xsl:text> </xsl:text>
+
+ <xsl:for-each select="$dates">
+ <xsl:variable name="date" select="format-date(., '[Y0001][M01][D01]')"/>
+ <xsl:text>Date: </xsl:text>
+ <xsl:value-of select="$date"/>
+ <xsl:text> </xsl:text>
+
+ <xsl:text>Count: </xsl:text>
+ <xsl:value-of select="count(//programme[fn:starts-with(@start, $date) and fn:ends-with(@channel,$source)])"/>
+ <xsl:text> </xsl:text>
+ </xsl:for-each>
+ </xsl:for-each>
+
+ </xsl:for-each>
+-->
+<!-- <xsl:text>Programmes: </xsl:text> -->
+ <xsl:text>CREATE TABLE programme(datum date, tijd time, channel varchar(30), source varchar(20), name varchar(255)); </xsl:text>
+ <xsl:for-each select="tv/programme">
+ <xsl:text>INSERT INTO programme(datum,tijd,channel,source,name) VALUES ("</xsl:text>
+ <xsl:value-of select="fn:concat(fn:substring(@start,1,4),'-',fn:substring(@start,5,2),'-',fn:substring(@start,7,2))"/>
+ <xsl:text>", "</xsl:text>
+ <xsl:value-of select="fn:concat(fn:substring(@start,9,2),':',fn:substring(@start,11,2))"/>
+ <xsl:text>", "</xsl:text>
+ <xsl:value-of select="@channel"/>
+ <xsl:text>","</xsl:text>
+ <xsl:value-of select="fn:substring-after(@channel,'.')"/>
+ <xsl:text>","</xsl:text>
+ <xsl:value-of select="fn:translate(title,'"','')" />
+ <xsl:text>"); </xsl:text>
+ </xsl:for-each>
+ <xsl:text>COMMIT TRANSACTION; </xsl:text>
+</xsl:template>
+</xsl:stylesheet>