php - 如何替换链接子项以创建适合播客的提要?

标签 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>

由此,我想创建一个适合播客的提要。我想将 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 中做到这一点?

最佳答案

这是一个简单而纯粹的 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. 所需新 URL 的组成部分被指定为全局 <xsl:param> s

关于php - 如何替换链接子项以创建适合播客的提要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/321834/

相关文章:

php - 仅当购物车项目具有特定元数据时,才从购物车页面中删除 WooCommerce 购物车数量选择器

php - 为什么mysql丢弃浮点值?

c# - 如何将对象集合/字典序列化为 <key>value</key>

MySQL 查询到 XQuery

xml - 使用 XSLT 向 SOAP XML 消息添加多个命名空间

php - 比较运算符 - 类型杂耍和 bool 值

php - 用 PHP 解析 iTunes RSS Atom 提要?

java - 如何将我的数据加载到 GraphML 文件中以便于在 Prefuse 中使用?

xml - 使用 XSLT 从 XML 中检索所有属性值

java - 使用 XSLT 从 xml 文件的父标签和子标签获取值