如何使用 PHP 函数过滤选择节点集?

How to filter a select nodeset with a PHP function?(如何使用 PHP 函数过滤选择节点集?)
本文介绍了如何使用 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:functionArray, php: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.

推荐答案

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)