Calling PHP Functions from XSL

Nadeem | | Saturday, July 14th, 2007

Craig, a colleague of mine who newly joined our development team at Talis showed me this neat little trick. Many things are far easier to do in PHP than they are in XSL, and some things simply can’t be done in pure XSL. A solution is to call PHP functions directly from within your XSL.

1) In your xsl stylesheet add:

   namespace xmlns:php=""

2) To call the php function and access the result use:

  2.   <!– for string use this –>
  3.   <xsl:value-of select="php:functionString(‘phpFunctionName’, /xpath)"/>
  5.   <!– for DOM Nodes use this –>
  6.   <xsl:copy-of select="php:function(‘phpFunctionName’, /xpath)"/>

You can pass as many parameters as you want to either php:function or php:functionString – the latter merely converts output to a string and otherwise they are identical.

3) you must register them with the XSL Transformer:

  2. $doc = new DOMDocument();
  3. $xsl = new XSLTProcessor();
  5. $doc->load($xsl_filename);
  6. $xsl->importStyleSheet($doc);
  8. $doc->load($xml_filename);
  10. $xsl->registerPHPFunctions(); // This is the important call for this functionality
  12. echo $xsl->transformToXML($doc);

4) In your php function, access parameters passed in as strings as if they are a php string. If you pass a dom structure as a parameter then you need to access it along the lines of:

  2. function ProcessDomFromXslCall($DomList) {
  3.    $NodeList = $DomList[0]->getElementsByTagNameNS(‘namespace’, ‘element-name’);
  4.    for($i=0; $i<$NodeList->length; $i++) {
  5.      echo $i, $NodeList->item($i)->nodeValue;
  6.    }
  7.  }

$DomList will include the root element of the XPath used to call the PHP function

If you want to dump what you pass to PHP as a string you need to do:

  2.  function DumpStuff($DomElements) {
  3.    echo $DomElements[0]->C14N(); // note this function isn’t yet documented in the PHP manual !
  4.  }

It’s a very useful feature … good luck with it.


  1. Really useful, thanks.

    As an aside, if your PHP function is generating XML and you want to process that XML further down your XSLT chain, you probably want to use disable-output-escaping in your call like this:

    Comment by Dave — March 6, 2008 @ 3:46 pm

  2. (Let’s try that again)

    <xsl:value-of disable-output-escaping=”yes” select=”php:functionString(‘outputComponent’, name(.))”/>

    Comment by Dave — March 6, 2008 @ 3:47 pm

  3. Nice tutorial, very usefuil thanks.
    I have a clue: how can I use a simple var defined in PHP??


    define(‘MY_VAR’,'test var’);

    Is that possible to use a defined var in my xsl file?

    I don’t know, something like this:

    Any help?

    Thank you.

    Comment by Ezequiel — May 14, 2008 @ 3:43 pm

  4. Very nice tutorial. Thanks a lot to Dave for its trick :)

    Comment by shebang — April 13, 2009 @ 9:12 pm

  5. Good day I am so excited I found your webpage, I really found you by
    accident, while I was researching on Google for something else,
    Regardless I am here now and would just like to say thank you for a fantastic post and a all round thrilling
    blog (I also love the theme/design), I don’t have time to read through it all at
    the moment but I have bookmarked it and also added in your RSS feeds, so when I have time I will be back to read much more, Please do keep up the
    great b.

    Comment by church audio equipment — April 17, 2013 @ 1:27 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress | Theme by Roy Tanck