views:

505

answers:

5

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);

?>
A: 

You must remove that bloginfo information; that load method gets your XML/XSLT filenames.

$xml->load('/rentals/works.xml');
$xsl->load('/rentals/works.xsl');

Of course, they must indicate correct path your XML/XSLT files

Rubens Farias
+2  A: 

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);
Josh Davis
This should work if you use `get_bloginfo` instead, as the `bloginfo` function prints rather than returning a variable.
Richard M
Oh, right. I don't know WordPress functions but now it explains why the original poster was trying to execute that code there.
Josh Davis
A: 

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.

Alan
A: 

Hi,

I'm a real newbie to xml/xslt. I was hoping that someone would be able to help explain in layman's terms how to get the above to work.

I am using wordpress, and would like to get this script working across many pages on my site. Each page will have a different xml and xsl file that I would like to transform and display the xml content onto each page wordpress page.

Can you help? Where should I put the above code?

Thanks to anyone who can point me in the right direction.

Alan B
You would put the code directly into custom Wordpress page templates you create. The code will be the same except that it will call on different XML and XSL files. See below for the final version of the code I used.
Alan
I have added the code into a page template, but receive the following error message:Warning: domdocument() expects at least 1 parameter, 0 given in /path/to/file.xmlAny ideas how I can resolve this error?Many thanks
Alan B
Issue resolved. FYI, this was down to server configuration. The server was setup with php 4 and not php 5.
Alan B
A: 

Hi Alan, thanks for that. I was just wondering how to approach the fact that I have nearly 70 different xml files and associated xsl files which would mean that I would need to create 70 template pages. Is there a way to include the above script onto one php template and then in the wordpress admin screen simply add the path to the xml file and xsl file for that page? Thanks very much for your help :-)

Alan B
Hi Alan. Out of my realm of expertise. There's a wordpress plugin that allows you to add a shortcode to any page and fill in the filenames there, but if you want to use the php code in this post and edit it from the admin panel, it sounds like you can create a plugin: http://codex.wordpress.org/Writing_a_Plugin But that's beyond what I've learned so far. Good luck!
Alan