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.

Read More

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

?>

Related posts

Leave a Reply

4 comments

  1. 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);
    
  2. 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.

  3. 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