[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

XSLT server side parsing in PHP

Posted on 2004-11-04
8
Medium Priority
?
330 Views
Last Modified: 2008-03-17
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?
0
Comment
Question by:grexx
  • 6
  • 2
8 Comments
 
LVL 1

Author Comment

by:grexx
ID: 12494456
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?
0
 
LVL 15

Accepted Solution

by:
dualsoul earned 900 total points
ID: 12501894
You need to instal Sablotron - XSLT processor, you can use with PHP.

this links should give you info for download, install, and some samples:

http://www.gingerall.com/charlie/ga/xml/x_sabphp.xml?s=org
http://genomenews.free.fr/tutorials/sablot.html
0
 
LVL 1

Author Comment

by:grexx
ID: 12502302
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?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:dualsoul
ID: 12504119
hm...i see libxml and libxslt, but don't see Sablotron...

why not to try install it and run some samples?
0
 
LVL 1

Author Comment

by:grexx
ID: 12504425
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.
0
 
LVL 1

Author Comment

by:grexx
ID: 12504500
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);
?>
0
 
LVL 1

Author Comment

by:grexx
ID: 12524279
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....
0
 
LVL 1

Author Comment

by:grexx
ID: 12576646
<?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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question