function process() { // Load the XML source $xml = new DOMDocument(); $xml->load(SRC_DIR . '/set.xml'); // Interpolate all XInclude directives. $xml->xinclude(); // Load the XSLT stylesheet. $xsl = new DOMDocument(); $xsl->load(DOCBOOK_STYLESHEET_DIR . '/html/docbook.xsl'); // Configure the transformer $proc = new XSLTProcessor(); if (!$proc->hasExsltSupport()) { throw new Exception('EXSLT Support not available.'); } $proc->importStyleSheet($xsl); // attach the xsl rules //file_put_contents('test.html', $proc->transformToXml($xml)); $book = $xml->getElementById('tm'); var_dump($book); var_dump(gettype($book)); file_put_contents('test-book.html', $proc->transformToXml($book)); }
<?php $proc = new XSLTProcessor(); var_dump($proc->hasExsltSupport('stringValue'));
<?php include "pagVariables.php"; $proc = new XSLTProcessor(); if (!$proc->hasExsltSupport()) { die('EXSLT support not available'); } error_reporting(7); // Load the XML source $xml = new DOMDocument(); $xml->load('usOrfeo.xml'); $xsl = new DOMDocument(); $xsl->load('jh.xsl'); // Configure the transformer if (!$cOrden or !$cOrdenType) { $cOrden = "DEPE_CODI"; $cOrdenDType = "number"; $cOrdenType = "descending"; } if ($cOrdenType == "descending") { $cOrdenType = "ascending"; } else { $cOrdenType = "descending"; } $proc = new XSLTProcessor(); $proc->setParameter('', 'cOrdenDType', $cOrdenDType); $proc->setParameter('', 'cOrdenType', $cOrdenType); $proc->setParameter('', 'cOrden', $cOrden); $proc->setParameter('', 'pos1', 0); $proc->setParameter('', 'pos1', 1); $proc->importStyleSheet($xsl);
<?php $proc = new XSLTProcessor(); var_dump($proc->hasExsltSupport());
private function _xslt_transform($xml, $xsl, $name = '') { //log_message('debug', 'starting transformation'); $result = new SimpleXMLElement('<root></root>'); $proc = new XSLTProcessor(); if (!$proc->hasExsltSupport()) { log_message('error', 'XSLT Processor at server has no EXSLT Support'); } else { $start = microtime(true); //restore error handler to PHP to 'catch' libxml 'errors' restore_error_handler(); libxml_use_internal_errors(true); //clear any previous errors libxml_clear_errors(); //import XSLT stylesheet $proc->importStyleSheet($xsl); //profile transformation (only turn on for development!) //$proc->setProfiling(APPPATH.'logs/XSLTprofiling_'.$name.'.txt'); //transform $start_time = time(); $output = $proc->transformToXML($xml); log_message('debug', 'xlst transformation time: ' . (time() - $start_time) . ' seconds'); $errors = libxml_get_errors(); //empty errors libxml_clear_errors(); //restore CI error handler set_error_handler('_exception_handler'); if ($output) { $result = simplexml_load_string($output); //log_message('debug', 'form:'.$result->saveXML()); } array_push($errors, (object) array('message' => 'XML to HTML transformation for ' . $name . ' took ' . round(microtime(true) - $start, 2) . ' seconds', 'level' => 0)); $errors = $this->_error_msg_process($errors); $result = $this->_add_errors($errors, 'xsltmessages', $result); } return $result; }
/** * Converts the currently opened document to XHTML. * * This function does not modify the original document, but * modifications on the document will reflect on the result of * the conversion. * * You may want to call setXSLTransformation() and/or * setXSLOption() before calling this function to optimize the * result of the conversion for particular purposes. * * @param filename Filename of the resulting XHTML document. * * @return @c true if the document was successfully converted, * @c false otherwise. * * @sa setXSLTransformation(), setXSLOption() */ public function convertToXHTML($filename) { if ($this->filename == '') { trigger_error('No document was loaded for conversion to XHTML'); return false; } $xslDocument = new DOMDocument(); if ($xslDocument->load(LIBOPENDOCUMENT_PATH . "/xsl/{$this->xslTransformation}/document2xhtml.xsl") === false) { trigger_error('Could not open the selected XSL transformation'); return false; } $xsltProcessor = new XSLTProcessor(); if ($xsltProcessor->hasExsltSupport() == false) { trigger_error('EXSLT support in PHP is required for converting OpenDocument files'); return false; } if ($this->package == false) { $sourceDocument = $this->metaDocument; } else { if ($this->xslOptions['export-objects'] == 'true') { $this->exportObjects(dirname($filename)); } $preprocessXSLDocument = new DOMDocument(); if (copy(LIBOPENDOCUMENT_PATH . "/xsl/package2document.xsl", "{$this->tmpdir}/package2document.xsl") === false || $preprocessXSLDocument->load("{$this->tmpdir}/package2document.xsl") === false) { trigger_error('Could not open the XSL transformation for pre-processing'); return false; } $xsltProcessor->importStyleSheet($preprocessXSLDocument); $contentDocument = new DOMDocument(); if ($contentDocument->load("{$this->tmpdir}/content.xml") == false) { trigger_error('Could not load content XML document'); return false; } $xsltProcessor->setParameter('', 'mimetype', file_get_contents("{$this->tmpdir}/mimetype")); $sourceDocument = $xsltProcessor->transformToDoc($contentDocument); if ($sourceDocument === false) { trigger_error('Could not open source document'); return false; } unlink("{$this->tmpdir}/package2document.xsl"); } if (file_exists(LIBOPENDOCUMENT_PATH . "/xsl/{$this->xslTransformation}/document2xhtml.php") == true) { include_once LIBOPENDOCUMENT_PATH . "/xsl/{$this->xslTransformation}/document2xhtml.php"; OpenDocument2XHTML::init($this->xslOptions); $xsltProcessor->registerPHPFunctions(); } $xsltProcessor->importStyleSheet($xslDocument); foreach ($this->xslOptions as $key => $value) { $xsltProcessor->setParameter('', $key, $value); } if ($xsltProcessor->transformToURI($sourceDocument, $filename) == false) { trigger_error('XSL transformation failed to run'); return false; } return true; }
/** * Performs a xslt transformation. * * @param DOMDocument $xml * The document to be converted. * @param DOMDocument $xsl * The document to use as stylesheet for the conversion. * * @return array * ['sxe'] => The resulting SimpleXMLElement or FALSE if the transformation failed. * ['errors'] => Occurred errors as returned by libxml_get_errors() */ private function _xslt_transform($xml, $xsl) { $proc = new XSLTProcessor(); if (!$proc->hasExsltSupport()) { throw new Exception("XSLT Processor at server has no EXSLT Support"); } else { // Restore error handler to PHP to 'catch' libxml 'errors' restore_error_handler(); libxml_use_internal_errors(true); //clear any previous errors libxml_clear_errors(); // Import XSLT stylesheet $proc->importStyleSheet($xsl); // Transform $output = $proc->transformToXML($xml); // Get errors $errors = libxml_get_errors(); // Empty errors libxml_clear_errors(); // Restore CI error handler set_error_handler('_exception_handler'); if ($output) { return array('sxe' => simplexml_load_string($output), 'errors' => $errors); } } return array('sxe' => FALSE, 'errors' => $errors); }