Exporting Movies from the Kodi database backup

November 27, 2020

One of the options in Kodi is the ability to export your media as an XML file. I’ve gotten requests for a way to create a csv version of the data. Here’s my attempt at doing that.

I say attempt because it’s really not as trivial as it sounds and is a pain to do well. The csv format has a host of issues and it’s the wrong tool for what users are doing. I know right away if any of the movies contain commas in the title the file will not import correctly.

A LOT of people want to use Excel as a database and general purpose tool to write reports. You *can* use a hammer to put in screws but you’re really not doing yourself any favors.

Here’s an xslt script to export movie data from the kodi videodb.xml file.

<!--
This xsl produces a csv format list of kodi tv show content.
It is used from the command line so it's use can be automated

Example for Windows (Assumes msxsl from microsoft is installed. http://www.microsoft.com/en-us/download/details.aspx?id=21714):
msxsl videodb.xml VideodbToTvShowsCsv.xslt -o tvshows.csv

Example for linux (Assumes "xalan" is installed. Should be available from your software repository):
cat videodb.xml | xalan -xsl VideodbToTvShowsCsv.xslt -out tvshows.csv

written by Jay Sprenkle
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" encoding="us-ascii"/>

<!-- television shows-->
<xsl:template match="movie">
    <!--title-->
    <xsl:text>"</xsl:text>
    <xsl:value-of select='../title/text()'/>
    <xsl:text>"</xsl:text>
    <xsl:text>,</xsl:text>

    <xsl:value-of select='year/text()'/>
    <xsl:text>,</xsl:text>

    <!-- may be more than one rating so just take the first one -->
    <xsl:value-of select='ratings/rating[1]/value/text()'/>
    <xsl:text>,</xsl:text>

    <!--tagline-->
    <xsl:text>"</xsl:text>
    <xsl:value-of select='tagline/text()'/>
    <xsl:text>"</xsl:text>
    <xsl:text>,</xsl:text>

    <xsl:value-of select='runtime/text()'/>
    <xsl:text>,</xsl:text>

    <xsl:value-of select='id/text()'/>

    <xsl:text>
</xsl:text>
</xsl:template>

<xsl:template match="/videodb">
    <xsl:text>"Title"</xsl:text>
    <xsl:text>,</xsl:text>

    <xsl:text>"year"</xsl:text>
    <xsl:text>,</xsl:text>

    <xsl:text>"rating"</xsl:text>
    <xsl:text>,</xsl:text>

    <xsl:text>"tagline"</xsl:text>
    <xsl:text>,</xsl:text>

    <xsl:text>"runtime"</xsl:text>
    <xsl:text>,</xsl:text>

    <xsl:text>"id"</xsl:text>

    <xsl:text>
</xsl:text>

    <xsl:apply-templates select="movie"/>
</xsl:template>

</xsl:stylesheet>