Calling PHP Functions from XSL

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="http://php.net/xsl"
   exclude-result-prefixes="php"

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

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

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:

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

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:

  1.  
  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.  }
  8.  

$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:

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

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

5 thoughts on “Calling PHP Functions from XSL

  1. Dave

    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:

  2. Dave

    (Let’s try that again)

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

  3. Ezequiel

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

    example:

    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.

  4. church audio equipment

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>