Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Transform XML with XSLT using PHP in Wordpress

Right now I transform an XML document with an XSLT stylesheet using Javascript (in a Wordpress-based website). This works fine in Firefox and Chrome, but not in IE. Plus, if Javascript is not enabled, nothing would show up.

So, my goal is to do the XML/XSLT transformation to XHTML on the server, not the client, preferably using PHP.

I've tried many different PHP scripts that other people have written (I'm a newbie) but I can't get them to work. I've included the simplest PHP script I've found below. I know the dynamic filepath might be a problem, but I don't know a better way to locate the XML and XSLT files.

When I use the below script, I get the error: Parse error: syntax error, unexpected T_STRING in /home/alan/public_html/wp-content/themes/Stacked/page-renting.php on line 42

Alternative solutions would be welcome as well.

<?php

$xml = new DOMDocument();
$xml->load('<?php bloginfo('template_directory'); ?>/rentals/works.xml');

$xsl = new DOMDocument;
$xsl->load('<?php bloginfo('template_directory'); ?>/rentals/works.xsl');

$proc = new XSLTProcessor();
$proc->importStyleSheet($xsl);

echo $proc->transformToXML($xml);

?>
like image 229
Alan Avatar asked Jan 24 '10 19:01

Alan


3 Answers

You just have to replace that bit of PHP in the right context, this way:

$xml = new DOMDocument;
$xml->load(get_bloginfo('template_directory') . '/rentals/works.xml');

$xsl = new DOMDocument;
$xsl->load(get_bloginfo('template_directory') . '/rentals/works.xsl');

$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);

echo $proc->transformToXML($xml);
like image 83
Josh Davis Avatar answered Nov 14 '22 17:11

Josh Davis


Solved it.

I tried the above suggestions of Josh and Rubens, but the xml and xsl documents could still not be found. But from Josh's idea of a different way to access the template directory, I googled a bit and found this solution:

Here's the final PHP script I used to transform XML with XSLT on the server using PHP. Thanks to all who helped.

<?php

$xml = new DOMDocument;
$xml->load('./wp-content/themes/Stacked/rentals/WORKS.xml');

$xsl = new DOMDocument;
$xsl->load('./wp-content/themes/Stacked/rentals/WORKS.xsl');

$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);

echo $proc->transformToXML($xml);

?>

The two key things that make it work:

  1. Using a period and filepath as an alternative to the usual wordpress method i was using before.

  2. Case-sensitivity. My file names were capitalized (not wise, I know). As filepaths are not usually case sensitive, I didn't think of it, but turns out that in this case (when inside of a PHP script?), using the proper case for BOTH the theme name (Stacked) and the file name (WORKS.xml, WORKS.xsl) is necessary to make it find the file correctly.

like image 28
Alan Avatar answered Nov 14 '22 18:11

Alan


Another way would be not to use XSLT at all but instead a plugin that transforms XML using simple mark-up. See this plugin.

like image 42
Gustav S Avatar answered Nov 14 '22 16:11

Gustav S