簡體   English   中英

如何在PHP5中使用XSLT函數?

[英]How can I use XSLT functions with PHP5?

最近,我一直在幫助某人將站點從一個遠程Web服務器移至本地Web服務器。 但是,我繼承的代碼包含如下代碼:

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

但是,PHP5不喜歡這樣的代碼,並且據我所讀,在PHP5中不允許使用上述的XSLT函數。 顯然,此代碼必須是用PHP4編寫的,並且必須在運行PHP4的服務器上使用。 我的問題是,如何使此代碼在我的PHP5服務器上工作(使用“ XSLT”的代碼更多,以上僅為代碼片段)? 重寫所有代碼是使其正常工作的唯一方法嗎? 如果可能的話,我寧願避免這種情況。

我希望它能解決您的問題

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 );
        }

你必須像這樣在xslt中調用php函數

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

您也可以在xslt中調用自己的函數,但必須使用此類的靜態函數創建一個類

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));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM