我有一个 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>
请注意此解决方案的以下具体功能:
我们使用 XSLT 设计模式来使用和覆盖 identity transformation .
名为“GetFileName”的模板从完整的 URL(作为参数传递)中提取,仅提取去掉文件扩展名的文件名。这是递归调用自身的命名模板的一个很好的示例。
- 所需新 URL 的组成部分被指定为全局
<xsl:param>
s
关于php - 如何替换链接子项以创建适合播客的提要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/321834/