How to Transform XML with XSLT using PHP in Wordpress

旧巷老猫 提交于 2019-12-21 05:11:13

问题


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

?>

回答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:


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




回答4:


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



来源:https://stackoverflow.com/questions/2128470/how-to-transform-xml-with-xslt-using-php-in-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!