Page 1 of 1

XML

Posted: Wed Dec 09, 2009 12:13 pm
by st89
Is there a way to transform XML with XSLT on the server using PHP? I'm using a free web tutorial (you get what you pay for) and it's lacking to say the least. I've searched Google and tried a variety of solutions that all fail with fatal errors. The ASP code given for XML to XSLT transformation on the server is below:

Code: Select all

<%
'Load XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("cdcatalog.xml"))
 
'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("cdcatalog.xsl"))
 
'Transform file
Response.Write(xml.transformNode(xsl))
%>
I'd be really grateful if someone could point me to a resource for XML with XSLT creation on the server with PHP. Thanks in advance for any help/tips/pointers :D

Re: XML

Posted: Wed Dec 09, 2009 12:42 pm
by AbraCadaver
Check: http://us3.php.net/manual/en/book.xsl.php and: http://us3.php.net/manual/en/book.dom.php

Example here: http://us3.php.net/manual/en/xsltproces ... mtoxml.php

Code: Select all

<?php
// LoadXML
$xml = new DomDocument;
$xml->load('cdcatalog.xml');
 
// Load XSL
$xsl = new DomDocument;
$xsl->load('cdcatalog.xsl');
 
// Transform file
$xp = new XsltProcessor();
$xp->importStylesheet($xsl);
 
echo $xp->transformToXML($xml);
?>

Re: XML

Posted: Wed Dec 09, 2009 12:44 pm
by st89
st89 wrote:Is there a way to transform XML with XSLT on the server using PHP? I'm using a free web tutorial (you get what you pay for) and it's lacking to say the least. I've searched Google and tried a variety of solutions that all fail with fatal errors. The ASP code given for XML to XSLT transformation on the server is below:

Code: Select all

<%
'Load XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("cdcatalog.xml"))
 
'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("cdcatalog.xsl"))
 
'Transform file
Response.Write(xml.transformNode(xsl))
%>
I'd be really grateful if someone could point me to a resource for XML with XSLT creation on the server with PHP. Thanks in advance for any help/tips/pointers :D
After more Googling, I found an answer that works. It turns out my previous failed attempts were due to using XSLT-related functions which only work in PHP4. For anyone reading this post, the following solution ONLY works in PHP5:

Code: Select all

<?php
$xp = new XsltProcessor();
$xsl = new DomDocument;
$xsl->load('stylesheet.xsl');
$xp->importStylesheet($xsl);
$xml_doc = new DomDocument;
$xml_doc->load('data.xml');
echo $xp->transformToXML($xml_doc);

Re: XML

Posted: Wed Dec 09, 2009 12:46 pm
by st89
AbraCadaver wrote:Check: http://us3.php.net/manual/en/book.xsl.php and: http://us3.php.net/manual/en/book.dom.php

Example here: http://us3.php.net/manual/en/xsltproces ... mtoxml.php

Code: Select all

<?php
// LoadXML
$xml = new DomDocument;
$xml->load('cdcatalog.xml');
 
// Load XSL
$xsl = new DomDocument;
$xsl->load('cdcatalog.xsl');
 
// Transform file
$xp = new XsltProcessor();
$xp->importStylesheet($xsl);
 
echo $xp->transformToXML($xml);
?>
Thanks for the reply! I wrote and posted my own response before viewing your clearly-explained post! :D