XML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
st89
Forum Newbie
Posts: 17
Joined: Sat Nov 28, 2009 5:42 pm

XML

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: XML

Post 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);
?>
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
st89
Forum Newbie
Posts: 17
Joined: Sat Nov 28, 2009 5:42 pm

Re: XML

Post 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);
st89
Forum Newbie
Posts: 17
Joined: Sat Nov 28, 2009 5:42 pm

Re: XML

Post 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
Post Reply