grexx
asked on
XSLT server side parsing in PHP
I know my way around in XML and XSLT, and have made some pages that work properly in Firefox and IE6. Now what I would like to do is parse those files on the server using PHP. I've found something that looks as a start, but is not complete and not correct.
$html = xslt_process$xsltproc, 'index.xml', 'album.xsl');
I found this here on EE, and I suppose it should be something like this: take an xml-file and an xsl-file, and process it. I'm not a PHP-programmer, so would like some help here. I see the opening brace is missing. I suppose the $html string should be displayed, and that's it?
$html = xslt_process$xsltproc, 'index.xml', 'album.xsl');
I found this here on EE, and I suppose it should be something like this: take an xml-file and an xsl-file, and process it. I'm not a PHP-programmer, so would like some help here. I see the opening brace is missing. I suppose the $html string should be displayed, and that's it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
If I look at PHPInfo, then I see this at DOMXML
DOM/XML enabled
DOM/XML API Version 20020815
libxml Version 20606
HTML Support enabled
XPath Support enabled
XPointer Support enabled
DOM/XSLT enabled
libxslt Version 1.0.33
libxslt compiled against libxml Version 2.5.11
DOM/EXSLT enabled
libexslt Version 1.0.33
This looks like XSLT should be supported. Or is this separate from Sablotron?
DOM/XML enabled
DOM/XML API Version 20020815
libxml Version 20606
HTML Support enabled
XPath Support enabled
XPointer Support enabled
DOM/XSLT enabled
libxslt Version 1.0.33
libxslt compiled against libxml Version 2.5.11
DOM/EXSLT enabled
libexslt Version 1.0.33
This looks like XSLT should be supported. Or is this separate from Sablotron?
hm...i see libxml and libxslt, but don't see Sablotron...
why not to try install it and run some samples?
why not to try install it and run some samples?
ASKER
It's not my server, it's the server of my isp, and they say DOMXML is installed as parser (so Sablotron is not needed in their view). See for more info:
http://nl3.php.net/manual/en/ref.domxml.php and
http://nl3.php.net/manual/en/function.domxsltstylesheet-process.php
from which the following example:
<?php
$document = new DOMDocument('index.xml');
// Unfortunately there's no such method for DomXsltStylesheet
$stylesheet = domxml_xslt_stylesheet_fil e('/path/t o/styleshe et');
$params = array(
'param1' => 'value1'
...
, 'paramN' => 'valueN' );
$result = $stylesheet->process($docu ment, $params);
?>
This example doesn't work. I don't know what to use for the parameters ($params), so I deleted that and removed it from the $result line. The error I get is about line 2 ($document):
Warning: domdocument(): Entity: line 1: in /var/www/html/album/040915 /catalunya /album.php on line 2
QUESTIONS
1) Can I do without Sablotron and does DOMXML replace the need for it?
2) If DOMXML is enough to get a parser working, what's wrong with the above example?
3) What should I do with the parameters?
I've added a few points.
http://nl3.php.net/manual/en/ref.domxml.php and
http://nl3.php.net/manual/en/function.domxsltstylesheet-process.php
from which the following example:
<?php
$document = new DOMDocument('index.xml');
// Unfortunately there's no such method for DomXsltStylesheet
$stylesheet = domxml_xslt_stylesheet_fil
$params = array(
'param1' => 'value1'
...
, 'paramN' => 'valueN' );
$result = $stylesheet->process($docu
?>
This example doesn't work. I don't know what to use for the parameters ($params), so I deleted that and removed it from the $result line. The error I get is about line 2 ($document):
Warning: domdocument(): Entity: line 1: in /var/www/html/album/040915
QUESTIONS
1) Can I do without Sablotron and does DOMXML replace the need for it?
2) If DOMXML is enough to get a parser working, what's wrong with the above example?
3) What should I do with the parameters?
I've added a few points.
ASKER
Well I see I haven't copied the example really well. So here it is again, how I used it. The files index.xml and album.xsl are in the same directory as the parser-php-file. Using absolute paths doesn't solve anything.
<?php
$document = new DOMDocument('index.xml');
// Unfortunately there's no such method for DomXsltStylesheet
$stylesheet = domxml_xslt_stylesheet_fil e('album.x sl');
$result = $stylesheet->process($docu ment);
?>
<?php
$document = new DOMDocument('index.xml');
// Unfortunately there's no such method for DomXsltStylesheet
$stylesheet = domxml_xslt_stylesheet_fil
$result = $stylesheet->process($docu
?>
ASKER
I still don't know where I stand. So I repeat my questions:
QUESTIONS
1) Can I do without Sablotron and does DOMXML replace the need for it?
2) If DOMXML is enough to get a parser working, what's wrong with the above example?
3) What should I do with the parameters?
And I've added some point....
QUESTIONS
1) Can I do without Sablotron and does DOMXML replace the need for it?
2) If DOMXML is enough to get a parser working, what's wrong with the above example?
3) What should I do with the parameters?
And I've added some point....
ASKER
<?php
//Transformer XML -> HTML par XSL
$CurrentDir = dirname(__FILE__);
$xmldoc = domxml_open_file("$Current Dir\\sampl e.xml");
$xsldoc = domxml_xslt_stylesheet_fil e("$Curren tDir\\samp le.xsl");
$result = $xsldoc->process($xmldoc);
print $xsldoc->result_dump_mem($ result);
?>
This code just solved my problem. It works, and I don't need Sablotron for now. Maybe Sablotron has more or better support for XSLT, but for now I can do what I need to do.
//Transformer XML -> HTML par XSL
$CurrentDir = dirname(__FILE__);
$xmldoc = domxml_open_file("$Current
$xsldoc = domxml_xslt_stylesheet_fil
$result = $xsldoc->process($xmldoc);
print $xsldoc->result_dump_mem($
?>
This code just solved my problem. It works, and I don't need Sablotron for now. Maybe Sablotron has more or better support for XSLT, but for now I can do what I need to do.
ASKER
<?php
// Allocate a new XSLT processor
$xh = xslt_create();
// Process the document
if (xslt_process($xh, 'index.xml', 'album.xsl', 'result.xml')) {
echo "SUCCESS, sample.xml was transformed by sample.xsl into result.xml";
echo ", result.xml has the following contents\n<br />\n";
echo "<pre>\n";
readfile('result.xml');
echo "</pre>\n";
} else {
echo "Sorry, sample.xml could not be transformed by sample.xsl into";
echo " result.xml the reason is that " . xslt_error($xh) . " and the ";
echo "error code is " . xslt_errno($xh);
}
xslt_free($xh);
?>
When I run it, I get the following error:
Fatal error: Call to undefined function: xslt_create() in /var/www/html/album/040915
What't the problem here?