如何在WordPress中使用PHP使用XSLT转换XML

现在我使用Javascript(在基于Wordpress的网站中)使用XSLT样式表转换XML文档.这适用于Firefox和Chrome,但不适用于IE.另外,如果未启用Javascript,则不会显示任何内容.所以,我的目标是在服务器而不是客户端上进行XML /...

现在我使用Javascript(在基于Wordpress的网站中)使用XSLT样式表转换XML文档.这适用于Firefox和Chrome,但不适用于IE.另外,如果未启用Javascript,则不会显示任何内容.

所以,我的目标是在服务器而不是客户端上进行XML / XSLT到XHTML的转换,最好是使用PHP.

我尝试了许多其他人编写的PHP脚本(我是新手),但我无法让他们工作.我已经包含了我在下面找到的最简单的PHP脚本.我知道动态文件路径可能有问题,但我不知道找到XML和XSLT文件的更好方法.

当我使用下面的脚本时,我收到错误:解析错误:语法错误,第42行/home/alan/public_html/wp-content/themes/Stacked/page-renting.php中的意外T_STRING

也欢迎替代解决方案.

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

本文标题为:如何在WordPress中使用PHP使用XSLT转换XML

基础教程推荐