views:

403

answers:

3

I use XML and XSLT in my current project and I would like to know if it's good to let the browser render the XML into HTML with stylesheet instead to use something like the PHP xsltprocessor.

One main reason I use the Browser xslt processor is to allows API to access my XML data in a near future. So I want the transformation client-side so my XML is still available.

I might be wrong about the PHP xsltprocessor, but when a xml is processed via PHP , the data received is the rendered XML (in my case HTML) and the XML data is no longer available. Is that right?

Thank you for clearing things out.

+1  A: 

I recommend doing the transformation server-side because you have more control over it. If you rely on the browser you may see slight differences in the rendering engines in different browsers. If you transform on the server and pass down html then you can do cleaner testing by specifying exactly what html you want to generate, making sure a static version of that html looks correct, and then working on your XSLT to make sure it generates the html correctly.

To get the XML available on the client-side, in part of your transform you can output the sections of XML that you need (or the whole DOM) and put that either in a JavaScript variable or somewhere accessible on the page.

I've found that rendering client-side is nice for small, controlled (e.g. internal) uses but for anything where you want tight control over the output server-side is more reliable.

Peter Jacoby
I must admit that I had some trouble making the Javascript working in every major browsers using the Browser XSLT Rendering. But overall the HTML is rendered the same way so I think beside few Javascript strange behaviors, I still think client-side transformation is not so a bad decision. Is there any browser that do not support it at all ?
Steve Thomas
As far as I know all browsers support it, but you'll run into differences with how they handle extension functions or anything outside the basics of XSLT. It will probably work fine, but you can't really see what the end user experience will be so it depends on how much that matters to you.
Peter Jacoby
A: 

It all depends on what your end goal is. I work on an application that produces reports as xml and we send the xml and xslt to the browser to do the processing. There are a few reasons for this approach:

  • as you have already mentioned you can do further processing on the client
  • html is fairly verbose and if you have control of specifying your xml you can ensure that it isn't as verbose so in the end you send a small amount of data between the server and the browser
  • the xslt can be cached on the browser so won't be an overhead after the first time it is used
  • doing the transform on the server adds load to your server which can have an impact if you have many people trying to access the page at the same time.

In certain cases we do the transform on the server. e.g. we transform to an html table and then change the http content type to send the result as excel.

Andrew
A: 

If you design application with MVC pattern, then it won't matter. You'll be able to expose HTML to regular browsers and XML via API.

And even if you do XSLT on the server side, it should be fairly simple to skip that step if/when you add an API.

Usually XSLT on client-side has many disadvantages — high latency because both stylesheet and all data have to finish loading completely before anything starts rendering. With plain HTML you get lower latency (just one file, streamed) and additional resources start downloading even before HTML is finished.

porneL
So I guess its high latency versus server running time. But what if that stylesheet is browser cached ?
Steve Thomas
Even with stylesheet cached, you still have to wait until entire XML is downloaded before XSLT starts generating page. Unless your XML is much much smaller than HTML (compare gzipped sizes!) then it's still slower.
porneL