duanchensou8685 2013-05-14 08:43
浏览 132
已采纳

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

Because of a legacy system I have that produces an old RDF based RSS 1.0 Feed, and the inability of most of the RSS Readers to handle HTTP Basic Auth, I'd like to have a PHP script that reads this feed and produces an ATOM feed from it (as I have a nice reader here that can handle HTTP Auth, looks nice, but that sadly cannot cope with RSS 1.0).

Googling around for some time, I pretty much didn't find a lot. This is the code I tried right now, but the XSLT doesn't work, and I don't know anything about XSLT), and I got it from here. Getting behind the HTTP Basic Auth already worked, but I'll leave it in there:

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

$opts = array('http' =>
  array(
    'method'  => 'GET',
    'header'  => "Content-Type: text/xml
".
      "Authorization: Basic ".base64_encode("$https_user:$https_password")."
",
    '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);

This is the XSLT file:

<?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>

The output should be all elements, so I can wrap them with a element and have it be read by an RSS Reader that doesn't handle RSS 1.0 anymore.

The RSS that is produced by the system looks like this:

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

Do you know a PHP based script that can transform the RSS 1.0 to an Atom formatted feed? Or can you correctify the XSLT I use? For reference, the actual output right now looks like this:

<?xml version="1.0"?>
<items/>
  • 写回答

1条回答 默认 最新

  • dsljpwi494719 2013-05-14 09:15
    关注

    This is most probably an namespace issue. Try to add:

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

    as namespace to your xslt stylesheet.

    For example the following 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>
    

    Will generate the following output:

    <?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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数电几道习题,写出作答过程
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52
  • ¥100 只改动本课件的 cal_portfolio_weight_series(decision_date), 跑完本课件。设计一个信息比率尽量高的策略。
  • ¥20 如何在visual studio 2022中添加ImageMagick库
  • ¥50 如何实现uniapp编译的微信小程序做可回溯视频
  • ¥15 求Houdini使用行家,付费。价格面议。
  • ¥15 前端高拍仪调用问题报错