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 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突