Forum Moderators: coopster
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.
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