Reputation: 155
So, hopefully this will be my final request for help on this project, as you guys have already helped so much already.
Basically, I'm writing some code that takes a YouTube playlist and converts it to an RSS feed. I've gotten it to work the way I want to, except for one minor detail: I'm not sure why, but I need to remove one of the namespaces from the YouTube XML before it will compile.
The namespace declaration from YouTube looks like this:
<feed xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:gml='http://www.opengis.net/gml' xmlns:yt='http://gdata.youtube.com/schemas/2007' xmlns:georss='http://www.georss.org/georss' gd:etag='W/"A04NRn47eCp7I2A9WhJTF0g."'>
Normally, I'd use the exact same ones, except for whatever reason if I include the xmlns='http://www.w3.org/2005/Atom'
segment, the whole thing returns null.
Up until now, my technique has been to just chop that line out of the YouTube XML, however this strikes me as a tad inelegant. If someone who knows more about this than I do could point out what's wrong with my current code, I'd greatly appreciate it.
Adam
EDIT: For reference, here's my XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007' gd:etag='W/"Ak8EQX47eCp7I2A9WhdSEkQ."'>
<xsl:template match="/">
<xsl:for-each select="entry">
<xsl:if test="yt:position < 3">
<item>
<title><xsl:value-of select="title" /></title>
<xsl:variable name="videoid" select="substring-before(substring-after(media:group/media:content/@url, 'v/'), '?')" /> <!--Extracting the YouTube video ID-->
<description>
<embed src="http://www.youtube.com/v/{$videoid}" type="application/x-shockwave-flash" allowscriptaccess="never" allowfullscreen="true" width="500" height="300"></embed> <!--Embed code for the video-->
<br />
<br />
<xsl:call-template name="ParseLink"> <!--Parses the video description such that URLs become links-->
<xsl:with-param name="text" select="media:group/media:description" />
</xsl:call-template>
<img src="http://i.ytimg.com/vi/{$videoid}/hqdefault.jpg" style="display:none"/> <!--Includes hidden thumbnail-->
</description>
<link>http://www.youtube.com/watch?v=<xsl:value-of select="$videoid" /></link> <!--Link to video on YouTube-->
<guid isPermaLink="false">http://www.youtube.com/watch?v=<xsl:value-of select="$videoid" /></guid>
</item>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template name="ParseLink"> <!--YouTube automatically turns URLs into links, this performs the same function for RSS-->
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text,'http')">
<a href="http{substring-before(substring-after($text, 'http'), '
')}">Watch the complete video on our website</a>
<br/>
<xsl:call-template name="ParseText"> <!--Once the link is found, parses the rest of the text-->
<xsl:with-param name="text">
<xsl:value-of select="substring-after($text,'
')"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="ParseText"> <!--Replaces line breaks in the description to <br> tags-->
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text,'
')">
<xsl:value-of select="substring-before($text,'
')"/>
<br />
<xsl:call-template name="ParseText">
<xsl:with-param name="text">
<xsl:value-of select="substring-after($text,'
')"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Upvotes: 0
Views: 636
Reputation: 6947
The reason your stylesheet does not work is because you don't have the Atom namespace defined... The default namespace from YouTube is http://www.w3.org/2005/Atom. If you want to match on the "feed" node, you would have a stylesheet like this:
<xsl:stylesheet xmlns:atom="http://www.w3.org/2005/Atom" ... more stuff ... >
<xsl:template match="/atom:feed">
etc. etc.
</xsl:template>
</xsl:stylesheet>
EDIT: Here's an example stylesheet that works:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007" gd:etag="W/"Ak8EQX47eCp7I2A9WhdSEkQ."">
<xsl:template match="/atom:feed">
<xsl:apply-templates select="atom:entry"/>
</xsl:template>
<xsl:template match="atom:entry">
<p>Entry:<xsl:value-of select="atom:title"/>
</p>
</xsl:template>
</xsl:stylesheet>
EDIT: OK, Here's your updated stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007" gd:etag="W/"Ak8EQX47eCp7I2A9WhdSEkQ."">
<xsl:template match="/atom:feed">
<xsl:for-each select="atom:entry">
<item>
<title>
<xsl:value-of select="atom:title"/>
</title>
<xsl:variable name="videoid" select="substring-before(substring-after(media:group/media:content/@url, 'v/'), '?')"/>
<!--Extracting the YouTube video ID-->
<description>
<embed src="http://www.youtube.com/v/{$videoid}" type="application/x-shockwave-flash" allowscriptaccess="never" allowfullscreen="true" width="500" height="300"/>
<!--Embed code for the video-->
<br/>
<br/>
<xsl:call-template name="ParseLink">
<!--Parses the video description such that URLs become links-->
<xsl:with-param name="text" select="media:group/media:description"/>
</xsl:call-template>
<img src="http://i.ytimg.com/vi/{$videoid}/hqdefault.jpg" style="display:none"/>
<!--Includes hidden thumbnail-->
</description>
<link>http://www.youtube.com/watch?v=<xsl:value-of select="$videoid"/>
</link>
<!--Link to video on YouTube-->
<guid isPermaLink="false">http://www.youtube.com/watch?v=<xsl:value-of select="$videoid"/>
</guid>
</item>
</xsl:for-each>
</xsl:template>
<xsl:template name="ParseLink">
<!--YouTube automatically turns URLs into links, this performs the same function for RSS-->
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text,'http')">
<a href="http{substring-before(substring-after($text, 'http'), '
')}">Watch the complete video on our website</a>
<br/>
<xsl:call-template name="ParseText">
<!--Once the link is found, parses the rest of the text-->
<xsl:with-param name="text">
<xsl:value-of select="substring-after($text,'
')"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="ParseText">
<!--Replaces line breaks in the description to <br> tags-->
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text,'
')">
<xsl:value-of select="substring-before($text,'
')"/>
<br/>
<xsl:call-template name="ParseText">
<xsl:with-param name="text">
<xsl:value-of select="substring-after($text,'
')"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1