PDA

View Full Version : xPath for traversing iTunes XML


Spudhead
12-21-2004, 06:36 PM
Ok, so I've loaded all my MP3's into iTunes :) and I notice that you can export it as XML. "Hmm," thinks I, "this could be interesting."

So I've got my XML file, and it goes a little something like this:


<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>Major Version</key>
<integer>1</integer>
<key>Minor Version</key>
<integer>1</integer>
<key>Application Version</key>
<string>4.6</string>
<key>Music Folder</key>
<string>file://localhost/*****/</string>
<key>Library Persistent ID</key>
<string>9AAAD18079D148DB</string>
<key>Tracks</key>
<dict>
<key>35</key>
<dict>
<key>Track ID</key>
<integer>35</integer>
<key>Name</key>
<string>Fish</string>
<key>Artist</key>
<string>Mr. Scruff</string>
<key>Album</key>
<string>Funkungfusion (2 of 2)</string>
<key>Grouping</key>
<string>Tunes</string>
<key>Genre</key>
<string>Breaks</string>
<key>Kind</key>
<string>MPEG audio file</string>
<key>Size</key>
<integer>4049024</integer>
<key>Total Time</key>
<integer>252160</integer>
<key>Track Number</key>
<integer>1</integer>
<key>Date Modified</key>
<date>2004-12-14T19:15:54Z</date>
<key>Date Added</key>
<date>2004-12-09T13:15:54Z</date>
<key>Bit Rate</key>
<integer>128</integer>
<key>Sample Rate</key>
<integer>44100</integer>
<key>Normalization</key>
<integer>783</integer>
<key>Artwork Count</key>
<integer>1</integer>
<key>Location</key>
<string>file://localhost/*****/</string>
<key>File Folder Count</key>
<integer>-1</integer>
<key>Library Folder Count</key>
<integer>-1</integer>
</dict>


Now, what I want is this (I'm doing it in ASP via Microsoft.XMLDOM if that's any help):

A nodelist of all "string" nodes where the previous node is a "key" node containing the text "Name".

Get where I'm going with this? :thumbsup: Is there a better way of doing it?

allida77
12-21-2004, 08:23 PM
I do not know the answer to your question but this may be intersting to you as well:http://developer.apple.com/sdk/itunescomsdk.html

Spudhead
12-21-2004, 09:06 PM
It certainly is interesting, thanks. Although it's not letting me download it at the moment :confused:.

I'm still going to have a go at the xPath issue though. I KNOW there's a way, it's something like "plist/dict/dict/dict/string[previous-sibling::="Name"]/text()"

but that's not even syntactically correct :o :confused:

mark87
09-10-2005, 01:59 AM
Yes, I'm opening a post almost a year old! But it's on topic.

I've just learnt XML/XSL this evening, and wanted to make my iTunes XML file into something a bit prettier! I came up with this for the XSL (to display artist, album, and song only) -


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">

<html>
<body>
<h1>iTunes Library (<xsl:value-of select="count(/*/*/*/key)" /> Tracks)</h1>

<table border="1">

<tr bgcolor="#000000">
<th><font color="#FFFFFF">Artist</font></th>
<th><font color="#FFFFFF">Album</font></th>
<th><font color="#FFFFFF">Song</font></th>
</tr>

<xsl:for-each select="/*/*/dict[1]/dict">
<xsl:call-template name="list" />
</xsl:for-each>

</table>
</body>
</html>

</xsl:template>

<xsl:template name="list">
<tr>
<td><xsl:value-of select="child::*[preceding-sibling::* = 'Artist']" /></td>
<td><xsl:value-of select="child::*[preceding-sibling::* = 'Album']" /></td>
<td><xsl:value-of select="child::*[preceding-sibling::* = 'Name']" /></td>
</tr>
</xsl:template>

</xsl:stylesheet>


If Spudhead is still around, perhaps it'll help!

I've just realised, I have far too much music... And I know it uses deprecated HTML tags. :)

Also, how do I sort in order of artist name? I know there's xml:sort but I couldn't work out where to put it.