XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Søren JepsenSubject: Parsing my itunes library with xsl!
Author: Søren Jepsen
Date: 01 Mar 2007 10:42 AM
Hi all!

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

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

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

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

My xsl looks the following:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="songsByAlbum" match="dict" use="string[preceding-sibling::key[1]='Album']"/>
<xsl:key name="songsByGenre" match="dict" use="string[preceding-sibling::key[1]='Genre']"/>

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

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


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

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

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


<xsl:sort select="string[preceding-sibling::key[1]='Album']"/>
<xsl:for-each select="key('songsByAlbum',string[preceding-sibling::key[1]='Album'])[string[preceding-sibling::key[1]='Genre']=$genre][1]">

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

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

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

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

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

</xsl:stylesheet>


Any ideas?

Kindly regards

Søren Jepsen





Posttop
Tony LavinioSubject: Parsing my itunes library with xsl!
Author: Tony Lavinio
Date: 06 Mar 2007 08:48 AM
General XSLT questions not related to Stylus Studio
are best handled on the xsl-list mailing list run by
Mulberry Technologies.

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.