<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>My Beloved PHP &#187; XSL</title>
	<atom:link href="http://www.mybelovedphp.com/category/xsl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mybelovedphp.com</link>
	<description>The Art of Quick &#38; Dirty Programming</description>
	<lastBuildDate>Sun, 06 Jun 2010 21:08:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Parsing newsfeeds with XSL (1)</title>
		<link>http://www.mybelovedphp.com/2006/10/11/parsing-newsfeeds-with-xsl/</link>
		<comments>http://www.mybelovedphp.com/2006/10/11/parsing-newsfeeds-with-xsl/#comments</comments>
		<pubDate>Wed, 11 Oct 2006 10:23:39 +0000</pubDate>
		<dc:creator>programmer</dc:creator>
				<category><![CDATA[RSS]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XSL]]></category>

		<guid isPermaLink="false">http://mybelovedphp.com/?p=4</guid>
		<description><![CDATA[Magpie is a very popular RSS parser for PHP. Although I used Magpie quite a while to my satisfaction, it suffers a few flaws. At first it seems to have problems with the UTF8. Dealing with UTF8 is not easy in PHP (and MySQL). And there is verbose code, you feel it can be done [...]]]></description>
			<content:encoded><![CDATA[<p>Magpie is a very popular RSS parser for PHP. Although I used Magpie quite a while to my satisfaction, it suffers a few flaws. At first it seems to have problems with the UTF8. Dealing with UTF8 is not easy in PHP (and MySQL). And there is verbose code, you feel it can be done quicker and simplier. We will parse feeds with only 5 lines of code  and a simple stylesheet.<br />
<span id="more-4"></span><br />
So why not create an RSS feed ourselves. We will use XSL transformations (XSLT). XSL is developed to manipulate XML documents and that is exactly what we want. Remember RSS is a short and simple XML-document.</p>
<p>XSL is a powerful language but a little counterintuitive. You can do a lot with surprisingly little code, but sometimes it will cost you hours and you won&#8217;t work it out. </p>
<p>To use XSL parsing you need to have the libxslt compiled into PHP.</p>
<p>You transform XML document by applying the XSLT transform with the XSLT stylesheet.</p>
<p>In php:</p>
<pre class="brush: xslt;">

// Load XSL
$xsl = newDOMDocument; $xsl-&gt;load('stylesheet.xsl');

// Create new XSLTProcessor
$xslt = new XSLTProcessor();
// Load stylesheet
$xslt-&gt;importStylesheet($xsl);

// Load XML-document
$feed = file_get_contents('http://.../yourRSSfeed.xml')
$xml = new DOMDocument; $xml-&gt;loadXML($feed);

// Transform into outputstring
echo $results = $xslt-&gt;transformToXML($xml);
</pre>
<p>The xml is your RSS-feed, that is easy.<br />
<script type="text/javascript"><!--
google_ad_client = "pub-4607392855246580";
google_ad_width = 336;
google_ad_height = 280;
google_ad_format = "336x280_as";
google_ad_type = "text";
//2006-10-13: php_post
google_ad_channel = "0383969095";
google_color_border = "FFFFFF";
google_color_bg = "FDFAFE";
google_color_link = "2B6EA2";
google_color_text = "CCCCCC";
google_color_url = "2B6EA2";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
The XSLT stylesheet has to look like this. You start by telling what kind of file it is.</p>
<pre class="brush: xslt;">
&lt;xsl :stylesheet version=&quot;1.0&quot;
   xmlns:xsl = &quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
      &lt;xsl <img src='http://www.mybelovedphp.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> utput method=&quot;xml&quot;
      omit-xml-declaration=&quot;yes&quot;/&gt;
&lt;/xsl&gt;
</pre>
<p>Then we code which nodes to transform, look for an rss root tag:<br />
&lt;code&gt;&lt;xsl :template match=&quot;/rss&quot;&gt;&lt;/xsl&gt;&lt;/code&gt;</p>
<p>and do this print list of all items:<br />
&lt;pre&gt;&lt;p id=&quot;MyFeed&quot;&gt;&#8230;.&lt;/p&gt;&lt;/pre&gt;<br />
&lt;pre&gt; &lt;xsl :for-each select=&quot;//item&quot;&gt;&lt;/xsl&gt;&lt;/pre&gt;</p>
<p>&lt;pre&gt;<br />
&lt;a href=&quot;{link}&quot;&gt;&lt;xsl :value-of select=&quot;title&quot; /&gt;&lt;/a&gt;<br />
&lt;/pre&gt;<br />
Value of select , selects the textcontent of the title tag.<br />
{link} is the same but a shortcut for easy use between quotation marks.<br />
With title of item als text and link-url as url.</p>
<p>Complete xsl-stylesheet:</p>
<pre class="brush: xslt;">
&lt;xsl :stylesheet version=&quot;1.0&quot;
     xmlns:xsl = &quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
  &lt;xsl <img src='http://www.mybelovedphp.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> utput method=&quot;xml&quot;
     omit-xml-declaration=&quot;yes&quot;/&gt;&lt;/xsl&gt;&lt;xsl :template match=&quot;/rss&quot;&gt;
  &lt;ul id=&quot;nieuws&quot;&gt;
  &lt;xsl :for-each select=&quot;//item&quot;&gt;
   &lt;li class=&quot;style&quot;&gt;
       &lt;a href=&quot;{link}&quot;&gt;
  &lt;xsl :value-of select=&quot;title&quot; /&gt;&lt;/a&gt;
   &lt;/li&gt;
  &lt;/xsl&gt;
 &lt;/ul&gt;
&lt;/xsl&gt;  
</pre>
<p>Save this to your directory as stylesheet.xsl and run the earlier phpcode.<br />
This will give you a simple list with url and names of the posts.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mybelovedphp.com/2006/10/11/parsing-newsfeeds-with-xsl/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
