Page 1 of 1

Parsing RSS Feeds...

Posted: Fri Aug 29, 2003 10:06 am
by Gonik
Hello ppl.. I'm working on a project right now and i want to parse some rss feeds and display them.

Can you please give me some EASY examples on how to parse and format them the way i want? Please use simple functions because i'm not "comfortable" with classes, yet. Still learning :)

Thanx in advance for any reply / resource site etc.,
Nick

Posted: Fri Aug 29, 2003 10:37 am
by volka
I'm not sure wether you find yourself more comfortable with xslt but it's a good choice to handle xml-input. e.g.

Code: Select all

<?php
// a simple stylesheet to process rdf
$xsl = <<<EOX
<xsl:stylesheet
	version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	xmlns:rss="http://my.netscape.com/rdf/simple/0.9/"
>
	<xsl:output method="html" />
	<xsl:template match="/">
		<xsl:apply-templates  />
	</xsl:template>
	
  <xsl:template match="rdf:RDF">
 		<div class="feed">
 			<xsl:apply-templates />
 		</div>
  </xsl:template>

	<xsl:template match="rss:channel">
		<div>
			<span class="channel">
				<xsl:value-of select="rss:title" />
			</span>
			<span class="description">
				<xsl:value-of select="rss:description" />
			</span>
		</div>
	</xsl:template>
	
	<xsl:template match="rss:item[position() mod 2=0]">
		<a>
			<xsl:attribute name="class">itemlink_even</xsl:attribute>
			<xsl:attribute name="href">
				<xsl:value-of select="rss:link" />
			</xsl:attribute>
			<xsl:value-of select="rss:title" />
		</a>
	</xsl:template>
	<xsl:template match="rss:item[position() mod 2=1]">
		<a>
			<xsl:attribute name="class">itemlink_odd</xsl:attribute>
			<xsl:attribute name="href">
				<xsl:value-of select="rss:link" />
			</xsl:attribute>
			<xsl:value-of select="rss:title" />
		</a>
	</xsl:template>

	<xsl:template match="*">
		<fieldset>
			<xsl:value-of select="name()" />
			<br/>
			<xsl:value-of select="namespace-uri()" />
		</fieldset>
	</xsl:template>

</xsl:stylesheet>
EOX;


$rdf = file_get_contents('http://www.devnetwork.net/service/forum_lastposts.php?limit=10&client=klip');
$arguments = array(
     '/_xml' => $rdf,
     '/_xsl' => $xsl
);

$xh = xslt_create();
$result = xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments);

echo $result;
?>
http://www.w3.org/Style/XSL/
every day something new to learn ;)

Posted: Fri Aug 29, 2003 11:03 am
by Gonik
thanx for the quick reply. I have some knowledge on XML/XSL, but I get this error..
Parse error: parse error, unexpected $end in C:\01\php\rssparser.php on line 73
what can be wrong?

Posted: Fri Aug 29, 2003 12:46 pm
by volka
hm, I don't get this error here
try it without the heredoc-string simply setting $xsl='test';
You will get an xslt-error but ...
having the xsl within the php-file isn't such a good idea anyway. It's just an example ;)