[xsl] Parsing my itunes library with xsl!

Subject: [xsl] Parsing my itunes library with xsl!
From: Søren Jepsen <sje@xxxxxxxxxxx>
Date: Tue, 6 Mar 2007 17:37:32 +0100
Hi all!

I'm currently trying to parse my ITunes library.xml file into a more =20
neatly xml structure (example):

<Library>
<Genre genre=3D"Miscellaneous">
<Artist Name=3D"Aretha Franklin">
<Album Title=3D"Queen of Soul: The Atlantic Recordings (2 of 4)">
<Tracks/>
</Album>
</Artist>
<Artist Name=3D"Aretha Franklin">
<Album Title=3D"Queen of Soul: The Atlantic Recordings (3 of 4)">
<Tracks/>
</Album>
</Artist>
<Artist Name=3D"Max Graham vs Yes">
<Album Title=3D"Absolute Summer Hits 2005">
<Tracks/>
</Album>
</Artist>
</Genre>
</Library>

My problem is i want the following structure, where the Artist is =20
only listed once, and
contains multiple albums below the artist:

<Library>
<Genre genre=3D"Miscellaneous">
<Artist Name=3D"Aretha Franklin">
<Album Title=3D"Queen of Soul: The Atlantic Recordings (2 of 4)">
<Tracks/>
</Album>
<Album Title=3D"Queen of Soul: The Atlantic Recordings (3 of 4)">
<Tracks/>
</Album>
</Artist>
<Artist Name=3D"Max Graham vs Yes">
<Album Title=3D"Absolute Summer Hits 2005">
<Tracks/>
</Album>
</Artist>
</Genre>
</Library>

My xsl looks the following:

<xsl:stylesheet xmlns:xsl=3D"http://www.w3.org/1999/XSL/Transform"; =20
version=3D"1.0">
<xsl:key name=3D"songsByAlbum" match=3D"dict" use=3D"string[preceding- =20=


sibling::key[1]=3D'Album']"/>
<xsl:key name=3D"songsByGenre" match=3D"dict" use=3D"string[preceding- =20=


sibling::key[1]=3D'Genre']"/>

<xsl:output method=3D"html" encoding=3D"UTF-8" indent=3D"yes"/>

<xsl:template match=3D"/plist/dict/dict">
<Library>
<xsl:for-each select=3D"dict[generate-id(.)=3Dgenerate-id(key=20
('songsByGenre',string)[1])]">
<xsl:sort select=3D"string[preceding-sibling::key[1]=3D'Genre']"/>
<xsl:for-each select=3D"key('songsByGenre',string)[1]">
<xsl:call-template name=3D"albumsInGenre">
<xsl:with-param name=3D"genre" select=3D"string[preceding-sibling::key [1]=20=


=3D'Genre']"/>
</xsl:call-template>
</xsl:for-each>
</xsl:for-each>
</Library>
</xsl:template>


<xsl:template name=3D"albumsInGenre"> <xsl:param name=3D"genre"/> <xsl:variable name=3D"song" select=3D"/plist/dict/dict/dict"/>

<Genre>
<xsl:attribute name=3D"genre">
<xsl:value-of select=3D"$genre"/>
</xsl:attribute>

<xsl:for-each select=3D"$song[generate-id(.)=3Dgenerate-id(key=20
('songsByAlbum',string[preceding-sibling::key[1]=3D'Album'])[1])]">


<xsl:sort select=3D"string[preceding-sibling::key[1]=3D'Album']"/>
<xsl:for-each select=3D"key('songsByAlbum',string[preceding- sibling::key=20=


[1]=3D'Album'])[string[preceding-sibling::key[1]=3D'Genre']=3D$genre] [1]">=


<Artist> <xsl:call-template name=3D"artistName"/> <Album> <xsl:call-template name=3D"albumName"/> </Album> </Artist>

</xsl:for-each>
</xsl:for-each>
</Genre>
</xsl:template>

<xsl:template name=3D"albumName">
<xsl:attribute name=3D"Title">
<xsl:value-of select=3D"string[preceding-sibling::key[1]=3D'Album']"/>
</xsl:attribute>
</xsl:template>

<xsl:template name=3D"artistName">
<xsl:attribute name=3D"Name">
<xsl:value-of select=3D"string[preceding-sibling::key[1]=3D'Artist']"/>
</xsl:attribute>
</xsl:template>

<xsl:template match=3D"text()"/> <!-- swallow up unmatched text -->

</xsl:stylesheet>


For testing export xml from your own Itunes.



Any ideas?


Kindly regards

SJE

Current Thread