<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
	xmlns="http://xspf.org/ns/0/"
	exclude-result-prefixes="str"
	xmlns:str="http://exslt.org/strings"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output
	method="xml"
	encoding="utf-8"
	media-type="application/xspf+xml"/>

<xsl:param name="date"/>
<xsl:param name="title"/>

<xsl:template match="/playlist">
 <playlist version="1">
  <creator>Geoffrey Swift</creator>
   <xsl:if test="$date">
    <date>
     <xsl:value-of select="$date"/>
    </date>
   </xsl:if>
  <info>http://www.trollied.org/~blimey/</info>
  <title>
   <xsl:choose>
    <xsl:when test="$title">
     <xsl:value-of select="$title"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:text>Wimpy to XSPF converter</xsl:text>
    </xsl:otherwise>
   </xsl:choose>
  </title>
  <trackList>
   <xsl:apply-templates select="item"/>
  </trackList>
 </playlist>
</xsl:template>

<!-- <item> may refer to another playlist, probably not well formed -->
<xsl:template match="item['.xml' = substring(filename, string-length(filename) - (4 - 1), 4)]">
 <xsl:variable name="filename">
  <xsl:choose>
   <xsl:when test="function-available('str:encode-uri')">
    <xsl:value-of select="str:encode-uri(filename, true)"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="filename"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:variable>
 <xsl:variable name="url" select="concat('http://www.trollied.org/~blimey/radio/wimpy2xml.php?url=', $filename)"/>
 <xsl:variable name="doc" select="document($url)"/>
 <xsl:apply-templates select="$doc/playlist/item"/>
</xsl:template>

<xsl:template match="item">
 <track>
  <location>
   <xsl:value-of select="filename"/>
  </location>
  <creator>
   <xsl:value-of select="artist"/>
  </creator>
  <title>
   <xsl:value-of select="title"/>
  </title>
  <image>
   <xsl:value-of select="visual"/>
  </image>
 </track>
</xsl:template>

</xsl:stylesheet>
