Hello all,

I'm trying to access a method in a class from an XSL document, with registerPHPFunctions() turned on. I can successfully call built-in PHP functions, but methods in a class I've written seem to error out when I attempt to call them. I've made a really simple example here to show you what I mean.

I'm sure it's just a syntax problem, but I'm struggling to understand the scope of the method once it's being called from the XSL.

<?
class Foo {
	function foo () {
		$xml = new DOMDocument('1.0', 'UTF-8');
		$xml->appendChild(new DOMElement("root"));
		$xslDoc = new DomDocument();
		$xslDoc->load("foo.xsl");
		$xsl = new XsltProcessor();
		$xsl->registerPHPFunctions();
		$xsl->importStylesheet($xslDoc);
		$dom->formatOutput = true;
		$dom = $xsl->transformToDoc($xml);
		print ($dom->saveHTML());
	}
	//this is the method I'd like to call from inside the XSL
	function test () {
		return ("Value from a method called test()");
	}
}
$foo = new Foo();
?>

And the contents of foo.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:php="http://php.net/xsl">

<xsl:output method="html" encoding="iso-8859-1" indent="yes"/>

<xsl:template match="/">
	the year is <xsl:value-of select="php:function('date', 'Y')"/><br/>
	the value from a class is [b]<xsl:value-of select="php:function('foo::test')"/>[/b]
</xsl:template>

</xsl:stylesheet>

The bolded line in the XSL is the troublesome one. Anyone have any ideas why I can't call $foo->test()?

Thanks for any ideas!

    Well, I answered my own question. Posting here for anyone else who may stumble upon this post.

    You have to set your function as a static method to make it accessible from the XSL. Nothing else worked, so static it is.

      Write a Reply...