如何使用PHP函数过滤选择的节点集? [英] How to filter a select nodeset with a PHP function?

查看:68
本文介绍了如何使用PHP函数过滤选择的节点集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否以及如何向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:functionArrayphp:functionSetphp: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.

推荐答案

对我有用的是返回

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函数过滤选择的节点集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆