Forum Moderators: coopster

Message Too Old, No Replies

XSL and PHP - beginner help needed

How do I have xsl transformations applied server-side using PHP?

         

Stressed Oot

12:22 pm on Jun 30, 2008 (gmt 0)

10+ Year Member



Hi,

I have a site serving a range of xml documents which are transformed to xhtml with corresponding xsl sheets. At present this transformation is performed client-side by the browser.

I'm interested in setting up a PHP script to intercept requests for xml pages and have these transformations performed server-side.
The server in question is PHP 5 enabled.

I'm very new to PHP but I have experimented with the following script which successfully performs the required transformation when I direct my browser to it.

<?php

$path_xml = "test01.xml";
$path_style = test01.xsl";

$xml_obj = new DomDocument;
$xsl_obj = new DomDocument;

if (!$xml_obj->load($path_xml)) {
echo "Error! Unable to open " . $path_xml . "!\n";
exit;
}

if (!$xsl_obj->load($path_style)) {
echo "Error! Unable to open " . $path_style . "!\n";
exit;
}

$xslt_parse = new xsltprocessor;

$xslt_parse->importStyleSheet($xsl_obj);

echo $xslt_parse->transformToXML($xml_obj);

?>

However, I'm still pretty confused as to how I would go about applying this process to all of my xml documents without writing a similar script for each individual document and then rewriting all of the existing links which refer to these documents.

Obviously the less I have to disturb the current setup to achieve the server-side transformation the better.

As I mentioned above I'm very new to PHP so don't be shy about spelling it out to me.
Any help appreciated.

WesleyC

1:11 pm on Jun 30, 2008 (gmt 0)

10+ Year Member



Is your server running Apache? If so, use a .htaccess file and write a regular expression to do a permanent (302) redirect from *.xml to myxmlscript.php?file=*.xml . Then, use $_GET['file'] (or whatever you name it) to find the file, parse it, and spit it back out
.

httpwebwitch

5:39 am on Jul 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



fyi a permanent redirect is 301, not 302
getting them mixed up is potentially fatal

tomda

7:23 am on Jul 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And do not forget to check your variable and make it safe against URL injection.

httpwebwitch

3:03 pm on Jul 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



another suggestion: don't serve HTML to URIs that once served XML. That will annoy the web. If something is "myfile.xml", it should be XML, not something pre-transformed by the server into something else.

So instead, leave the XML versions where they are, and offer a parallel set of URLs, served as HTML

mySpecialDocument.xml <- XML, with a client-side XSLT and CSS. Stays Put.
mySpecialDocument.html <- a new page, HTML, transformed on the server with PHP + XSLT.

The code that follows is adlib, untested, pseudocode, but should show you what I'm thinking

set up a single rule in your htaccess:

RewriteRule (.*).html /transformer.php?filename=$1&xslt=html

Then create your PHP transformer:


$filename = $_GET['filename'];
$xslt = "/xslt/" . $_GET['xslt'] . ".xslt";
$output = dotransform($filename,$xslt); // for this substitite the code you already have, which works
echo $output;

This technique is very effective. I have a web service that runs exactly like this which offers the same data in a variety of flavours:

example.com/example.xml <- this is the script that actually does the Data Access and parsing

the others are all derivations of the XML service, each using a different XSLT:
example.com/example.html (transforms example.xml into HTML)
example.com/example.js (transforms example.xml into executable Javascript)
example.com/example.json (transforms example.xml into a JSON object)
example.com/example.csv (transforms example.xml into a comma-separated table)
example.com/example.txt (transforms example.xml into a monospaced text layout)

and your htaccess will contain multiple rewriting rules


RewriteRule (.*).html /transformer.php?filename=$1&xslt=html
RewriteRule (.*).js /transformer.php?filename=$1&xslt=js
RewriteRule (.*).json /transformer.php?filename=$1&xslt=json
RewriteRule (.*).csv /transformer.php?filename=$1&xslt=csv
RewriteRule (.*).txt /transformer.php?filename=$1&xslt=txt