XSLTProcessor 中 registerPHPFunctions 后无法调用 php 函数
发布:smiling 来源: PHP粉丝网 添加日期:2013-11-30 14:01:26 浏览: 评论:0
XSLT 是一个非常方便的转换 XML 的工具,PHP 里面是通过 XSLTProcessor 来实现;XSLT 中内置了许多有用的函数,同时,只需要调用 XSLTProcessor 实例的 registerPHPFunctions 方法,我们就可以在 XSLT 中直接使用 PHP 的函数,这大大增强了 XSLT 的处理能力。
但是,在 XSLT 中使用 PHP 函数时,很多人会遇到如下两种错误:
(1)、Warning: XSLTProcessor::transformToXml(): xmlXPathCompiledEval: 1 objects left on the stack.
(2)、PHP Warning: XSLTProcessor::transformToXml(): xmlXPathCompOpEval: function function bound to undefined prefix php in ….
- <?php
- $xml = <<<EOB
- <allusers>
- <user>
- <uid>bob</uid>
- </user>
- <user>
- <uid>joe</uid>
- </user>
- </allusers>
- EOB;
- $xsl = <<<EOB
- <?xml version="1.0" encoding="UTF-8"?>
- <xsl:stylesheet version="1.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
- <xsl:output method="html" encoding="utf-8" indent="yes"/>
- <xsl:template match="allusers">
- <html><body>
- <h2>Users</h2>
- <table>
- <xsl:for-each select="user">
- <tr><td>
- <xsl:value-of
- select="php:function('ucfirst',string(uid))"/>
- </td></tr>
- </xsl:for-each>
- </table>
- </body></html>
- </xsl:template>
- </xsl:stylesheet>
- EOB;
- $xmldoc = DOMDocument::loadXML($xml);
- $xsldoc = DOMDocument::loadXML($xsl);
- $proc = new XSLTProcessor();
- $proc->registerPHPFunctions();
- $proc->importStyleSheet($xsldoc);
- echo $proc->transformToXML($xmldoc);
- ?>
其实,出现这种错误,是因为我们没有定义 PHP namespace,只需要在
- <xsl:stylesheet version="1.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
中增加 xmlns:php="http://php.net/xsl" 就能解决此问题, 即
- <xsl:stylesheet version="1.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- xmlns:php="http://php.net/xsl">
Tags: XSLTProcessor registerPHPFunctions
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)