gpt4 book ai didi

php - 如何在带有 registerPHPFunctions 的 XSLT 中使用 DOMDocumentFragment?

转载 作者:可可西里 更新时间:2023-10-31 23:20:59 24 4
gpt4 key购买 nike

请参阅“有效”“无效” 部分:是我的代码错误还是 DOMDocument-PHP 实现的错误?


如果您不熟悉 XSLT 和 registerPHPFunctions 请参阅 this link for context and preparations .假设按字符串输入,

 function XSL_transf($xml,$xsl) {
$xmldoc = DOMDocument::loadXML($xml);
$xsldoc = DOMDocument::loadXML($xsl);
$proc = new XSLTProcessor();
$proc->registerPHPFunctions(); // here
$proc->importStyleSheet($xsldoc);
echo $proc->transformToXML($xmldoc);
}
$xml='<root/>'; //simplest
$xsl = <<<'EOB'
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:php="http://php.net/xsl">
... HERE COPY/PASTE YOUR XSLT template ...
</xsl:stylesheet>
EOB;

有效

子句<xsl:copy-of ... />接收 DOMElementDOMDocument 和(为什么不呢?)DOMDocumentFragment。所以,如果我们有一个返回 DOMDocument 的 PHP 函数,我们就可以使用它。

function foo1() {
$dom = DOMDocument::loadXML('<t> foo <tt val="123"/> bar </t>');
return $dom;
}

调用 foo1进入模板,

<xsl:template match="/">
PHP foo1()=<xsl:copy-of select="php:function('foo1')" />
</xsl:template>

结果(您可以使用 XSL_transf($xml,$xsl) 查看):

 <t> foo <tt val="123"/> bar </t>

它不起作用

通过以下方式改变上面的功能

function foo1() {
$dom = new DOMDocument;
$tmp = $dom->createDocumentFragment();
$tmp->appendXML('<t> foo <tt val="123"/> bar </t> test');
return $tmp;
}

结果为空。没有错误消息,但没有结果。

最佳答案

您可以使用以下 XPath 表达式解决:

php:function('foo1')/node()

现在,这是一个 PHP 错误吗?还是 libxml2 错误?很难说,因为如果你看看 specifications of XPath 1.0 ,您找不到任何对文档片段的引用。最相似的是 root node ,也是用来表示文档的节点。

我认为问题来自 PHP,when it constructs the node set to be returned .它不应该返回整个文档片段节点,而是返回一个由其内容组成的节点集

但是,我可能完全错了,因为我从未使用过 libxml2,也不知道它是如何工作的。

关于php - 如何在带有 registerPHPFunctions 的 XSLT 中使用 DOMDocumentFragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20360350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com