<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- credit due to: http://use.perl.org/~ziggy/journal/9780 -->
<!-- but yeah, fuck using Perl... -->
<!-- and let's not forget to check: http://www.xmldatabases.org/WK/blog/1086_Cleaning_up_iTunes_plist_XML.item -->

<!-- invoke with$ java org.apache.xalan.xslt.Process -IN /Users/rob/Music/iTunes/iTunes\ Music\ Library.xml -XSL ~/Library/XSL/iTunesLib2pipeDfile.xsl -OUT ~/Desktop/iTunesLib.txt -->

<xsl:output method="text"/>

<xsl:template match="text()"/>

<xsl:template match="plist/dict/dict[preceding-sibling::key[text() = 'Tracks']]">
<xsl:apply-templates select="dict" mode="display"/>
</xsl:template>

<xsl:template match="dict" mode="display">
	<xsl:text>(NULL, </xsl:text>
	<xsl:apply-templates select="key[. = 'Name']" mode="display"/><!-- aka Track Name -->
	<xsl:choose>
		<xsl:when test="key[. = 'Track Number']">
			<xsl:apply-templates select="key[. = 'Track Number']" mode="display"/>
		</xsl:when>
		<xsl:otherwise><xsl:text>NULL, </xsl:text></xsl:otherwise>
	</xsl:choose>
	<xsl:choose>
		<xsl:when test="key[. = 'Genre']">
			<xsl:apply-templates select="key[. = 'Genre']" mode="display"/>
		</xsl:when>
		<xsl:otherwise><xsl:text>NULL, </xsl:text></xsl:otherwise>
	</xsl:choose>
	<xsl:choose>
		<!-- PROBLEM!! >> how are we going to escape quote characters? -->
		<!-- PROBLEM!! >> need to get this to yield the artist_id and not the artist_name -->
		<xsl:when test="key[. = 'Artist']">
			<xsl:apply-templates select="key[. = 'Artist']" mode="display"/>
		</xsl:when>
		<xsl:otherwise><xsl:text>NULL, </xsl:text></xsl:otherwise>
	</xsl:choose>
	<xsl:choose>
		<!-- PROBLEM!! >> how are we going to escape quote characters? -->
		<!-- PROBLEM!! >> need to get this to yield the album_id and not the album_title -->
		<xsl:when test="key[. = 'Album']">
			<xsl:apply-templates select="key[. = 'Album']" mode="display"/>
		</xsl:when>
		<xsl:otherwise><xsl:text>NULL, </xsl:text></xsl:otherwise>
	</xsl:choose>
	<xsl:text>NULL, </xsl:text>
	<xsl:choose>
		<!-- PROBLEM!! >> how are we going to escape quote characters? line breaks? -->
		<xsl:when test="key[. = 'Comments']">
			<xsl:apply-templates select="key[. = 'Comments']" mode="display"/>
		</xsl:when>
		<xsl:otherwise><xsl:text>NULL, </xsl:text></xsl:otherwise>
	</xsl:choose>
	<xsl:choose>
		<xsl:when test="key[. = 'Rating']">
			<xsl:apply-templates select="key[. = 'Rating']" mode="display"/>
		</xsl:when>
		<xsl:otherwise><xsl:text>NULL)</xsl:text></xsl:otherwise>
	</xsl:choose>
	<xsl:text>&#xa;</xsl:text>
</xsl:template>

<xsl:template match="key" mode="display">
	<xsl:choose>
		<xsl:when test=". = 'Rating'">
			<xsl:text>'</xsl:text><xsl:value-of select="(following-sibling::*/text() div 10)-1"/><xsl:text>')</xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:text>'</xsl:text><xsl:value-of select="following-sibling::*/text()"/><xsl:text>', </xsl:text>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- DESIRED OUTPUT:
	(NULL, 'track_title', '#', 'genre', 'artist_id', 'album_id', NULL, 'track_comments', 'track_rating')
--><!-- Problems, challenges, etc...; (1) how do we get the artist_id's ?? and (3) how do we get the album_id's...? -->

</xsl:stylesheet>