How to Transform XML with XSLT using PHP in Wordpress How to Transform XML with XSLT using PHP in Wordpress wordpress wordpress

How to Transform XML with XSLT using PHP in Wordpress


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


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.


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