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.

3

1 Answer 1

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));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.