/** * @param string $inFile * @param string $xssFile * @param string $outFileOrDir * @throws \Exception */ public static function applyXslt($inFile, $xssFile, $outFileOrDir) { if (!file_exists($inFile)) { throw new \Exception("File {$inFile} cannot be found"); } if (!file_exists($xssFile)) { throw new \Exception("File {$xssFile} cannot be found"); } // Load the XML source $xml = new \DOMDocument(); $xml->load($inFile); $xsl = new \DOMDocument(); $xsl->load($xssFile); // Configure the transformer $processor = new \XSLTProcessor(); if (version_compare(PHP_VERSION, '5.4', "<")) { if (defined('XSL_SECPREF_WRITE_FILE')) { ini_set("xsl.security_prefs", XSL_SECPREF_CREATE_DIRECTORY | XSL_SECPREF_WRITE_FILE); } } else { // the php online docs only mention setSecurityPrefs, but somehow some installs have setSecurityPreferences... if (method_exists('XSLTProcessor', 'setSecurityPrefs')) { $processor->setSecurityPrefs(XSL_SECPREF_CREATE_DIRECTORY | XSL_SECPREF_WRITE_FILE); } else { $processor->setSecurityPreferences(XSL_SECPREF_CREATE_DIRECTORY | XSL_SECPREF_WRITE_FILE); } } $processor->importStyleSheet($xsl); // attach the xsl rules if (is_dir($outFileOrDir)) { if (!$processor->setParameter('', 'base.dir', realpath($outFileOrDir))) { echo "setting param base.dir KO\n"; } } $out = $processor->transformToXML($xml); if (!is_dir($outFileOrDir)) { file_put_contents($outFileOrDir, $out); } }
/** * Transforms the DOM document */ protected function transform(DOMDocument $document) { if (!$this->toDir->exists()) { throw new BuildException("Directory '" . $this->toDir . "' does not exist"); } $xslfile = $this->getStyleSheet(); $xsl = new DOMDocument(); $xsl->load($xslfile->getAbsolutePath()); $proc = new XSLTProcessor(); if (defined('XSL_SECPREF_WRITE_FILE')) { if (version_compare(PHP_VERSION, '5.4', "<")) { ini_set("xsl.security_prefs", XSL_SECPREF_WRITE_FILE | XSL_SECPREF_CREATE_DIRECTORY); } else { $proc->setSecurityPrefs(XSL_SECPREF_WRITE_FILE | XSL_SECPREF_CREATE_DIRECTORY); } } $proc->importStyleSheet($xsl); $proc->setParameter('', 'output.sorttable', $this->useSortTable); if ($this->format == "noframes") { $writer = new FileWriter(new PhingFile($this->toDir, "phpunit-noframes.html")); $writer->write($proc->transformToXML($document)); $writer->close(); } else { ExtendedFileStream::registerStream(); $toDir = (string) $this->toDir; // urlencode() the path if we're on Windows if (FileSystem::getFileSystem()->getSeparator() == '\\') { $toDir = urlencode($toDir); } // no output for the framed report // it's all done by extension... $proc->setParameter('', 'output.dir', $toDir); $proc->transformToXML($document); ExtendedFileStream::unregisterStream(); } }
/** * Try to process the XSLT transformation * * @param string XML to process. * @param string XSLT sheet to use for the processing. * * @throws BuildException On XSLT errors */ protected function process($xml, $xsl) { $processor = new XSLTProcessor(); // Create and setup document. $xmlDom = new DOMDocument(); $xmlDom->resolveExternals = $this->resolveDocumentExternals; // Create and setup stylesheet. $xslDom = new DOMDocument(); $xslDom->resolveExternals = $this->resolveStylesheetExternals; if ($this->html) { $xmlDom->loadHTML($xml); } else { $xmlDom->loadXML($xml); } $xslDom->loadxml($xsl); if (defined('XSL_SECPREF_WRITE_FILE')) { if (version_compare(PHP_VERSION, '5.4', "<")) { ini_set("xsl.security_prefs", XSL_SECPREF_WRITE_FILE | XSL_SECPREF_CREATE_DIRECTORY); } else { $processor->setSecurityPrefs(XSL_SECPREF_WRITE_FILE | XSL_SECPREF_CREATE_DIRECTORY); } } $processor->importStylesheet($xslDom); // ignoring param "type" attrib, because // we're only supporting direct XSL params right now foreach ($this->xsltParams as $param) { $this->log("Setting XSLT param: " . $param->getName() . "=>" . $param->getExpression(), Project::MSG_DEBUG); $processor->setParameter(null, $param->getName(), $param->getExpression()); } $errorlevel = error_reporting(); error_reporting($errorlevel & ~E_WARNING); @($result = $processor->transformToXML($xmlDom)); error_reporting($errorlevel); if (false === $result) { //$errno = xslt_errno($processor); //$err = xslt_error($processor); throw new BuildException("XSLT Error"); } else { return $result; } }