1

Recently, I've been helping someone move a site over from one remote webserver to a local webserver. However, the code I've inherited contains code like this:

$xsltproc = xslt_create();
$html = xslt_process($xsltproc, $path, 'xsl/Stylesheet2.xsl');

However, PHP5 doesn't like code like that, and from what I've read, using XSLT functions like the above is not allowed in PHP5. Obviously, this code must've been written in PHP4 and used on a server running PHP4. My question is, how can I get this code to work on my PHP5 server (there is plenty more code using "XSLT", the above is just a snippet)? Is rewriting all the code the only way to make it work? I'd rather avoid that if at all possible.

j0k
  • 22,600
  • 28
  • 79
  • 90
user1617942
  • 233
  • 3
  • 7
  • 1
    _"Note: If you need xslt support with PHP 5 you can use [the XSL extension](http://us.php.net/manual/en/book.xsl.php)."_ - [XSLT docs](http://us.php.net/manual/en/intro.xslt.php) – Wiseguy Aug 22 '12 at 19:28
  • 1
    possible duplicate of [How do I enable XSLT functions in PHP 5?](http://stackoverflow.com/questions/4451042/how-do-i-enable-xslt-functions-in-php-5) – Wiseguy Aug 22 '12 at 19:30
  • check this http://stackoverflow.com/questions/4451042/how-do-i-enable-xslt-functions-in-php-5 – PoX Aug 22 '12 at 19:32

1 Answers1

0

i hope it's resolve your problem

function transform($xmlvalue, $xsl,$paraArray=array(),$IsFileXML=FALSE)
        {
            $XML = new DOMDocument();
            if ($IsFileXML)
                $XML->load( $xmlvalue);
            else
                $XML->loadXML( $xmlvalue );
            $xslt = new XSLTProcessor();
            $XSL = new DOMDocument();
            $XSL->load( $xsl, LIBXML_NOCDATA);
            $xslt->importStylesheet( $XSL );
            foreach ($paraArray as $key => $value) {
                $xslt->setParameter('', $key,$value);
            }
            $xslt->registerPHPFunctions();
            return $xslt->transformToXML( $XML );
        }

you have to call php function in xslt like this

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:php="http://php.net/xsl"  exclude-result-prefixes="php" version="1.0">
  <xsl:output method="html" version="1.0" encoding="utf-8" indent="yes"  omit-xml-declaration="no"/>
.....
.....
....

<tr>
              <td valign="top" class="found_on">
                Added : <strong>
                  <xsl:value-of select ="php:function('ClsXSLTFunction::getPublishDate',$publishdate)"/>
                </strong> by <strong>
                  <xsl:value-of select="user"/>
                </strong>
              </td>
            </tr>
....
....
</xsl:stylesheet>

you can call your own function also in xslt but you have to create a class with static function like this

class ClsXSLTFunction
{
    public static function getPublishDuration($publishdate)
    {
        $epoch_1 = strtotime($publishdate);
        $epoch_2 = strtotime(date('y-m-d H:m:s'));
        $diff_seconds  = $epoch_1 - $epoch_2;
        $diff_weeks    = floor($diff_seconds/604800);
        $diff_seconds -= $diff_weeks   * 604800;
        $diff_days     = floor($diff_seconds/86400);
        $diff_seconds -= $diff_days    * 86400;
        $diff_hours    = floor($diff_seconds/3600);
        $diff_seconds -= $diff_hours   * 3600;
        $diff_minutes  = floor($diff_seconds/60);
        $diff_seconds -= $diff_minutes * 60;

        return $diff_days."d".$diff_hours."h".$diff_minutes."m";
    }

    public static function getPublishDate($publishdate)
    {
        return date("m/d/Y",strtotime($publishdate));
    }
}
Shekhar Gigras
  • 654
  • 3
  • 5