PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

在 XSLTProcessor 中 registerPHPFunctions 后无法调用 php 函数解决办法_PHP教程

原创
2016-07-13 10:55:32 806浏览

XSLT 是一个非常方便的转换 XML 的工具,PHP 里面是通过 XSLTProcessor 来实现;XSLT 中内置了许多有用的函数,同时,只需要调用 XSLTProcessor 实例的 registerPHPFunctions 方法,我们就可以在 XSLT 中直接使用 PHP 的函数,这大大增强了 XSLT 的处理能力。

但是,在 xslt 中使用 php 函数时,很多人会遇到如下两种错误:

(1) Warning: XSLTProcessor::transformToXml(): xmlXPathCompiledEval: 1 objects left o
n the stack.
(2)PHP Warning: XSLTProcessor::transformToXml(): xmlXPathCompOpEval: function func
tion bound to undefined prefix php in ….

 代码如下 复制代码

$xml =
 
  bob
 

 
  joe
 


EOB;
$xsl =
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 

EOB;
$xmldoc = DOMDocument::loadXML($xml);
$xsldoc = DOMDocument::loadXML($xsl);
 
$proc = new XSLTProcessor();
$proc->registerPHPFunctions();
$proc->importStyleSheet($xsldoc);
echo $proc->transformToXML($xmldoc);
?>

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

 代码如下 复制代码
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

中增加 xmlns:php="http://php.net/xsl" 就能解决此问题, 即

 代码如下 复制代码

     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:php="http://php.net/xsl">

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/632243.htmlTechArticleXSLT 是一个非常方便的转换 XML 的工具,PHP 里面是通过 XSLTProcessor 来实现;XSLT 中内置了许多有用的函数,同时,只需要调用 XSLTProcessor 实例...
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。