Link to home
Start Free TrialLog in
Avatar of grexx
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?
Avatar of grexx
grexx

ASKER

Well, I just found this example:

<?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/catalunya/album.php on line 4

What't the problem here?
ASKER CERTIFIED SOLUTION
Avatar of dualsoul
dualsoul

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of grexx

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?
hm...i see libxml and libxslt, but don't see Sablotron...

why not to try install it and run some samples?
Avatar of grexx

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_file('/path/to/stylesheet');
$params = array(
   'param1' => 'value1'
   ...
   , 'paramN' => 'valueN' );
$result = $stylesheet->process($document, $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.
Avatar of grexx

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_file('album.xsl');

$result = $stylesheet->process($document);
?>
Avatar of grexx

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....
Avatar of grexx

ASKER

<?php
//Transformer XML -> HTML par XSL
$CurrentDir = dirname(__FILE__);
$xmldoc = domxml_open_file("$CurrentDir\\sample.xml");
$xsldoc = domxml_xslt_stylesheet_file("$CurrentDir\\sample.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.