脚本宝典收集整理的这篇文章主要介绍了如何使用PHP函数筛选select nodeset?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
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)可以是:
<p> <name>Name-1</name> <name>Name-2</name> <name>Name-3</name> <name>Name-4</name> </p>
在样式表中,它被称为:
<xsl:template name="listing"> <xsl:apply-templates select="PHP:function('all_but_first',/p/name)"> </xsl:apply-templates> </xsl:template>
通知如下:
我也尝试其他“函数”名称,因为我看到有PHP:functionString,但所有尝试到目前为止(PHP:functionArray,PHP:functionSet和PHP:functionList)没有工作.
在PHP手册中,我写了我可以返回另一个包含元素的DOMDocument,然而这些元素不再是从原始文档.这对我来说没什么意义
DOMDocumentFragment
的实例,而不是一个数组.所以要尝试你的例子,我保存你的输入为foo.xml.然后我做了foo.xslt看起来像这样:
<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:
<?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',true); $xmlDoc = new SimpleXMLElement('./foo.xml',true); $proc = new XSLTProcessor(); $proc->registerPHPFunctions(); $proc->importStylesheet($xslDoc); $buffer = $proc->transformToXML($xmlDoc); echo $buffer; ?>
以上是脚本宝典为你收集整理的如何使用PHP函数筛选select nodeset?全部内容,希望文章能够帮你解决如何使用PHP函数筛选select nodeset?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。