php_newB Posted April 5, 2007 Share Posted April 5, 2007 I have this dynamic feed for my website, but I'd like to style it using xslt trouble is I don't know how!! I want to display every item as list, any idea how to do this <?php include 'dbconfig.php'; // Compile our header, since this will never need to change $compile .= "<xml varsion=\"1.0\">\n"; $compile .= "<rss version=\"2.0\">\n\n"; $compile .= "<channel>\n"; // Our query to get all the news information $result = mysql_query("SELECT * FROM news") or die(mysql_error()); // Loop out all the info from the database in the required format while($row = mysql_fetch_assoc($result)) { $compiled .= "<article_Title>{$row['title']}</article_Title>\n"; $compiled .= "<description>{$row['content']}</description>\n"; //$compiled .= "<link>http://www.domain.com/news.php?id={$row['id']}</link>\n\n"; } // close the database connection mysql_close(); // add the footer of our RSS document $compiled .= "</channel>\n"; $compiled .= "</rss>\n"; // Now send our document to the client echo($compiled); ?> Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/45719-format-php-feed-with-xslt/ Share on other sites More sharing options...
Barand Posted April 20, 2007 Share Posted April 20, 2007 I don't have your db so I hard coded a few articles (and changed the structure slightly) eg <article> <title>eee</title> <description>fffffff</description> </article> Main file <?php $compiled = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"; $compiled .= "<rss version=\"2.0\">"; $compiled .= "<channel>"; $compiled .= "<article>"; $compiled .= "<title>aaa</title>"; $compiled .= "<description>bbb</description>"; $compiled .= "</article>"; $compiled .= "<article>"; $compiled .= "<title>cccccc</title>"; $compiled .= "<description>ddddd</description>"; $compiled .= "</article>"; $compiled .= "<article>"; $compiled .= "<title>eee</title>"; $compiled .= "<description>fffffff</description>"; $compiled .= "</article>"; $compiled .= "<article>"; $compiled .= "<title>gggggggggg</title>"; $compiled .= "<description>hhhhhhhh</description>"; $compiled .= "</article>"; $compiled .= "</channel>"; $compiled .= "</rss>"; $xml = simpleXML_load_string($compiled); $xsl = simplexml_load_file('php_newb.xsl'); $proc = new XsltProcessor(); $proc->importStylesheet($xsl); $newxml = $proc->transformToDoc($xml); print $newxml->saveXML(); ?> and the "php_newb.xsl" file <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:apply-templates/> </xsl:template> <xsl:template match="channel"> <DIV> <table border="0" width="400"> <tr><th>Title</th><th>Description</th></tr> <xsl:apply-templates select="article"/> </table> </DIV> </xsl:template> <xsl:template match="article"> <tr> <td><xsl:apply-templates select="title"/></td> <td><xsl:apply-templates select="description"/></td> </tr> </xsl:template> <xsl:template match="title"> <span style="color: #0000FF"><xsl:value-of select="." /></span> </xsl:template> <xsl:template match="description"> <span style="color: #FF0000"><xsl:value-of select="." /></span> </xsl:template> </xsl:stylesheet> Quote Link to comment https://forums.phpfreaks.com/topic/45719-format-php-feed-with-xslt/#findComment-233661 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.