在PHP中,从基于RDF的RSS Feed创建ATOM源

时间:2013-05-14 08:43:58

标签: php xml xslt rss atom-feed

由于遗留系统我生成了一个旧的基于RDF的RSS 1.0 Feed,并且大多数RSS阅读器无法处理HTTP Basic Auth,我想要一个PHP脚本来读取这个feed并生成一个ATOM提供它(因为我有一个很好的读者可以处理HTTP Auth,看起来不错,但遗憾的是无法应对RSS 1.0)。

谷歌搜索了一段时间,我几乎找不到很多东西。这是我现在尝试的代码,但是XSLT不起作用,我对XSLT一无所知,我从here得到了它。获得HTTP Basic Auth已经有效了,但我会留在那里:

$https_user = "thisismyhttpbasicusername";
$https_password = "thisismyhttpbasicpassword";
$https_server = "sometld.tld/dokuwiki/feed.php";

$opts = array('http' =>
  array(
    'method'  => 'GET',
    'header'  => "Content-Type: text/xml\r\n".
      "Authorization: Basic ".base64_encode("$https_user:$https_password")."\r\n",
    'content' => $body,
    'timeout' => 60
  )
);

$context  = stream_context_create($opts);
$url = 'http://'.$https_server;
$xml = file_get_contents($url, false, $context, -1, 40000);
$xsl = file_get_contents("http://sometld.tld/minitools/rdf2atom.xslt");
$xslDoc = new DOMDocument();
$xslDoc->loadXML($xsl);
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml);
$proc = new XSLTProcessor();
$proc->importStylesheet($xslDoc);
echo $proc->transformToXML($xmlDoc);

这是XSLT文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<items>
  <xsl:copy-of select="//item">
    <xsl:apply-templates/>
  </xsl:copy-of>
</items>
</xsl:template>
</xsl:stylesheet>

输出应该是所有元素,所以我可以用一个元素包装它们,并让它不再处理RSS 1.0的RSS阅读器。

系统生成的RSS如下所示:

<rdf:RDF
   xmlns="http://purl.org/rss/1.0/"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel> ... </channel>
  <item rdf:about="http://sometld.tld/dokuwiki/doku.php?id=interessante_und_hilfreiche_links&amp;rev=1368016122&amp;do=diff">
    <dc:format>text/html</dc:format>
    <dc:date>2013-05-08T14:28:42+02:00</dc:date>
    <dc:creator>akku</dc:creator>
    <title>interessante_und_hilfreiche_links</title>
    <link>http://sometld.tld/dokuwiki/doku.php?id=interessante_und_hilfreiche_links&amp;rev=1368016122&amp;do=diff</link>
    <description>
*  .NET Framework Setup Verification Tool &lt;- This .NET Framework setup verification tool is designed to automatically perform a set of steps to verify the installation state of one or more versions of the .NET Framework on a computer.  It will verify the presence of files, directories, registry keys and values for the .NET Framework.  It will also verify that simple applications that use the .NET Framework can be run correctly.</description>
  </item>
  <item>... more items ... </item>
</rdf:RDF>

您是否知道基于PHP的脚本可以将RSS 1.0转换为Atom格式的Feed?或者你可以纠正我使用的XSLT吗?作为参考,现在的实际输出如下所示:

<?xml version="1.0"?>
<items/>

1 个答案:

答案 0 :(得分:1)

这很可能是命名空间问题。 尝试添加:

xmlns="http://purl.org/rss/1.0/"

作为xslt样式表的命名空间。

例如以下xslt:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:rss="http://purl.org/rss/1.0/">
    <xsl:template match="/">
        <items>
            <xsl:copy-of select="//rss:item" />
        </items>
    </xsl:template>
</xsl:stylesheet>

将生成以下输出:

<?xml version="1.0"?>
<items xmlns:rss="http://purl.org/rss/1.0/">
    <item xmlns="http://purl.org/rss/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" rdf:about="http://sometld.tld/dokuwiki/doku.php?id=interessante_und_hilfreiche_links&amp;rev=1368016122&amp;do=diff">
        <dc:format>text/html</dc:format>
        <dc:date>2013-05-08T14:28:42+02:00</dc:date>
        <dc:creator>akku</dc:creator>
        <title>interessante_und_hilfreiche_links</title>
        <link>http://sometld.tld/dokuwiki/doku.php?id=interessante_und_hilfreiche_links&amp;rev=1368016122&amp;do=diff</link>
        <description>
            *  .NET Framework Setup Verification Tool &lt;- This .NET Framework setup verification tool is designed to automatically perform a set of steps to verify the installation state of one or more versions of the .NET Framework on a computer.  It will verify the presence of files, directories, registry keys and values for the .NET Framework.  It will also verify that simple applications that use the .NET Framework can be run correctly.
        </description>
    </item>
    <item xmlns="http://purl.org/rss/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/">... more items ... </item>
</items>