2007-05-28 02:55:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2007-11-25 02:24:39 +00:00
|
|
|
* Special XSLT processor specifically for HTML documents. Loosely
|
|
|
|
* based off of XSLTProcessor, but does not inherit from that class
|
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;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->xsltProcessor = new XSLTProcessor();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imports stylesheet for processor to use
|
|
|
|
* @param $xsl XSLT DOM tree, or filename of the XSL transformation
|
2007-11-25 02:24:39 +00:00
|
|
|
* @return bool Success?
|
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
|
2007-05-28 02:55:50 +00:00
|
|
|
* @param $xml XML DOM tree
|
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) {
|
|
|
|
$out = $this->xsltProcessor->transformToXML($xml);
|
|
|
|
|
|
|
|
// 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-06-24 22:22:00 +00:00
|
|
|
$out = str_replace(' xmlns="http://www.w3.org/1999/xhtml"', '', $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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-28 02:55:50 +00:00
|
|
|
}
|
|
|
|
|