2007-05-28 02:55:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2008-02-25 21:21:12 +00:00
|
|
|
* Decorator/extender XSLT processor specifically for HTML documents.
|
2007-05-28 02:55:50 +00:00
|
|
|
*/
|
|
|
|
class ConfigDoc_HTMLXSLTProcessor
|
|
|
|
{
|
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
/**
|
|
|
|
* Instance of XSLTProcessor
|
|
|
|
*/
|
2007-05-28 02:55:50 +00:00
|
|
|
protected $xsltProcessor;
|
|
|
|
|
2008-02-25 21:21:12 +00:00
|
|
|
public function __construct($proc = false) {
|
|
|
|
if ($proc === false) $proc = new XSLTProcessor();
|
|
|
|
$this->xsltProcessor = $proc;
|
2007-05-28 02:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-02-25 21:21:12 +00:00
|
|
|
* @note Allows a string $xsl filename to be passed
|
2007-05-28 02:55:50 +00:00
|
|
|
*/
|
|
|
|
public function importStylesheet($xsl) {
|
|
|
|
if (is_string($xsl)) {
|
|
|
|
$xsl_file = $xsl;
|
|
|
|
$xsl = new DOMDocument();
|
|
|
|
$xsl->load($xsl_file);
|
|
|
|
}
|
|
|
|
return $this->xsltProcessor->importStylesheet($xsl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-11-25 02:24:39 +00:00
|
|
|
* Transforms an XML file into compatible XHTML based on the stylesheet
|
2008-04-22 01:58:06 +00:00
|
|
|
* @param $xml XML DOM tree, or string filename
|
2007-11-25 02:24:39 +00:00
|
|
|
* @return string HTML output
|
|
|
|
* @todo Rename to transformToXHTML, as transformToHTML is misleading
|
2007-05-28 02:55:50 +00:00
|
|
|
*/
|
|
|
|
public function transformToHTML($xml) {
|
2008-04-22 01:58:06 +00:00
|
|
|
if (is_string($xml)) {
|
|
|
|
$dom = new DOMDocument();
|
|
|
|
$dom->load($xml);
|
|
|
|
} else {
|
|
|
|
$dom = $xml;
|
|
|
|
}
|
|
|
|
$out = $this->xsltProcessor->transformToXML($dom);
|
2007-05-28 02:55:50 +00:00
|
|
|
|
|
|
|
// fudges for HTML backwards compatibility
|
2007-11-25 02:24:39 +00:00
|
|
|
// assumes that document is XHTML
|
2007-05-28 02:55:50 +00:00
|
|
|
$out = str_replace('/>', ' />', $out); // <br /> not <br/>
|
|
|
|
$out = str_replace(' xmlns=""', '', $out); // rm unnecessary xmlns
|
2007-11-25 02:24:39 +00:00
|
|
|
|
2007-05-28 02:55:50 +00:00
|
|
|
if (class_exists('Tidy')) {
|
|
|
|
// cleanup output
|
|
|
|
$config = array(
|
|
|
|
'indent' => true,
|
|
|
|
'output-xhtml' => true,
|
|
|
|
'wrap' => 80
|
|
|
|
);
|
|
|
|
$tidy = new Tidy;
|
|
|
|
$tidy->parseString($out, $config, 'utf8');
|
|
|
|
$tidy->cleanRepair();
|
|
|
|
$out = (string) $tidy;
|
|
|
|
}
|
2007-11-25 02:24:39 +00:00
|
|
|
|
2007-05-28 02:55:50 +00:00
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
/**
|
|
|
|
* Bulk sets parameters for the XSL stylesheet
|
|
|
|
* @param array $options Associative array of options to set
|
|
|
|
*/
|
2007-05-28 03:33:12 +00:00
|
|
|
public function setParameters($options) {
|
|
|
|
foreach ($options as $name => $value) {
|
|
|
|
$this->xsltProcessor->setParameter('', $name, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-25 21:21:12 +00:00
|
|
|
/**
|
|
|
|
* Forward any other calls to the XSLT processor
|
|
|
|
*/
|
|
|
|
public function __call($name, $arguments) {
|
|
|
|
call_user_func_array(array($this->xsltProcessor, $name), $arguments);
|
|
|
|
}
|
|
|
|
|
2007-05-28 02:55:50 +00:00
|
|
|
}
|
|
|
|
|