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

问题描述:

由于我的遗留系统产生了一个基于旧的RDF的RSS 1.0 Feed,并且大多数RSS阅读器无法处理HTTP Basic Auth, d喜欢有一个PHP脚本来读取这个提要,并从中产生一个ATOM提要(因为我在这里有一个很好的读者,可以处理HTTP Auth,看起来不错,但很遗憾不能应对RSS 1.0)。在PHP中,从基于RDF的RSS源创建一个ATOM Feed

谷歌搜索了一段时间,我几乎没有找到很多。这是我现在尝试的代码,但XSLT不起作用,我对XSLT一无所知),我从here得到它。获取后面的HTTP基本认证已经工作,但我会离开它在那里:

$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阅读器不能处理RSS 1.0了。

由该系统产生的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格式的饲料?或者你可以校正我使用的XSLT?作为参考,现在的实际输出如下所示:

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

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

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> 
+0

事实上,这是问题。非常感谢。 – Akku 2013-05-14 09:58:52