So therefore, it is a complicated way to get that to work. Instead, this is a simpler way.
What you will need:
- An PHP/XML file
- An XSLT stylesheet
The PHP file
In the meantime, let's not think PHP, but let's think XML. Start typing out your XML file like an RSS feed so it looks something like this:
example.php
<rss version="2.0">
<channel>
<title>Website announcements</title>
<link>http://portal.trap17.com/example.php</link>
<description>Announcements related to this site.</description>
<language>en-CA</language>
<item>
<title>More Website Maintenance</title>
<pubDate>Fri, 21 Mar 2008</pubDate>
<description>New menus added. Because of the reduced site use and reduced info, more things are removed.</description>
</item>
<item>
<title>Website maintenance</title>
<pubDate>Wed, 26 Dec 2007</pubDate>
<description>Planned website maintenance for the next day or two.</description>
</item>
<item>
<title>Severe Server Issues</title>
<pubDate>Fri, 14 Dec 2007</pubDate>
<description>The server has been running on and off. Please standby as we try to correct this issue.</description>
</item>
</channel>
</rss>
To determine if the user wants to get RSS or XHTML, we will use a $_GET variable. I'll call this $type.
Next is the HTTP headers and the XML declaration. To put that in code, we will do use this PHP code (put it above the XML stuff, like this):
$type = $_GET['type'];
header("Content-Type: application/xml");
header("Content-Language: en-CA");
echo "<?xml version='1.0' encoding='utf-8'?>\n";
?>
<rss version="2.0">
<channel>
<title>Website announcements</title>
<link>http://portal.trap17.com/example.php</link>
<description>Announcements related to this site.</description>
<language>en-CA</language>
...the rest of the file...
</channel>
</rss>
The lines added about simply declared a variable called type, which I will explain in the next paragraph. The HTTP header Content-Type was added to ensure that the server sends the file as application/xml, the correct MIME type for an XML file. The second HTTP header is optional, which specifies the language as Canadian English. The XML PI is printed out using echo for the last line.
For the type variable, we can have 2 results: xhtml or rss. You may be wondering how to get users to submit a form to specify whether to get the file as RSS or as XHTML. For GET variables, that's not necessary to submit a form. The URL will look like this:
example.php?type=xhtml for XHTML
example.php?type=rss for an RSS feed
Let's put that file aside for now and build our XSLT file.
The XSLT file
The XSLT file is used to transform the XML document into readable XHTML. I will not explain the components of an XSLT file here, you can learn more from http://w3schools.com/xsl/.
Here is the code (explained at the bottom):
feed_xhtml.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output encoding="utf-8" method="html" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" omit-xml-declaration="yes" media-type="text/html"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-CA" lang="en-CA">
<head>
<title>Announcements</title>
<link rel="alternate" type="application/rss+xml" title="Announcements" href="feed.php?type=rss" />
</head>
<body>
<h1>Announcements</h1>
<p>On this page, you will find important announcements about the website.</p>
<xsl:for-each select="//item">
<h2><xsl:value-of select="title" /></h2>
<p><xsl:value-of select="pubDate" /> - <xsl:value-of select="description" /></p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The first 2 lines are default for XSLT files. The <xsl:output /> tag is required, although I'm not exactly sure of its function.
Within the <xsl:template> tag, we can see that it looks much like an (X)HTML file. Notice the <link /> tag. This will allow browsers (IE, Firefox, Opera and Safari) to detect an RSS version of this page.
Now in the <body> section, you can see a tag called <xsl:for-each>. What we are doing here is finding all the <item> elements in the XML file and as you can see, printing out the title, date and description of the item in <h2>s and <p>aragraphs.
This is a really basic XSLT template, you can add other HTML things like CSS and JavaScript and all that if you wish.
Let's go back and link this stylesheet to the original PHP file. We only want the stylesheet to be used when the user requests XHTML, so we can use the if function:
$type = $_GET['type'];
header("Content-Type: application/xml");
header("Content-Language: en-CA");
echo "<?xml version='1.0' encoding='utf-8'?>\n";
if($type=="xhtml")
{
echo "<?xml-stylesheet type='text/xsl' href='feed_xhtml.xsl'?>\n";
}
?>
<rss version="2.0">
<channel>
<title>Website announcements</title>
<link>http://portal.trap17.com/example.php</link>
<description>Announcements related to this site.</description>
<language>en-CA</language>
...the rest of the file...
</channel>
</rss>
The <?xml-stylesheet ?> thing is only used when the type is xhtml, it isn't used with other types.
Now you may visit this page, and no matter what variable you put in, it will still go to a page for the browser to subscribe you to the RSS feed. This is where PHP comes in handy here.
All XML documents require a root element, in this case, it was <rss>. If you don't know what a root element is, learn more about XML at http://w3schools.org/xml/. But in this case, it doesn't matter as long as it is the same.
If the user requests RSS, we will leave <rss version="2.0">, but if the user does not, we will change that.
The final file looks like this:
$type = $_GET['type'];
header("Content-Type: application/xml");
header("Content-Language: en-CA");
echo "<?xml version='1.0' encoding='utf-8'?>\n";
if($type=="xhtml")
{
echo "<?xml-stylesheet type='text/xsl' href='feed_xhtml.xsl'?>\n";
}
?>
<<?php if($type=="rss"){echo 'rss version="2.0"';}else{echo 'news';}?>>
<channel>
<title>Website announcements</title>
<link>http://portal.trap17.com/example.php</link>
<description>Announcements related to this site.</description>
<language>en-CA</language>
...the rest of the file...
</channel>
</<?php if($type=="rss"){echo 'rss';}else{echo 'news';}?>>
If the type isn't RSS, it will be <news>. You can choose any word here, but make sure that the opening and closing tags are the same.
Now to see the XHTML version of the file, go to example.php?type=xhtml
For the RSS feed, go to example.php?type=rss
I hope you understood this tutorial and had fun with it. Feel free to correct me on anything or to suggest improvements!

