如何在 Wordpress 中使用 PHP 使用 XSLT 转换 XML [英] How to Transform XML with XSLT using PHP in Wordpress

查看:32
本文介绍了如何在 Wordpress 中使用 PHP 使用 XSLT 转换 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

So, my goal is to do the XML/XSLT transformation to XHTML on the server, not the client, preferably using PHP.

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

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.

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

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

也欢迎其他解决方案.

<?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 的那一点,这样:

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

这篇关于如何在 Wordpress 中使用 PHP 使用 XSLT 转换 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆