Software is like sex... It's better when it's free.

Blog - Using PHP functions in XSL

Using PHP functions in XSL

A major new features of PHP5 is that the parsing of XML / XSL XSLTProcessor will natively use PHP to perform heavy and cumbersome transformations before. Now, no need to do to make a template to do ucwords(), just a little setup and we are good.

At PHP's level :

<?php
$xml = new DOMDocument();
$xml->load('flux.xml');
$xsl = new DOMDocument();
$xsl->load('flux.xsl');
$proc = new XSLTProcessor();

// It specifies the parser that it wants to use PHP functions in XSL
$proc->registerPHPFunctions();

$proc->importStyleSheet($xsl); 
echo $proc->transformToXML($xml);
?>

At XSL's level :

<!--
- Simple test to call php function
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl"
 version="1.0">
<!-- We add the PHP's xmlns -->
  <xsl:output method="xml" encoding="UTF-8" indent="yes" />
  <xsl:template match="root">
    <html>

      <!-- We use the php suffix to call the function ucwords() -->
      <xsl:value-of select="php:function('ucwords','php can now be used in xsl')" />
      <!-- Output: 'Php Can Now Be Used In Xsl' -->

    </html>
  </xsl:template>
</xsl:stylesheet>

You can even use static functions of class, at PHP's level :

<?php
class XSL
{
  /**
  * stringToUrl
  *
  * @param $string the string
  * @param $delimiter the implode character
  * @return $string the string modified
  * @see self::fullTrim
  * @see self::removeSpecialCaracteres
  * @see self::removeAccents
  */
  static function stringToUrl($string,$delimiter=" ")
  {
    return self::fullTrim(self::removeSpecialCaracteres(strtolower(self::removeAccents($string))),
    $delimiter);		
  }

  /**
  * Remove accents in string
  *
  * @param $string the string
  * @return $string the string modified
  */
  static function removeAccents($string)
  {
    static $search = array ("à", "á", "â", "ã", "ä", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï",
                            "ñ", "ò" ,"ó", "ô", "õ", "ö", "ù", "ú", "û", "ü", "ý", "ÿ", "À", "Á",
                            "Â", "Ã", "Ä", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ñ", "Ò",
                            "Ó", "Ô", "Õ", "Ö", "Ù", "Ú", "Û", "Ü", "Ý");
    static $replac = array ("a", "a", "a", "a", "a", "c", "e", "e", "e", "e", "i", "i", "i", "i",
                            "n", "o", "o", "o", "o", "o", "u", "u", "u", "u", "y", "y", "A", "A",
                            "A", "A", "A", "C", "E", "E", "E", "E", "I", "I", "I", "I", "N", "O",
                            "O", "O", "O", "O", "U", "U", "U", "U", "Y");											 	
    return str_replace($search,$replac,$string);			
  }

  /**
  * Remove special chars in string
  *
  * @param $string the string
  * @return $string the string modified	 
  */
  static function removeSpecialCaracteres($string)
  {
    static $search = array ("²", "&", "~", "#", "{", "(", "[", "-", "|", "`", "\\", "^", "'", ")",
                            "]", "}", "^", "¨", "£", "¤", "%", "*", ",", "?", ";", ".", ":",
                            "/", "!", "§", "<", ">", "»", "«", "\n", "\r", "  ");
    static $replac = " ";
    return str_replace($search,$replac,$string);
  }

  /**
  * Perform a full trim in string
  *
  * @param string $element the string
  * @param $delimiter implode glue
  * @return string the string modified 
  */
  static function fullTrim($element,$delimiter="")
  {		
    return implode($delimiter,self::processFunction(explode(' ',$element),'trim'));
  }

}
?>

And at XSL's level :

<!--
- More complex test to call php class function
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl"
 version="1.0">
  <xsl:output method="html" version="XHTML 1.0" encoding="UTF-8" indent="yes" />
  <xsl:template match="root">
    <html>

      <!-- We use the php suffix to call the static class function stringToUrl() -->
      <xsl:value-of select="php:function('XSL::stringToUrl','une_superstring-àÔ|modifier')" />
      <!-- Output: 'une_superstring ao modifier' -->

    </html>
  </xsl:template>
  </xsl:stylesheet>

We can therefore easily implement PHP's changes to our XSL templates.

Tags:  phpxmlxsl.
Posted the Monday 04 may 2009 17:49:31

Comments :

reskp91, the friday 12 june 2009 10:10:33
excellent ! :)