Sending node collection to XSL from PHP function
I am using PHP XML and XSL to generate documents. If you are familiar with PHP implementation you may know that XSL function tokenize(string,pattern) is not available. I am trying to implement that function in PHP so that it can be called from within XSL like so
<xsl:value-of select="php:function('tokenize','aaa bbb ccc',' ')[last()]"/>
which should produce 'ccc'
The problem is that 开发者_运维知识库I cannot find a way to return node collection consisting of simple strings into XSL. When I try to send DOMNodeList I get " A PHP Object cannot be converted to a XPath-string" error. The techniques of writing XSL extension functions in PHP are not documented in PHP manual. Can some gurus out there shed some light on this?
Here is my current work around. I am able to do the following in XSL
<xsl:value-of select="php:function('tokenizeIntoXML',$stop_at_class,' ')/node()[last()]"/>
with this PHP function
function tokenizeIntoXML($string, $delimiter)
{
$ret = null;
$toks = explode($delimiter, $string);
if(!empty($toks))
{
$xml = new DomDocument();
$xml -> loadXML("<result/>");
foreach($toks as $t)
{
$xml -> documentElement -> appendChild(new DOMText($t));
}
return $xml -> documentElement;
}
return $ret;
}
False, if you include the corresponding namespace for exslt you actually can use tokenize as described here.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://exslt.org/strings"> <!-- here -->
<xsl:output
method="xml"
omit-xml-declaration="yes"
media-type="text/html"
encoding="UTF-8"
indent="yes"
/>
<xsl:template match="/">
<xsl:value-of select="fn:tokenize('a,b,c', ',')"/>
</xsl:template>
</xsl:stylesheet>
Tested on win764 XAMPP (apache2.2 php5.3)
精彩评论