0

ですから、うまくいけば、これがこのプロジェクトに関する私の最後のリクエストになると思います。

基本的には、YouTube プレイリストを取得して RSS フィードに変換するコードを書いています。1 つの小さな詳細を除いて、希望どおりに動作するようになりました。理由はわかりませんが、コンパイルする前に、YouTube XML から名前空間の 1 つを削除する必要があります。

YouTube の名前空間宣言は次のようになります。

<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/&quot;A04NRn47eCp7I2A9WhJTF0g.&quot;'>

通常、まったく同じものを使用しますが、何らかの理由でxmlns='http://www.w3.org/2005/Atom'セグメントを含めると、全体が null を返します。

これまで、私のテクニックは YouTube XML からその行を切り取っていましたが、これは少し洗練されていないように思えます。これについて私よりも詳しい人が、現在のコードの何が問題なのかを指摘できれば、非常にありがたいです。

アダム

編集:参考までに、ここに私の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/&quot;Ak8EQX47eCp7I2A9WhdSEkQ.&quot;'>

<xsl:template match="/">
<xsl:for-each select="entry">
<xsl:if test="yt:position &lt; 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'), '&#xA;')}">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,'&#xA;')"/>
                </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,'&#xA;')">
            <xsl:value-of select="substring-before($text,'&#xA;')"/>
            <br />
            <xsl:call-template name="ParseText">
                <xsl:with-param name="text">
                    <xsl:value-of select="substring-after($text,'&#xA;')"/>
                </xsl:with-param>
            </xsl:call-template>
        </xsl:when>

        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>

    </xsl:choose>
</xsl:template>

4

1 に答える 1

1

スタイルシートが機能しない理由は、Atom名前空間が定義されていないためです...YouTubeのデフォルトの名前空間はhttp://www.w3.org/2005/Atomです。「フィード」ノードで一致させたい場合は、次のようなスタイルシートがあります。

<xsl:stylesheet xmlns:atom="http://www.w3.org/2005/Atom" ... more stuff ... >
    <xsl:template match="/atom:feed">
        etc. etc.
    </xsl:template>
</xsl: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/&quot;Ak8EQX47eCp7I2A9WhdSEkQ.&quot;">
    <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>

編集:OK、更新されたスタイルシートは次のとおりです。

<?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/&quot;Ak8EQX47eCp7I2A9WhdSEkQ.&quot;">
    <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'), '&#xA;')}">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,'&#xA;')"/>
                </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,'&#xA;')">
            <xsl:value-of select="substring-before($text,'&#xA;')"/>
            <br/>
            <xsl:call-template name="ParseText">
                <xsl:with-param name="text">
                    <xsl:value-of select="substring-after($text,'&#xA;')"/>
                </xsl:with-param>
            </xsl:call-template>
        </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
于 2012-06-27T21:21:05.897 に答える