HOME       >>       Programming

Rss And Xhtml From The Same Data A simple way


FirefoxRocks

You may have heard of methods to transform RSS feeds into XHTML by the means of an XSLT stylesheet, but there are a few problems with that method that I've found. I've encountered that if you choose to publish RSS, it won't read the XSLT stylesheet for some strange reason, and if you get it to output XHTML, it doesn't detect the RSS.

 

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

 

It isn't that hard and I will go through both of these with you throughout this tutorial.

 

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

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):

 

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://www.w3schools.com/xml/xsl_intro.asp.

 

Here is the code (explained at the bottom):

 

feed_xhtml.xsl

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:

 

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//?gtnjs=1. 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:

 

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!



VIEW DESKTOP VERSION REGISTERGET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.