2013-05-14 87 views
0

由於我的遺留系統產生了一個基於舊的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/> 

回答

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

事實上,這是問題。非常感謝。 – Akku 2013-05-14 09:58:52