但是,在 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">
Users
select="user">
select="php:function('ucfirst',string(uid))"/>
|
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">
|