After taking a closer look at the WPL format I realized that an XSLT stylesheet to convert it to M3U would be very simple.
I've switched around between music-playing programs over the last few years. I suppose I should call them "media players", but I only use them to play music, which is part of the reason I ended up using Songbird, an open source Windows/Linux/Mac music front end that doesn't pretend to be anything else. It looks a bit like iTunes, without all the ads in your face; how great is that?
Before that I used MediaMonkey, and before that, the Windows Media Player. Guess which of these uses the most standardized, XML-based format for playlists? Surprise: the Microsoft one.
Windows Media Player can create WPL files, which seem to conform to the W3C SMIL standard, and it can export M3U files, which MediaMonkey uses. To convert WPL files to m3u for Songbird, reading them individually into Windows Media Player and exporting them one at a time was annoying. I did some web searches for wpl2m3u and only found one script that I couldn't quite follow, and after taking a closer look at the WPL format I realized that an XSLT stylesheet to convert it to M3U would be very simple. So here it is:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="text"/>
<xsl:template name="textAfterLastSlash"><!-- but actually backslash -->
<xsl:param name="string">dummy string</xsl:param>
<xsl:choose>
<xsl:when test="not(contains($string,'\'))">
<xsl:value-of select="$string"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="textAfterLastSlash">
<xsl:with-param name="string" select="substring-after($string,'\')"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="smil">
<xsl:text>#EXTM3U </xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="media">
<xsl:text>#EXTINF:0,</xsl:text>
<xsl:call-template name="textAfterLastSlash">
<xsl:with-param name="string" select="@src"/>
</xsl:call-template>
<xsl:text> </xsl:text>
<xsl:value-of select="@src"/>
<xsl:text> </xsl:text>
</xsl:template>
<xsl:template match="title"/>
</xsl:stylesheet>
It's not very long, but if you want fancy XSLT, I have a recursive named template, which I wrote for something else but modified here to look for the text after the last backslash. The is a trick I've used more lately to get XSLT to output a carriage return, because if I put an actual carriage return inside of an xsl:text element like I always did before, telling Emacs to re-indent the whole thing tends to screw that up.
With a long plane ride tomorrow night to go to Oxford for the XML Summer School, I want to load up the MP3 player with something conducive to sleeping, so I just converted my playlist of Lata Mangeshkar ballads so that I can put that on. (If you like classic Bollywood soundtracks, check out Music from the Third Floor; if you're new to it and interested, start with the compilations there.)

Leave a comment