Page 1 of 1
PHP5 dom
Posted: Thu Mar 17, 2005 1:19 pm
by damo33
Hope someone can help with this...
Am trying to generate XSL in PHP and use it to transform XML, also generated dynamically. Below is a summary of code...
Code: Select all
//construct xsl and save to DOM
$xsldoc = new DomDocument('1.0');
$xslroot = $xsldoc->createElement('xsl:stylesheet');
$xslroot = $xsldoc->appendChild($xslroot);
etc etc
...$xsldoc->saveXML();
//construct xml and save to DOM
$doc = new DomDocument('1.0');
$root = $doc->createElement('root');
$root = $doc->appendChild($root);
etc etc
...$xml_string = $doc->saveXML();
//transform. this is where i have problems. 2nd line wont work
$xslt = new xsltProcessor;
$xslt->importStyleSheet(DomDocument::load($xsldoc));
echo $xslt->transformToXML(DomDocument::loadXML($xml_string));
thanks
feyd | Please use Code: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Thu Mar 17, 2005 2:34 pm
by timvw
and what are the errors you are recieving? "not work" is vague.
here is a sample i use to generate xml from a mysql resultset.
http://timvw.madoka.be/programming/php/rstoxml.php.txt
here is how i perform the translation
http://timvw.madoka.be/programming/php/ ... rm.php.txt
and then the glue between those functions
Code: Select all
<?php
require_once('error.php');
require_once('mysql.php');
require_once('rstoxml.php');
require_once('transform.php');
require_once('output.php');
$query = "SELECT m.message_id AS message_id, m.title AS title, m.content AS content, m.datetime AS datetime, IF(ISNULL(comment_id), 0, COUNT(*)) AS count FROM message AS m LEFT JOIN comment AS c USING (message_id) GROUP BY m.message_id ORDER BY m.datetime DESC LIMIT 5";
$result = mysql_query($query) or trigger_error(mysql_error(), E_USER_ERROR);
$xml = rstoxml($result);
$xsl = file_get_contents('xsl/index.xsl');
$xhtml = transform($xml, $xsl);
output($xhtml);
?>
Posted: Thu Mar 17, 2005 3:37 pm
by damo33
sorry.. i should be more specific. The problem is that I need to generate the XSL dynamically, not from a file, and the $xslt->importStyleSheet(DomDocument::load) code is expecting a file, not an object. i get these errors:
Warning: I/O warning : failed to load external entity "3%AA%E7ws%FA%12" in C:\PHP\classes4\dataXML.php on line 66
Warning: importStylesheet() expects parameter 1 to be object, boolean given in C:\PHP\classes4\dataXML.php on line 66
Warning: XSLTProcessor::transformToXml() [function.transformToXml]: No stylesheet associated to this object in C:\PHP\classes4\dataXML.php on line 67
any ideas?
Posted: Thu Mar 17, 2005 9:53 pm
by timvw
the
documentation clearly says:
XSLTProcessor->importStylesheet ( DOMDocument stylesheet )
(it is domdocument::load that expects a filename)
so you should simply use $xslt->importStylesheet($xsldoc);
Posted: Fri Mar 18, 2005 5:17 am
by damo33
Hi
Thanks for the response...
Have tried
$xslt = new xsltProcessor;
$xslt->importStyleSheet($xsldoc);
echo $xslt->transformToXML(DomDocument::loadXML($xml_string));
as you suggested, so I am now passing the DOM object, and not the string produced from the saveXML() method.
I now get:
Warning: compilation error: element xsl:stylesheet in C:\PHP\classes4\dataXML6.php on line 66
Warning: xsltParseStylesheetProcess : document is not a stylesheet in C:\PHP\classes4\dataXML6.php on line 66
Warning: XSLTProcessor::transformToXml() [function.transformToXml]: No stylesheet associated to this object in C:\PHP\classes4\dataXML6.php on line 67
Is this because of errors in my $xsl DOM construct code itself?...
$xsldoc = new DomDocument('1.0');
$xslroot = $xsldoc->createElement('xsl:stylesheet');
$xslroot = $xsldoc->appendChild($xslroot);
and so on....
Cheers
Damian
Posted: Fri Mar 18, 2005 5:27 am
by timvw
meaby you could consider validating against the xslt-dtd first?
Posted: Fri Mar 18, 2005 10:17 am
by damo33
well the XSL DOM stuff seems to display ok in IE...
Posted: Sat Mar 19, 2005 7:52 am
by damo33
but still not when generated in the php file...
