【问题标题】:How to extract podcast URLs from XML feed with xsltproc?如何使用 xsltproc 从 XML 提要中提取播客 URL?
【发布时间】:2018-02-16 10:39:20
【问题描述】:

我想使用 xsltproc(或我可以在 Bash 中使用的任何其他工具)从播客提要中提取 URL。有以下两种类型的 XML 提要。

A型

<rss xmlns:media="http://search.yahoo.com/mrss/">
    <channel>
    <title>Podcast</title>
    <item>
        <title>Episode</title>
        <media:content url="http://example.org/example.mp3" fileSize="1234" type="audio/mpeg"/>
    </item>
    </channel>
</rss>

B型

<rss>
    <channel>
    <title>Podcast</title>
    <item>
        <title>Episode</title>
        <guid>episode::x</guid>
        <enclosure type="image/jpeg" url="http://example.org/coverart.jpg"/>
        <enclosure type="audio/mpeg" url="http://example.net/audio.mp3"/>
    </item>
    </channel>
</rss>

我有以下样式表,它返回类型 B 的 URL,但不返回类型 A 的 URL。我什至可以将这两者混合在一个样式表中吗?

<?xml version="1.0"?>
<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">
    <output method="text"/>
    <template match="/">
        <for-each select = "rss/channel/item/enclosure">
            <value-of select="@url"/><text>&#10;</text>
        </for-each>
        <for-each select = "rss/channel/item/media">
            <value-of select="@url"/><text>&#10;</text>
        </for-each>
    </template>
</stylesheet>

【问题讨论】:

    标签: xslt rss


    【解决方案1】:

    Type A XML 中,有一个与&lt;content&gt; 节点关联的命名空间,其别名为media。命名空间不包含在样式表中。它需要包含在样式表中,以便正确访问与命名空间关联的元素。

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:media="http://search.yahoo.com/mrss/"
        exclude-result-prefixes="media">
    

    在模板内部,for-each 循环应该是media:content,(缺少content 元素)。

    <xsl:for-each select="//media:content">
        <xsl:value-of select="@url" />
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-12
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 2017-03-14
      • 1970-01-01
      相关资源
      最近更新 更多