note 59280 added to function.xsl-xsltprocessor-register-php-functions
| From: | taylorbarstowatthatgooglemailthingy at osu1 dot php dot net | Date: | Thu, 01 Dec 2005 15:14:07 +0000 |
| Subject: | note 59280 added to function.xsl-xsltprocessor-register-php-functions | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-99419@lists.php.net to get a copy of this message | ||
From a PHP function, you can pass a nodeset back to XSL using a DOMDocument. For example:
<?php
function getNodeSet() {
$xml =
"<test>" .
"<a-node>This is a node</a-node>" .
"<a-node>This is another node</a-node>" .
"</test>";
$doc = new DOMDocument;
$doc->loadXml($xml);
return $doc;
}
?>
The only problem I've found is that the root level node in your returned DOM document acts like
the root level node of your original. SO, it's easy to introduce an infinite loop like so:
<xsl:template match="/">
<xsl:apply-templates select="php:function('getNodeSet')" />
</xsl:template>
To avoid this, I've been using a construct like:
<xsl:template match="/">
<xsl:for-each select="php:function('getNodeSet')" />
<xsl:apply-templates />
</xsl:for-each>
</xsl:template>
which effectively discards the root node. Presumably, it's worth creating a template to do the
discard:
<xsl:template select="*" mode="discardRoot">
<xsl:apply-templates select="./*" />
</xsl:template>
Which you can call like so:
<xsl:apply-templates select="php:function('getNodeSet')"
mode="discardRoot" />
----
Manual Page -- http://www.php.net/manual/en/function.xsl-xsltprocessor-register-php-functions.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59280
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59280&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59280&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59280&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59280&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59280&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59280&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59280&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59280&report=yes
Search -- http://master.php.net/manage/user-notes.php