现在我使用 Javascript(在一个基于 Wordpress 的网站中)转换一个带有 XSLT 样式表的 XML 文档。这在 Firefox 和 Chrome 中工作正常,但在 IE 中不行。另外,如果未启用 Javascript,则不会显示任何内容。
因此,我的目标是在服务器(而不是客户端)上将 XML/XSLT 转换为 XHTML,最好使用 PHP。
我已经尝试过许多其他人编写的不同 PHP 脚本(我是新手),但我无法让它们工作。我在下面包含了我发现的最简单的 PHP 脚本。我知道动态文件路径可能是个问题,但我不知道找到 XML 和 XSLT 文件的更好方法。
当我使用下面的脚本时,我收到错误:解析错误:语法错误,意外的 T_STRING in/home/alan/public_html/wp-content/themes/Stacked/page-renting.php on line 42
也欢迎其他解决方案。
<?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);
?>
最佳答案
您只需要在正确的上下文中替换那部分 PHP,这样:
$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);
关于php - 如何在 Wordpress 中使用 PHP 使用 XSLT 转换 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2128470/