iTunes automatically creates a backup copy of its library file in a XML format. Manually creating this file from the File->Library menu is also possible. By doing so, the entire iTunes Library metadata is exported to an XML format where it can be further processed. Common requirements may be to import the library into an Excel spreadsheet as a comma-separated-value (CSV) file, or to insert the metadata into a database using SQL statements.
For me, I was working on a audio/video related project and I needed some sample, yet real-life metadata to work with. So I ended up writing a simple and compressive iTunes parsing library, implemented as a single XSL that performs most of the work. This proved to be especially useful since Apple did not implement the iTunes Library as a 'real' schema, but rather a freeform key/value pair whose definition isn't published anywhere.
The iTunes Stylesheet XSL is freely available under a BSD license. Using it can greatly reduce the amount of effort required to parse and use iTunes Library metadata. The library has been developed and tested against iTunes version 9.
Now on to the code... The following example shows how easy it is to iterate through every title and output the name and artist.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:include href="iTunes.xsl">
<xsl:template match="/">
<xsl:for-each select="plist/dict/dict/dict">
<xsl:call-template name="name">,
<xsl:call-template name="artist">
</xsl:for-each>
</xsl:call-template>
Now suppose the requirement was to insert the library into a SQL database; no problem. The iTunes Stylesheet XSL has a built-in template for escaping when the output is SQL, specifically, it escapes singles quotes.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:include href="iTunes.xsl">
<xsl:template match="/">
<xsl:for-each select="plist/dict/dict/dict">
<xsl:call-template name="sql_escape">
<xsl:with-param name="arg1">
<xsl:call-template name="name">
</xsl:call-template>
</xsl:with-param>,
<xsl:call-template name="sql_escape">
<xsl:with-param name="arg1">
<xsl:call-template name="artist">
</xsl:call-template>
</xsl:with-param>
</xsl:for-each>
</xsl:call-template>
Download iTunes Stylesheet XSL
iTunes Stylesheet XSL contains templates to parse the following:
- Track ID
- Name
- Artist
- Composer
- Album Artist
- Album
- Genre
- Kind
- Size
- Total Time
- Disc Number
- Disc Count
- Track Number
- Track Count
- Year
- Date Modified
- Date Added
- Bit Rate
- Sample Rate
- Comments
- Play Count
- Play Date
- Play Date UTC
- Release Date
- Artwork Count
- Rating
- Rating Computed
- Album Rating
- Series
- Season
- Episode
- Episode Order
- Content Rating
- Persistent ID
- Track Type
- Purchased
- Podcast
- Unplayed
- Has Video
- HD
- Movie
- TV Show
- Music Video
- Video Width
- Video Height
- Location
- File Folder Count
- Library Folder Count








Thanks for this Steve. The iTunes Stylesheet works amazingly well. I would not have known where to start without it.
But the examples you gave above do not work at all. Many of the tags are out of order and others unclosed. Thankfully I didn't give up easily and was able to adjust the examples until I got something that worked.
Perhaps you might want to fix up the examples so that others can benefit like I did. You might also want to package them as xsl files and explain the concept of templating a bit more (we are not all xml experts!).
So while it was a struggle, I did get there eventually and wanted to say thanks.
Peter
I had benefited on those you have posted Steve. Though it took me time to fixed, but then I was able to get the idea and come up to a positive outcome. And though it was time consuming but it work out easily.
Hi, just found this (looking for iTunes bug fix script). Looks like a good good if you need to get all the metadata. But in case people are unawares, the most common data (Title/Album/Time/Track/etc.)is put on the clipboard, tab delimited, via copy. I use this to get the metadata into excel all the time.
xsltproc toDB.xsl iTunes\ Music\ Library.xml
toDB.xsl:20: parser error : Opening and ending tag mismatch: call-template line 14 and for-each
^
toDB.xsl:21: parser error : Premature end of data in tag for-each line 6
^
toDB.xsl:21: parser error : Premature end of data in tag template line 5
^
toDB.xsl:21: parser error : Premature end of data in tag include line 3
^
toDB.xsl:21: parser error : Premature end of data in tag stylesheet line 1
^
cannot parse toDB.xsl
Fantastically insightful thank you, There's no doubt that your current followers would possibly want a great deal more posts of this nature keep up the great hard work.