Guest User

Solution to http://stackoverflow.com/questions/3056990/transform-rss-feed-into-another-standard-xml-format-with-php/3057592#3057592

a guest
Jun 17th, 2010
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.16 KB | None | 0 0
  1. <?php error_reporting(-1);
  2.  
  3. $xml = <<< XML
  4. <?xml version="1.0" encoding="UTF-8"?>
  5.     <rss version="2.0">
  6.         <channel>
  7.             <title>Name des RSS Feed</title>
  8.             <description>Feed Beschreibung</description>
  9.             <language>de</language>
  10.             <link>http://xml-rss.de</link>
  11.             <lastBuildDate>Sat, 1 Jan 2000 00:00:00 GMT</lastBuildDate>
  12.             <item>
  13.                 <title>Titel der Nachricht</title>
  14.                 <description>Die Nachricht an sich</description>
  15.                 <link>http://xml-rss.de/link-zur-nachricht.htm</link>
  16.                 <pubDate>Sat, 1. Jan 2000 00:00:00 GMT</pubDate>
  17.                 <guid>01012000-000000</guid>
  18.             </item>
  19.             <item>
  20.                 <title>Titel der Nachricht</title>
  21.                 <description>Die Nachricht an sich</description>
  22.                 <link>http://xml-rss.de/link-zur-nachricht.htm</link>
  23.                 <pubDate>Sat, 1. Jan 2000 00:00:00 GMT</pubDate>
  24.                 <guid>01012000-000000</guid>
  25.             </item>
  26.             <item>
  27.                 <title>Titel der Nachricht</title>
  28.                 <description>Die Nachricht an sich</description>
  29.                 <link>http://xml-rss.de/link-zur-nachricht.htm</link>
  30.                 <pubDate>Sat, 1. Jan 2000 00:00:00 GMT</pubDate>
  31.                 <guid>01012000-000000</guid>
  32.             </item>
  33.         </channel>
  34.     </rss>
  35. XML;
  36.  
  37. $xsl = <<< XSL
  38. <?xml version="1.0" encoding="ISO-8859-1"?>
  39. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  40. <xsl:template match="/">
  41. <items>
  42.   <xsl:copy-of select="//item">
  43.     <xsl:apply-templates/>
  44.   </xsl:copy-of>
  45. </items>
  46. </xsl:template>
  47. </xsl:stylesheet>
  48. XSL;
  49.  
  50.    $xslDoc = new DOMDocument();
  51.    $xslDoc->loadXML($xsl);
  52.  
  53.    $xmlDoc = new DOMDocument();
  54.    $xmlDoc->loadXML($xml);
  55.  
  56.    $proc = new XSLTProcessor();
  57.    $proc->importStylesheet($xslDoc);
  58.    echo $proc->transformToXML($xmlDoc);
  59.  
  60. /** output
  61.  
  62. X-Powered-By: PHP/5.3.2
  63. Set-Cookie: ZDEDebuggerPresent=php,phtml,php3; path=/
  64. Set-Cookie: ZendDebuggerCookie=127.0.0.1%3A10137%3A0||084|77742D65|1000; path=/
  65. Content-type: text/html
  66.  
  67. <?xml version="1.0"?>
  68. <items><item>
  69.                 <title>Titel der Nachricht</title>
  70.                 <description>Die Nachricht an sich</description>
  71.                 <link>http://xml-rss.de/link-zur-nachricht.htm</link>
  72.                 <pubDate>Sat, 1. Jan 2000 00:00:00 GMT</pubDate>
  73.                 <guid>01012000-000000</guid>
  74.             </item><item>
  75.                 <title>Titel der Nachricht</title>
  76.                 <description>Die Nachricht an sich</description>
  77.                 <link>http://xml-rss.de/link-zur-nachricht.htm</link>
  78.                 <pubDate>Sat, 1. Jan 2000 00:00:00 GMT</pubDate>
  79.                 <guid>01012000-000000</guid>
  80.             </item><item>
  81.                 <title>Titel der Nachricht</title>
  82.                 <description>Die Nachricht an sich</description>
  83.                 <link>http://xml-rss.de/link-zur-nachricht.htm</link>
  84.                 <pubDate>Sat, 1. Jan 2000 00:00:00 GMT</pubDate>
  85.                 <guid>01012000-000000</guid>
  86.             </item></items>
  87.  
  88.  
  89.  
  90.  
  91. */
Add Comment
Please, Sign In to add comment