我想知道是否以及如何向 XSLT 处理器注册一个 PHP 用户空间函数,该函数不仅可以获取节点数组,还可以返回它?
I wonder if and how it is possible to register a PHP userspace function with the XSLT processor that is able not only to take an array of nodes but also to return it?
现在 PHP 抱怨使用通用设置将数组转换为字符串:
Right now PHP complains about an array to string conversion using the common setup:
function all_but_first(array $nodes) {
array_shift($nodes);
shuffle($nodes);
return $nodes;
};
$proc = new XSLTProcessor();
$proc->registerPHPFunctions();
$proc->importStylesheet($xslDoc);
$buffer = $proc->transformToXML($xmlDoc);
要转换的 XMLDocument ($xmlDoc
) 例如可以是:
The XMLDocument ($xmlDoc
) to transform can for example be:
<p>
<name>Name-1</name>
<name>Name-2</name>
<name>Name-3</name>
<name>Name-4</name>
</p>
在样式表中是这样调用的:
Within the stylesheet it's called like this:
<xsl:template name="listing">
<xsl:apply-templates select="php:function('all_but_first', /p/name)">
</xsl:apply-templates>
</xsl:template>
通知如下:
注意:数组到字符串的转换
Notice: Array to string conversion
我不明白为什么如果函数得到一个数组作为输入,它也不能返回一个数组?
I don't understand why if the function gets an array as input is not able to return an array as well?
我也在尝试其他函数"名称,因为我看到有 php:functionString
但到目前为止都尝试过 (php:functionArray
, php:functionSet
和 php:functionList
) 不起作用.
I was also trying other "function" names as I've seen there is php:functionString
but all tried so far (php:functionArray
, php:functionSet
and php:functionList
) did not work.
在 PHP 手册中,我可以返回另一个包含元素的 DOMDocument
,但是这些元素不再来自原始文档.这对我来说没有多大意义.
In the PHP manual it's written I can return another DOMDocument
containing elements, however then those elements aren't from the original document any longer. That does not make much sense to me.
对我有用的是返回一个 DOMDocumentFragment
而不是数组.因此,为了在您的示例中进行尝试,我将您的输入保存为 foo.xml
.然后我让 foo.xslt
看起来像这样:
Something that works for me is to return an instance of DOMDocumentFragment
instead of an array. So to try it on your example, I saved your input as foo.xml
. Then I made foo.xslt
look like this:
<xsl:stylesheet version="1.0" xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:php="http://php.net/xsl">
<xsl:template match="/">
<xsl:call-template name="listing" />
</xsl:template>
<xsl:template match="name">
<bar> <xsl:value-of select="text()" /> </bar>
</xsl:template>
<xsl:template name="listing">
<foo>
<xsl:for-each select="php:function('all_but_first', /p/name)">
<xsl:apply-templates />
</xsl:for-each>
</foo>
</xsl:template>
</xsl:stylesheet>
(这主要只是您使用 xsl:stylesheet
包装器来调用它的示例.)问题的真正核心,foo.php
:
(This is mostly just your example with a xsl:stylesheet
wrapper to invoke it.) And the real heart of the matter, foo.php
:
<?php
function all_but_first($nodes) {
if (($nodes == null) || (count($nodes) == 0)) {
return ''; // Not sure what the right "nothing" return value is
}
$returnValue = $nodes[0]->ownerDocument->createDocumentFragment();
array_shift($nodes);
shuffle($nodes);
foreach ($nodes as $node) {
$returnValue->appendChild($node);
}
return $returnValue;
};
$xslDoc = new SimpleXMLElement('./foo.xslt', 0, true);
$xmlDoc = new SimpleXMLElement('./foo.xml', 0, true);
$proc = new XSLTProcessor();
$proc->registerPHPFunctions();
$proc->importStylesheet($xslDoc);
$buffer = $proc->transformToXML($xmlDoc);
echo $buffer;
?>
重要的部分是调用 ownerDocument->createDocumentFragment()
以生成从函数返回的对象.
The important part being the call to ownerDocument->createDocumentFragment()
to make the object that gets returned from the function.
这篇关于如何使用 PHP 函数过滤选择节点集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!