How to work with DOMDocumentFragment in XSLT using registerPHPFunctions?

See the sections “it works” and “it does NOT work”: is it an error of my code or an error of implementation of DOMDocument-PHP?


If you are new to XSLT and registerPHPFunctions, see this link for context and preparation . Suppose you enter a string,

 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;

working

The clause <xsl:copy-of ... />gets DOMElement, DOMDocument and (why not?) DOMDocumentFragment. So, if we have a PHP function that returns a DOMDocument, we can use it.

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

Call foo1to template,

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

RESULTS (you can use XSL_transf($xml,$xsl)to view):

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

Does not work

Change function above to

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

RESULT is empty. There are no error messages, but no result.

+4
2

, XPath:

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

, PHP? libxml2? , , XPath 1.0, . root node, node, .

, PHP, node, . node, , .

, , libxml2, , .

+3

, , , registerPHPFunctions(). libxml2/libxslt (http://xmlsoft.org/).

, , , , xml . "" node , <t> . node, node . DocumentFragment , , .

, DocumentFragments .

@Peter Krauss: - xml, node. libxslt, . , . , xslt?

0

All Articles