xslt::transform

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function transform($what, $stylesheet = null, $functions = null, $options = array()) {
$options = array_merge(array('WEB_ROOT' => cfg::WEB_ROOT, 'VERSION' => cfg::VERSION), $options);
$xslt = new xsltProcessor;
$xslt->importStyleSheet(DomDocument::load(cfg::FS_ROOT . '/lib/xsl/' . $stylesheet));
$xslt->registerPHPFunctions($functions);
$xslt->setParameter('http://php.net/xsl', $options);
switch (true) {
case is_string($what):
if(file_exists($what)) {
$what = DomDocument::load($what);
}else{
$what = DomDocument::loadXML($what);
}
break;
case is_object($what):
if(get_class($what) == 'DOMDocument') {
break;
}
default: // $what must be DOMDocument or string file name or raw xml.
throw new Exception('XSLTTransform: wrong $what type');
break;
}
return $xslt->transformToXML($what);
}