PDA

View Full Version : Displaying XML feed on website



Mike
19-08-2004, 10:12 PM
At the moment this is a very broad question, but it might develop into something more specific :)

Say I wanted to display an XML feed on my website... how would I do it? I can't just point at the xml URL, as you just get a big mess of text and tags :) so how do I make that fairly readable and also fit into the content/style/look of my page.

Mainly I'm wanting to know how to display it within my page - the look and feel can come later once the basics make sense :p

My site is hosted on an apache server (I think) so can use PHP.

Thanks,

Mike.

stu120404
20-08-2004, 04:15 PM
** Bump :) **

Mike
20-08-2004, 06:50 PM
Thanks Stu ;)

I'm guessing nobody here knows anything about XML. I've got a few books out of the library, so I'll see if I can figure something out...

but as always, I'll still be waiting for a reply here :D

Mike.

bmason
20-08-2004, 08:25 PM
Have a look at XSLT.

You will want a transform that converts your XML to HTML.
I'm sure PHP will support it and there should be plenty of examples out there.

I use it a bit at work so post back if you get stuck. Although I still cling to a very good orielly book (which also includes an example to do what you want).

Mike
20-08-2004, 08:44 PM
> Have a look at XSLT.

What am I looking for?

> You will want a transform that converts your XML to
> HTML.
> I'm sure PHP will support it and there should be
> plenty of examples out there.

From what I can tell PHP doesn't support it natively - it's an addon extension, and not something I can use :(

> I use it a bit at work so post back if you get stuck.
> Although I still cling to a very good orielly book
> (which also includes an example to do what you
> want).

If you can give some pointers at what to do for starters that'd be great :D

Mike.

bmason
21-08-2004, 08:23 PM
For a basic into to XSLT have a look at the XSLT tutorial on Zvon (http://www.zvon.org/). For testing I recommend getting a command line tool. If you run windows grab MSXSL, its the easiest to install. Otherwise you can use either Xalan or saxon.

I've made an [awful] example below. Provided you don't need to include the rendered XML as part of another page it should be all you need to do. The XSLT is run in the visitors browser (for IE6, mozilla, opera etc) so you don't need to do anything on the server. The example is hosted on my paradise account so its just static content.

The XML is here (http://homepages.paradise.net.nz/bamason/tmp/test.xml). It should render as HTML in your browser but if you view the source you will see its still just XML.
The style sheet it uses to render the page is here (http://homepages.paradise.net.nz/bamason/tmp/test.xsl).
I don't think there is a way to view the resulting HTML, so I've put a copy of the output from running it with Xalan here (http://homepages.paradise.net.nz/bamason/tmp/testout.html), which should look the same as the XML above.

The magic is the second line in the XML which tells the browser where to find the stylesheet to use.
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
If you use this method you will need to make sure a similar line appears in the XML you want to display.


If you post some more info about what you want to do and where the XML is coming from I might be able to post something more specific.

bmason
21-08-2004, 08:29 PM
Oops, just saw your origional post.

You will need to do something with PHP, or some other scripting stuff, if you want the XML to be rendered as part of another page (unless you do something with frames).

Check out which version of PHP you have and check just incase XSLT was included with it.

My exaple should still be good as a way to have a play with XSLT to see what can be done.

Mike
21-08-2004, 09:10 PM
> The magic is the second line in the XML which tells
> the browser where to find the stylesheet to use.
> <?xml-stylesheet type="text/xsl"
> href="test.xsl"?>
> If you use this method you will need to make sure a
> similar line appears in the XML you want to display.

I am getting the XML feed from elsewhere, so cannot specify where to find the stylesheet within the XML (otherwise this exercise would probably be a lot easier) :)

Here is the first 3 lines of the XML feed:<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?>
<feed version="0.3" xmlns="http://purl.org/atom/ns#"> I am trying to figure out how to pull an XML atom feed (in this case from blogger.com) and display it in a webpage, but because the stylesheet points somewhere else (and displays rather plainly) its making it rather difficult :(.

Mike.
Mike.

bmason
21-08-2004, 10:00 PM
OK then I can think of two ways around it.

1)
Set up a simple PHP page that grabs the atom feed.
Then using PHP replace any existing reference to a stylesheet with your own using a simple string replace, regular expression, or parsing the XML.
And finally serve teh resulting XML to the client.

Advantages: Its simple. Doesn't need XSLT support in PHP.
Disadvantages: The XML feed would still need to be displayed on a separate page.

2)
Use PHP to run your own XSLT transform on the XML (ignoring the existing style sheet).

Advantages: You will be able to embed it in another page. No need to modify the feed itself.
Disadvantages: Have to run the transform on your server. Might not be possible if you PHP doesn't support XSLT.



Since its just a standard Atom feed there are probably existing transforms available which you will be able to start with for the XSLT.
And there are probably also examples around showing how to run XSLT using PHP. I don't know it so I can't help.



What kind of hosting are you using? Can you get PHP XSLT installed? If it is linux (and not too restrictive) it may be possible to use something other than PHP to run the XSLT (eg perl with or without mod-perl , or even bash).

Mike
21-08-2004, 10:26 PM
Thanks for your suggestions Brett,

My host is a free host, so I'm unlikely to get anything new added, nor do I know how to find out what's already installed. I guess that happens with free hosts :D

Your suggestion 1 might work, if I can figure out how to do it :) I guess now I need to learn some PHP (back to the library I suppose :)). If I can somehow get it to use my own stylesheet, I can possibly then pull whatever is produced into an existing page anyway (maybe?).

Perl might be an option, but I'm even more blank with perl than I am with PHP or XML :D

Thanks for your help - if you think of anything else I'm always open to more suggestions :)

I might have a chat with the web developer at work and see if he has any suggestions... although he mainly uses .NET/ASP etc. so he might not have many suggestions when it comes to the PHP... but he does work a lot with XML so we'll see.

At the moment this is just a hobbie of sorts - I see places offering XML feeds all the time so I was trying to see how it was used.

Cheers,

Mike.