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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度