I am writing xsl stylesheet to extract information from iTunes Music Library. xml file.
I want to store track information of playlists in an array and later iterate over them to get more information. I am confused how to store values in an array in xslt?
My attempt is here:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/">
<xsl:variable name="tracks"
select="plist/dict/array/dict[integer[preceding-sibling::key[1]='Playlist ID']=6711]/array/dict/integer[preceding-sibling::key[1]='Track ID']" />
<!-- I want to iterate over that array outside for-each loop and gather more information, The below code is not working.-->
<xsl:for-each select="$tracks">
<xsl:value-of select="." />
<xsl:value-of select="plist/dict/dict/dict[integer[preceding-sibling::key[1]='Track ID']="."]/integer[preceding-sibling::key[1]='Track ID']" />
<xsl:value-of select="plist/dict/dict/dict[integer[preceding-sibling::key[1]='Track ID']="."]/string[preceding-sibling::key[1]='Name']" />
<xsl:value-of select="plist/dict/dict/dict[integer[preceding-sibling::key[1]='Track ID']="."]/string[preceding-sibling::key[1]='Total Time']" />
<xsl:value-of select="plist/dict/dict/dict[integer[preceding-sibling::key[1]='Track ID']="."]/string[preceding-sibling::key[1]='Location']" />
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
A typical Track Ids which are populated in array variable “tracks”, are represented in below manner in itunes list. I want to list Name, Location, time infor for each Track Ids stored in the array. Something wrong with my conditions.
<plist>
<dict>
<dict>
<dict>
<key>Track ID</key>
<integer>1633</integer>
<key>Name</key>
<string>Right here</string>
<key>Kind</key>
<string>MPEG audio file</string>
<key>Total Time</key>
<integer>358870</integer>
<key>Location</key>
<string>/Users/rakesh/Music/iTunes/iTunes%20Media/Music/track1633.mp3</string>
</dict>
<dict>
<!-- Next Track info -->
</dict>
</dict>
</dict>
</plist>
Here I am stuck. Can any XSLT experts here to help me out?
You could just create a variable and populate its results with an XPath expression all in one go rather than interate through to build it.
Alternatively you could use xsl:keys at the begining of your document, I’m assuming that the path to the track information is
/plist/dict/array/dictwith the first dict key beingTracksThis allows you to do
key('playlists','4555')to return all the trackids associated with playlist id4555and alsokey('tracks','1234')to get the dict object associated with track id1234You can then combine the two together to do
That will set
$mytracksequal to the array of dict objects for the tracks in playlist 6711. It aslo has the benefits of the speed enhancement granted by xsl:keyEDIT UPDATE—-
I’m guessing you are trying to make a CSV from this so this code should do that
to match a different playlist id just change the value of myplaylist
–EDIT Version without
xsl:key,again just alter the value of the myplaylist variable–EDIT Now mofified to original sort order of playlist
–EDIT Attempt to work around Qt Limitations