我希望它能解决你的问题
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));
}
}