duanchensou8685
2013-05-14 08:43
浏览 131

在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>
    
    打赏 评论

相关推荐 更多相似问题