首页 > php教程 > php手册 > 正文

XSLTProcessor 中 registerPHPFunctions 后无法调用 php 函数

php中文网
发布: 2016-05-25 16:53:03
原创
2074人浏览过

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免费学习笔记(深入)”;

其实,出现这种错误,是因为我们没有定义 PHP namespace,只需要在

MacsMind
MacsMind

电商AI超级智能客服

MacsMind 192
查看详情 MacsMind
<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">
登录后复制


教程地址:

欢迎转载!但请带上文章地址^^

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号