View Full Version : transforming XML/XSL with PHP?
bphein1980
11-03-2005, 11:19 PM
I have been using javascript to transform XML and XSL docs. I would like to move the processing to the server using PHP...
I know very little about PHP and found this script, but am not sure how to use it.
How would I send different XSL docs and XML docs to this page for processing?
<?php
$xml = get_xml_from_datasource();
$xsl = get_xsl_from_templatelibrary();
$arguments = array(
'/_xml' => $xml,
'/_xsl' => $xsl
);
$xp = xslt_create();
$result = xslt_process($xp, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments);
echo $result;
xslt_free($xp);
?>
Previously, in javascript, I could use a link that looked like this:
<a href="javascript:transform('whatever.xml','style.xsl')">Something</a>
As you can see, I can send whichever XML doc and whichever XSL doc I want to the script for processing. Does the PHP code above work the same, and how does it work?
Thanks.
firepages
11-04-2005, 02:34 AM
get_xml_from_datasource && get_xsl_from_templatelibrary are user functions not PHP functions and probably just load an external file into a variable?
from the same page you probably got that code from ? ( http://www.dotvoid.com/view.php?id=14 )...
<?php
$xp = xslt_create();
$result = xslt_process($xp, 'mylibrary.xml', 'libraryhtml.xsl');
echo $result;
xslt_free($xp);
?>
bphein1980
11-04-2005, 03:29 AM
hmmm.. I see. Thank you for the reply.
Basically I am working without a database. Currently, I just have XML and XSL files that are transformed through javascript by sending the XML and XSL document path through a link... i can tell the javascript to use a different XSL doc and/or XML doc.
Is PHP able to do something similar for tranforming:
<a href="javascript:transform('whatever.xml','style.xsl')">Something</a>
Is there a PHP equivelant to that?
Yes, that is the website that I found the code on. I am just getting into server side stuff, so I know very little. The code that I originally posted looked as though I could dynamically send which XML and XSL documents to be transformed.
Can it be done this way? Or do I need to hardcode each page to specify which XSL and XML doc to be transformed? I hope this all makes sense. What I am hoping to have is one PHP page that will be able to transform any of the XML and XSL docs I send to it.
For example, here is the javascript that I use (not my original):
var xslStylesheet;
var xsltProcessor = new XSLTProcessor();
var myDOM;
var xmlDoc;
function transform(source,stylesheet)
{
if(document.implementation && document.implementation.createDocument)
{
// load the xslt file
var myXMLHTTPRequest = new XMLHttpRequest();
myXMLHTTPRequest.open("GET", stylesheet, false);
myXMLHTTPRequest.send(null);
xslStylesheet = myXMLHTTPRequest.responseXML;
xsltProcessor.importStylesheet(xslStylesheet);
// load the xml file
myXMLHTTPRequest = new XMLHttpRequest();
myXMLHTTPRequest.open("GET", source, false);
myXMLHTTPRequest.send(null);
xmlDoc = myXMLHTTPRequest.responseXML;
var fragment = xsltProcessor.transformToFragment(xmlDoc, document);
document.getElementById("target").innerHTML = "";
myDOM = fragment;
document.getElementById("target").appendChild(fragment);
}
else if(window.ActiveXObject)
{
// IE
// Load XML
xml = new ActiveXObject("MSXML2.DOMDocument");
xml.async = false
xml.load(source)
// Load XSL
xsl = new ActiveXObject("MSXML2.DOMDocument");
xsl.async = false
xsl.load(stylesheet)
// Transform
document.getElementById("target").innerHTML=xml.transformNode(xsl);
}
else
{
// Browser unknown
alert("Browser unknown");
}
}
As you can see, I just use the javascript function to call which XML and XSL document to process, then it outputs it to the "target" area.
firepages
11-04-2005, 04:21 PM
<?php //process.php
$xp = xslt_create();
$result = xslt_process( $xp , "/files/xsl/{$_REQUEST['xml']}.xml" , /files/xsl/{$_REQUEST['xsl'].xsl}" ) ;
echo $result;
xslt_free($xp);
?>
its exactly the same as your javascript function , you pass it an xml filepath and an xsl filepath , ithe above could for instance be called with http:// domain.com /process.php?xml=test&xsl=test
bphein1980
11-04-2005, 05:54 PM
Thank you for the reply. Let me see if I got this straight...
transform.php
<?php
$xp = xslt_create();
$result = xslt_process( $xp , "/{$_REQUEST['xml']}.xml" , "/{$_REQUEST['xsl'].xsl}" ) ;
echo $result;
xslt_free($xp);
?>
index.html
<html>
<body>
<a href="http://www.whatever.com/process.php?xml=test&xsl=test">link</a>
</body>
</html>
So, by loading the index.html page and clicking the "Link", I should get a new page that is the transformation of test.xml and test.xsl? (all the files are located in the root folder).
Am I using the code correctly, or am I going at this completey wrong?
bphein1980
11-04-2005, 07:59 PM
Thank you very much Firepages! I got it working. The help is much appreciated!
Here is how I am using it:
<?php
$xp = xslt_create();
$result = xslt_process( $xp , "{$_REQUEST['xml']}.xml" , "{$_REQUEST['xsl']}.xsl" ) ;
echo $result;
xslt_free($xp);
?>
Linking like this:
<a ref="http://www.domain.com/transform.php?xml=test&xsl=test">link3</a>
Works great! Thank you!
Fou-Lu
11-05-2005, 08:59 AM
Just a heads up for you as well;
Never rely on $_REQUEST as being valid information. As in, the client could change your request variables in exchange for their own files. My recommendation would to simply compare your $_REQUEST variables for their existance in your filepath, and I would also only allow files from a specific path as well. You don't want the user importing something else that you do not want them to see.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.