如何替换链接子项以创建适合播客的Feed?

时间:2008-11-26 19:16:32

标签: php xml xslt rss

我有一个RSS来源:

http://feedity.com/rss.aspx/mr1-kossuth-hu/VVdXUlY
<item>
      <title>2008. november 23.</title>
      <link>http://www.mr1-kossuth.hu/m3u/0039c36f_3003051.m3u</link>
      <description>........</description>
      <pubDate>Wed, 26 Nov 2008 00:00:00 GMT</pubDate>
    </item>

由此,我想创建一个播客友好的Feed。我想将LINK孩子替换为:

http://stream001.radio.hu:8000/content/*.mp3

示例:

<item>
      <title>2008. november 23.</title>
      <link>http://stream001.radio.hu:8000/content/0039c36f_3003051.mp3</link>
      <description>........</description>
      <pubDate>Wed, 26 Nov 2008 00:00:00 GMT</pubDate>
    </item>

我如何在PHP中做到这一点?

4 个答案:

答案 0 :(得分:3)

这是一个简单而纯粹的XSLT 1.0解决方案,仅包含47行,其中一半是结束标记。 以下转型

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:output omit-xml-declaration="yes"/>

 <xsl:param name="pNewLink"
 select="'http://stream001.radio.hu:8000/content/'"/>

 <xsl:param name="pNewExt" select="'.mp3'"/>

    <xsl:template match="node()|@*">
      <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="link">
      <xsl:copy>
         <xsl:copy-of select="@*"/>

         <xsl:variable name="vFName">
           <xsl:call-template name="GetFileName">
             <xsl:with-param name="pFPath" select="."/>
           </xsl:call-template>
         </xsl:variable>

         <xsl:value-of select="concat($pNewLink,$vFName,$pNewExt)"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template name="GetFileName">
      <xsl:param name="pFPath"/>

      <xsl:choose>
        <xsl:when test="not(contains($pFPath, '/'))">
          <xsl:value-of select="substring-before($pFPath, '.')"/>
        </xsl:when>

        <xsl:otherwise>
          <xsl:call-template name="GetFileName">
            <xsl:with-param name="pFPath"
             select="substring-after($pFPath, '/')"/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

应用于提供的源XML文档

<item>
    <title>2008. november 23.</title>
    <link>http://www.mr1-kossuth.hu/m3u/0039c36f_3003051.m3u</link>
    <description>........</description>
    <pubDate>Wed, 26 Nov 2008 00:00:00 GMT</pubDate>
</item>

产生想要的结果

<item>
    <title>2008. november 23.</title>
    <link>http://stream001.radio.hu:8000/content/0039c36f_3003051.mp3</link>
    <description>........</description>
    <pubDate>Wed, 26 Nov 2008 00:00:00 GMT</pubDate>
</item>

请注意此解决方案的以下特定功能:

  1. 我们使用XSLT设计模式来使用和覆盖identity transformation

  2. 名为“GetFileName ”的模板从完整的URL(作为参数传递)中提取,只是文件扩展名被剥离的文件名。这个是递归调用自己的命名模板的一个很好的例子

  3. 所需新网址的组成部分指定为全局<xsl:param> s

答案 1 :(得分:1)

这就是用XSLT做得最好的事情。您可以编写一个简单的XSL转换来执行此操作,或者您可以执行hacky方式并使用preg_match,preg_replace和friends。

答案 2 :(得分:0)

最简单的方法是简单的字符串替换:

$str = file_get_contents('http://feedity.com/rss.aspx/mr1-kossuth-hu/VVdXUlY');
$str = str_replace('<link>http://www.mr1-kossuth.hu/m3u/', '<link>http://stream001.radio.hu:8000/content/', $str);
$str = str_replace('m3u</link>', 'mp3</link>', $str);

完成!

答案 3 :(得分:0)

<?php
 $str = file_get_contents('http://feedity.com/rss.aspx/mr1-kossuth-hu/VVdXUlY');
 $xml = simplexml_load_string($str);
if ($xml) {

$intro = str_replace('<link>http://www.mr1-kossuth.hu/m3u/', '<enclosure url="http://stream001.radio.hu:8000/content/', $xml->asXML());
$intro = str_replace('m3u</link>', 'mp3" type="audio/mpeg" />', $intro);
echo $intro;    
} else {
    $error = "Could not load intro XML file.";
}

?>