function pullStats($set) { $xp = new XsltProcessor(); // create a DOM document and load the XSL stylesheet $xsl = new DomDocument(); $xsl->load('stats.xslt'); // import the XSL styelsheet into the XSLT process $xp->importStylesheet($xsl); // create a DOM document and load the XML datat $xml_doc = new DomDocument(); $xml_doc->load('xmlcache/' . $set . '.xml'); // transform the XML into HTML using the XSL file if ($xml = $xp->transformToXML($xml_doc)) { $stats_xml = simplexml_load_string($xml); $temp_bottom = array(); $temp_top = array(); foreach ($stats_xml->top as $top) { array_push($temp_top, (string) $top); } foreach ($stats_xml->bottom as $bottom) { array_push($temp_bottom, (string) $bottom); } $temp_return = array('bottom' => $temp_bottom, 'top' => $temp_top, 'total' => (string) $stats_xml->total); return $temp_return; } }
public function TeiDisplay($file, array $options = array()) { if ($file->getExtension() != "xml") { return ""; } //queue_css_file('tei_display_public', 'screen', false, "plugins/TeiDisplay/views/public/css"); //echo "<h3>displaying ", $file->original_filename, "</h3><br/>"; $files = $file->getItem()->Files; foreach ($files as $f) { if ($f->getExtension() == "xsl") { $xsl_file = $f; } if ($f->getExtension() == "css") { $css_file = $f; } } //queue_css_url($css_file->getWebPath()); echo '<link rel="stylesheet" media="screen" href="' . $css_file->getWebPath() . '"/>'; //echo "transforming with ", $xsl_file->original_filename, "<br/>"; $xp = new XsltProcessor(); $xsl = new DomDocument(); //echo "loading ", "files/original/".$xsl_file->filename, "<br/>"; $xsl->load("files/original/" . $xsl_file->filename); $xp->importStylesheet($xsl); $xml_doc = new DomDocument(); //echo "loading ", "files/original/".$file->filename, "<br/>"; $xml_doc->load("files/original/" . $file->filename); try { if ($doc = $xp->transformToXML($xml_doc)) { return $doc; } } catch (Exception $e) { $this->view->error = $e->getMessage(); } }
/** * Simple XSLT transformation function * * @param mixed $xml DOMDocument or string containing xml * @param string $strXsltPath Relative file path to xslt document. Will look in both library location and * local app location for documents, and combine them so local overrides library * templates, if neccesary. * @param array $arrParams [optional] array of parameters to pass to stylesheet * @param bool $bolDoc [optional] return result as DOMDocument (default false) * @param array $arrInclude [optional] additional stylesheets that should be included in the transform * @return mixed newly formatted document as string or DOMDocument * @static */ public static function transform($xml, $strXsltPath, $arrParams = null, $bolDoc = false, $arrInclude = array()) { if ($strXsltPath == "") { throw new Exception("no stylesheet supplied"); } if (is_string($xml)) { // load xml document from string $objXml = new DOMDocument(); $objXml->loadXML($xml); $xml = $objXml; } $objXsl = self::generateBaseXsl($strXsltPath, $arrInclude); // create XSLT Processor $objProcessor = new XsltProcessor(); $objProcessor->registerPhpFunctions(); if ($arrParams != null) { // add in parameters foreach ($arrParams as $key => $value) { $objProcessor->setParameter(null, $key, $value); } } // transform $objXsl = $objProcessor->importStylesheet($objXsl); if ($bolDoc == true) { return $objProcessor->transformToDoc($xml); } else { return $objProcessor->transformToXml($xml); } }
/** * Render the table. * * @param mixed $tableDom * @param mixed $config */ public function render(Document $reportDom, Config $config) { $template = $config['template']; $out = $config['file']; if (!file_exists($template)) { throw new \RuntimeException(sprintf('XSLT template file "%s" does not exist', $template)); } $stylesheetDom = new \DOMDocument('1.0'); $stylesheetDom->load($template); $xsltProcessor = new \XsltProcessor(); $xsltProcessor->importStylesheet($stylesheetDom); $xsltProcessor->setParameter(null, 'title', $config['title']); $xsltProcessor->setParameter(null, 'phpbench-version', PhpBench::VERSION); $xsltProcessor->setParameter(null, 'date', date('Y-m-d H:i:s')); $output = $xsltProcessor->transformToXml($reportDom); if (!$output) { throw new \InvalidArgumentException(sprintf('Could not render report with XSL file "%s"', $template)); } if (null !== $out) { file_put_contents($out, $output); $this->output->writeln('Dumped XSLT report:'); $this->output->writeln($out); } else { $this->output->write($output); } }
protected function _processTransform(DomDocument $domDocument, DomDocument $xslTemplate) { $this->_processor->importStylesheet($xslTemplate); $transformedXml = $this->_processor->transformToXml($domDocument); $transformedXml = str_replace(array("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", "<?xml version=\"1.0\"?>\n"), '', $transformedXml); return $transformedXml; }
/** * Simple, dynamic xsl transform */ protected function transform($xml, $path_to_xsl, $output_type = null, array $params = array(), array $import_array = array(), $to_string = true) { if ($path_to_xsl == "") { throw new \Exception("no stylesheet supplied"); } // make sure we have a domdocument if (is_string($xml)) { $xml = Parser::convertToDOMDocument($xml); } // create xslt processor $processor = new \XsltProcessor(); $processor->registerPhpFunctions(); // add parameters foreach ($params as $key => $value) { $processor->setParameter(null, $key, $value); } // add stylesheet $xsl = $this->generateBaseXsl($path_to_xsl, $import_array, $output_type); $processor->importStylesheet($xsl); // transform if ($to_string == true) { return $processor->transformToXml($xml); } else { return $processor->transformToDoc($xml); } }
function get_document($aNode) { //get the directory in which the documents are contained from the config.php file global $xml_dir, $html_dir, $xslt, $html_xslt; //create the xslt $xp = new XsltProcessor(); // create a DOM document and load the XSL stylesheet $xsl = new DomDocument(); $xsl->load($xslt); // import the XSL styelsheet into the XSLT process $xp->importStylesheet($xsl); //open the xml document $xml = new DomDocument(); $xml->loadXML($aNode); //transform if ($html = $xp->transformToXML($xml)) { $return = str_replace('<?xml version="1.0"?>' . "\n", "", $html); $return = str_replace('<!DOCTYPE div PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">' . "\n", "", $return); $return = str_replace("\n", "", $return); $return = str_replace("\n ", "", $return); return str_replace("\t", "", $return); } else { trigger_error('XSL transformation failed.', E_USER_ERROR); } }
function pubmed_metadata($pmid, &$item) { global $debug; $ok = false; $url = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?' . 'retmode=xml' . '&db=pubmed' . '&id=' . $pmid; //echo $url; $xml = get($url); //echo $xml; if (preg_match('/<\\?xml /', $xml)) { $ok = true; if ($debug) { echo $xml; } $dom = new DOMDocument(); $dom->loadXML($xml); $xpath = new DOMXPath($dom); /* $nodeCollection = $xpath->query ("//crossref/error"); foreach($nodeCollection as $node) { $ok = false; } if ($ok) {*/ // Get JSON $xp = new XsltProcessor(); $xsl = new DomDocument(); $xsl->load('xsl/pubmed2JSON.xsl'); $xp->importStylesheet($xsl); $xml_doc = new DOMDocument(); $xml_doc->loadXML($xml); $json = $xp->transformToXML($xml_doc); //echo $json; $item = json_decode($json); // post process // Ensure metadata is OK (assumes a journal for now) if (!isset($item->issn)) { $issn = ''; if (isset($item->title)) { $issn = issn_from_journal_title($item->title); } if ($issn == '') { if (isset($item->eissn)) { $issn = $item->eissn; } } if ($issn != '') { $item->issn = $issn; } } if ($debug) { echo '<h3>Boo</h3>'; print_r($item); } } return $ok; }
/** * @param $path * @param array $data * @return string */ protected function evaluatePath($path, array $data = []) { $preferences = $this->XSLTSimple->addChild('Preferences'); $url = $preferences->addChild('url'); $url->addAttribute('isHttps', Request::secure()); $url->addAttribute('currentUrl', Request::url()); $url->addAttribute('baseUrl', URL::to('')); $url->addAttribute('previousUrl', URL::previous()); $server = $preferences->addChild('server'); $server->addAttribute('curretnYear', date('Y')); $server->addAttribute('curretnMonth', date('m')); $server->addAttribute('curretnDay', date('d')); $server->addAttribute('currentDateTime', date('Y-m-d H:i:s')); $language = $preferences->addChild('language'); $language->addAttribute('current', App::getLocale()); $default_language = \Config::get('app.default_language'); if (isset($default_language)) { $language->addAttribute('default', $default_language); } $languages = \Config::get('app.available_languages'); if (is_array($languages)) { foreach ($languages as $lang) { $language->addChild('item', $lang); } } // from form generator if (isset($data['form'])) { $this->XSLTSimple->addChild('Form', form($data['form'])); } // adding form errors to xml if (isset($data['errors'])) { $this->XSLTSimple->addData($data['errors']->all(), 'FormErrors', false); } // "barryvdh/laravel-debugbar": // adding XML tab if (true === class_exists('Debugbar')) { $dom = dom_import_simplexml($this->XSLTSimple)->ownerDocument; $dom->preserveWhiteSpace = false; $dom->formatOutput = true; $prettyXml = $dom->saveXML(); // add new tab and append xml to it if (false === \Debugbar::hasCollector('XML')) { \Debugbar::addCollector(new \DebugBar\DataCollector\MessagesCollector('XML')); } \Debugbar::getCollector('XML')->addMessage($prettyXml, 'info', false); } $xsl_processor = new \XsltProcessor(); $xsl_processor->registerPHPFunctions(); $xsl_processor->importStylesheet(simplexml_load_file($path)); return $xsl_processor->transformToXML($this->XSLTSimple); }
function getInterpretedXslt($xslPath, $xmlPath, $xsltProcessor = null) { if ($xsltProcessor == null) { $xsltProcessor = new XsltProcessor(); $xsltProcessor->registerPHPFunctions(); } $xsl = new DOMDocument(); $xsl->load($xslPath); $xsltProcessor->importStylesheet($xsl); $xml = new DOMDocument(); $xml->load($xmlPath); $output = $xsltProcessor->transformToXML($xml) or die('Transformation error!'); return $output; }
/** * Execute render process * @param string $templatePath * @param \DOMDocument $source * @param array $parameters * @return string */ public function execute($templatePath, \DOMDocument $source, array $parameters = array()) { $xsl = new \DomDocument(); $xsl->load($templatePath); $processor = new \XsltProcessor(); $processor->importStylesheet($xsl); $outputDom = $processor->transformToDoc($source); if ($parameters['output.type'] && $parameters['output.type'] == 'xml') { $result = $outputDom->saveXML(); } else { $result = $outputDom->saveHTML(); } return $result; }
public function xhtmlAction() { $xml = DOCS_PATH . $this->view->docid . '.xml'; $xsl = APPLICATION_PATH . 'modules/contingent/controllers/xml2html.xsl'; $doc = new DOMDocument(); $doc->substituteEntities = TRUE; $doc->load($xsl); $proc = new XsltProcessor(); $proc->importStylesheet($doc); $doc->load($xml); $proc->setParameter('', 'contextPath', '/'); $proc->setParameter('', 'nodeResPath', '/res/' . $this->view->docid . '/'); echo $proc->transformToXml($doc); }
public function run($args) { // Get variables from args array passed into detached process. $filepath = $args['filepath']; $filename = !empty($args['csv_filename']) ? $args['csv_filename'] : pathinfo($filename, PATHINFO_BASENAME); $format = $args['format']; $itemTypeId = $args['item_type_id']; $collectionId = $args['collection_id']; $createCollections = $args['create_collections']; $recordsArePublic = $args['public']; $recordsAreFeatured = $args['featured']; $elementsAreHtml = $args['html_elements']; $containsExtraData = $args['extra_data']; $tagName = $args['tag_name']; $columnDelimiter = $args['column_delimiter']; $enclosure = $args['enclosure']; $elementDelimiter = $args['element_delimiter']; $tagDelimiter = $args['tag_delimiter']; $fileDelimiter = $args['file_delimiter']; // TODO Intermediate stylesheets are not managed currently. // $stylesheetIntermediate = $args['stylesheet_intermediate']; $stylesheetParameters = $args['stylesheet_parameters']; $stylesheet = !empty($args['stylesheet']) ? $args['stylesheet'] : get_option('xml_import_xsl_directory') . DIRECTORY_SEPARATOR . get_option('xml_import_stylesheet'); $csvfilesdir = !empty($args['destination_dir']) ? $args['destination_dir'] : sys_get_temp_dir(); // Create a DOM document and load the XML data. $xml_doc = new DomDocument(); $xml_doc->load($filepath); // Create a DOM document and load the XSL stylesheet. $xsl = new DomDocument(); $xsl->load($stylesheet); // Import the XSL styelsheet into the XSLT process. $xp = new XsltProcessor(); $xp->setParameter('', 'node', $tagName); $xp->importStylesheet($xsl); // Write transformed xml file to the temp csv file. try { if ($doc = $xp->transformToXML($xml_doc)) { $csvFilename = $csvfilesdir . DIRECTORY_SEPARATOR . pathinfo($filename, PATHINFO_FILENAME) . '.csv'; $documentFile = fopen($csvFilename, 'w'); fwrite($documentFile, $doc); fclose($documentFile); //$this->_initializeCsvImport($basename, $recordsArePublic, $recordsAreFeatured, $collectionId); $this->_helper->flashMessenger(__('Successfully generated CSV File')); } else { $this->_helper->flashMessenger(__('Could not transform XML file. Be sure your XML document is valid.'), 'error'); } } catch (Exception $e) { $this->view->error = $e->getMessage(); } }
private function xsltTransform($xmlStr, $xslFile, $toDom = false) { $doc = new DOMDocument(); $doc->substituteEntities = TRUE; // $doc->resolveExternals = TRUE; $doc->load($xslFile); $proc = new XsltProcessor(); $proc->importStylesheet($doc); $doc->loadXML($xmlStr); if ($toDom) { return $proc->transformToDoc($doc); } else { return $proc->transformToXml($doc); } }
public function xhtmlAction() { $this->_helper->viewRenderer->setNoRender(); $xml = DOCS_PATH . $this->view->docid . '.xml'; $xsl = APPLICATION_PATH . 'modules/site/controllers/xml2html.xsl'; $doc = new DOMDocument(); $doc->substituteEntities = TRUE; $doc->load($xsl); $proc = new XsltProcessor(); $proc->importStylesheet($doc); @$doc->load($xml); $proc->setParameter('', 'contextPath', '/'); $proc->setParameter('', 'nodeResPath', '/res/' . $this->view->docid . '/'); $proc->registerPHPFunctions('TypecontentController::widget'); echo $proc->transformToXml($doc); }
/** * Return a formatted html list of resources associated with a given * tag. * * The caller should check the $tr->status variable to see if the * query was successful. On anything but a 200 or 304, this method * returns nothing, but it would generally be useful to have a * warning for 204 (no resources yet for this tag) or 404 (tag not * found). * * @return string html for a list of resources referenced by the * given tag. * * */ public function resList() { if (strlen($this->xml) == 0) { $this->getData(); // args ? } if ($this->is_valid()) { $xsl = new DOMDocument(); $xsl->load($this->loc->xsl_dir . "public_resourcelist.xsl"); $proc = new XsltProcessor(); $xsl = $proc->importStylesheet($xsl); // possibly cached DOM $taglist = $proc->transformToDoc($this->xml_DOM()); $this->html = $taglist->saveXML(); return $this->html; } }
public function perform() { $action = $this->action; $xml_str = $action->perform(); $xml_doc = simplexml_load_string($xml_str); if (!is_null($this->stylesheet)) { $xp = new XsltProcessor(); $xsl = new DomDocument; $xsl->load($this->stylesheet); $xp->importStylesheet($xsl); if ($html = $xp->transformToXML($xml_doc)) { return $html; } else { throw new RtException('XSL transformation failed.'); } } else { throw new RtException("Couldn't go on without xslt"); } }
function getReportDefinitionForm() { $fileName = "c:/web/afids_reports/trunk/writer/xsl_templates/reportDefinition.xsl"; $xsl = new DomDocument(); if (!$xsl->load($fileName)) { echo 'failed' . $fileName; } $stylesheet = new XsltProcessor(); $stylesheet->importStylesheet($xsl); // pass the report name (id) to the xsl and the filtering is done there $stylesheet->setParameter(null, "reportName", $this->reportName); if (!($translatedResponse = $stylesheet->transformToXML($this->xml_report_definition))) { return "error"; } else { return $translatedResponse; //echo $translatedResponse; } }
function outputXML($projectList) { $dom = new DOMDocument('1.0'); $dom->formatOutput = true; //header("Content-Type: text/plain"); $root = $dom->createElement('SVNDump'); $dom->appendChild($root); foreach ($projectList as &$project) { $this->addProjectNode($dom, $root, $project); } $xsl = new DOMDocument('1.0'); $xsl->load("cs242_portfolio.xsl"); $proc = new XsltProcessor(); $xsl = $proc->importStylesheet($xsl); $newdom = $proc->transformToDoc($dom); echo $newdom->saveHTML(); //echo $dom->saveXML(); }
public function transform($xsl_file = null, $xml_data = null) { CI()->benchmark->mark('xsl_transform (' . $xsl_file . ')_start'); set_error_handler(array('XSL_Transform', 'handleError')); $xsl = new DOMDocument('1.0', 'UTF-8'); $xsl->load($this->_getXSL($xsl_file)); $inputdom = new DomDocument('1.0', 'UTF-8'); $inputdom->loadXML($this->_getXML($xml_data)); $proc = new XsltProcessor(); $proc->importStylesheet($xsl); $proc->registerPhpFunctions($this->_allowedFunctions()); $result = $proc->transformToXML($inputdom); // http://www.php.net/manual/en/xsltprocessor.transformtoxml.php#62081 //$result = $proc->transformToDoc($inputdom); //$result = $result->saveHTML(); restore_error_handler(); // Strip out any <?xml stuff at top out output $result = preg_replace('/\\<\\?xml.+\\?\\>/', '', $result, 1); CI()->benchmark->mark('xsl_transform (' . $xsl_file . ')_end'); return $result; }
/** * Retreives and formats a tag cloud for the current page. The html * remains available in $this->html. If html data is already * present, simply returns that. * * @param $url string (Optional) The url (or id) for which a cloud should be * made. Default is to use the current page. * @param $max_tags integer (Optional). Defaults to 0, meaning get all * the tags. * * @returns string The html cloud that is built. */ public function buildCloud($url = '', $max_tags = 0, $cloudtype = '') { if ($this->html) { return $this->html; } if (!$this->xml) { $this->getData($url ? $url : $this->url, $max_tags, $cloudtype); } if ($this->is_valid()) { $xsl = new DOMDocument(); $xsl->load($this->loc->xsl_dir . "publiccloud.xsl"); $proc = new XsltProcessor(); $xsl = $proc->importStylesheet($xsl); // setting url format so that it is not hard coded in the xsl. $proc->setParameter('', 'tagviewbase', $this->loc->server_web_path . 'tagview.php?tag='); //using cloud->xml_DOM() because this data might have been cached already. $cloud = $proc->transformToDoc($this->xml_DOM()); $this->html = $cloud->saveXML(); } return $this->html; }
/** * Render the table. * * @param mixed $tableDom * @param mixed $config */ public function render(Document $reportDom, Config $config) { $template = $config['template']; $out = strtr($config['file'], ['%report_name%' => $reportDom->firstChild->getAttribute('name')]); if (!file_exists($template)) { throw new \RuntimeException(sprintf('XSLT template file "%s" does not exist', $template)); } foreach ($reportDom->query('.//row') as $rowEl) { $formatterParams = []; foreach ($rowEl->query('./formatter-param') as $paramEl) { $formatterParams[$paramEl->getAttribute('name')] = $paramEl->nodeValue; } foreach ($rowEl->query('./cell') as $cellEl) { $value = $cellEl->nodeValue; if ('' !== $value && $cellEl->hasAttribute('class')) { $classes = explode(' ', $cellEl->getAttribute('class')); $value = $this->formatter->applyClasses($classes, $value, $formatterParams); $cellEl->nodeValue = $value; } } } $stylesheetDom = new \DOMDocument('1.0'); $stylesheetDom->load($template); $xsltProcessor = new \XsltProcessor(); $xsltProcessor->importStylesheet($stylesheetDom); $xsltProcessor->setParameter(null, 'title', $config['title']); $xsltProcessor->setParameter(null, 'phpbench-version', PhpBench::VERSION); $xsltProcessor->setParameter(null, 'date', date('Y-m-d H:i:s')); $output = $xsltProcessor->transformToXml($reportDom); if (!$output) { throw new \InvalidArgumentException(sprintf('Could not render report with XSL file "%s"', $template)); } if ($out) { file_put_contents($out, $output); $this->output->writeln('Dumped XSLT report:'); $this->output->writeln($out); } else { $this->output->write($output); } }
/** * @brief Fetch an uBio RSS feed, and convert to object for ease of processing * * We convert RSS to JSON to create object. We use conditional GET to check whether * feed has been modified. * * @param url Feed URL * @param data Object * * @return Result from RSS fetch (0 is OK, 304 is feed unchanged, anything else is an error) */ function ubio_fetch_rss($url, &$data) { $rss = ''; $msg = '200'; $result = GetRSS($url, $rss, true); if ($result == 0) { // Archive $dir = dirname(__FILE__) . '/tmp/' . date("Y-m-d"); if (!file_exists($dir)) { $oldumask = umask(0); mkdir($dir, 0777); umask($oldumask); } $rss_file_name = $dir . '/' . md5($url) . '.xml'; $rss_file = fopen($rss_file_name, "w+") or die("could't open file --\"{$rss_file_name}\""); fwrite($rss_file, $rss); fclose($rss_file); // Convert to JSON $xp = new XsltProcessor(); $xsl = new DomDocument(); $xsl->load(dirname(__FILE__) . '/xsl/ubiorss.xsl'); $xp->importStylesheet($xsl); $xml_doc = new DOMDocument(); $xml_doc->loadXML($rss); $json = $xp->transformToXML($xml_doc); $data = json_decode($json); } else { switch ($result) { case 304: $msg = 'Feed has not changed since last fetch (' . $result . ')'; break; default: $msg = 'Badness happened (' . $result . ') ' . $url; break; } } echo $msg, "\n"; return $result; }
function do_transformation($xml, $xsl, &$result, $option) { if ($option == 'xml') { header("Content-type: application/xml"); $result = $xml; return 1; } else { if ($option == 'xsl') { /** * Compatibility function wrappers. These are function's wrappers that make * available functions found on newer PHP versions (mostly 4.3 ones). */ $version = explode(".", phpversion()); $major = $version[0]; $minor = $version[1]; $release = $version[2]; if ($major >= 5) { $xmlDOM = new DomDocument(); $xslDOM = new DomDocument(); $xsltEngine = new XsltProcessor(); $xmlDOM->loadXML($xml); $xslDOM->load($xsl); $xsltEngine->importStylesheet($xslDOM); $transformation = $xsltEngine->transformToXml($xmlDOM); echo $transformation; return 1; } else { $xsl_proc = xslt_create(); xslt_set_encoding($xsl_proc, "ISO-8859-1"); $arg = array('/_xml' => $xml); $result = xslt_process($xsl_proc, $xml, $xsl, NULL, $arg); xslt_free($xsl_proc); return 1; } } else { return 0; } } }
/** * Transforms this class to HTML using the given stylesheet. */ function toHtml() { // create a DOM document and load the XSL stylesheet $xsl = new DomDocument(); $xsl->load($this->getXslFilename()); // import the XSL styelsheet into the XSLT process $xp = new XsltProcessor(); $xp->importStylesheet($xsl); // create a DOM document and load the XML datat $xml_doc = new DomDocument(); $xml_doc->loadXML($this->toXml()); // transform the XML into HTML using the XSL file if ($html = $xp->transformToXML($xml_doc)) { return $html; } else { //trigger_error('XSL transformation failed.', E_USER_ERROR); error_log("XSL transformation failed: " . $this->toXml()); return 'XSL transformation failed.'; } // if return 'XSL transformation failed.'; }
function get_element_tech_documentation($anElementName, $type) { global $documentation, $doc_xslt; //create the xslt $xp = new XsltProcessor(); //echo $documentation . $anElementName . '.html';exit; // create a DOM document and load the XSL stylesheet $xsl = new DomDocument(); $xsl->load($doc_xslt); // import the XSL styelsheet into the XSLT process $xp->importStylesheet($xsl); //open the xml document $xml = new DomDocument(); $xml->load($documentation . '/' . $anElementName . ".html"); //set the parameter //$xp->setParameter('', array('element' => $anElementName, 'type' => $type)); //transform if ($html = $xp->transformToXML($xml)) { return $html; } else { trigger_error('XSL transformation failed.', E_USER_ERROR); } }
function createSQLScripts() { $xp = new XsltProcessor(); // create a DOM document and load the XSL stylesheet $xsl = new DomDocument(); $xsl->load($_ENV['db_xslt_create']); // import the XSL styelsheet into the XSLT process $xp->importStylesheet($xsl); $xp->setParameter(null, 'target', 'drop'); // create a DOM document and load the XML datat $xml_doc = new DomDocument(); $xml_doc->load($_ENV['db_xml_file']); // transform the XML into HTML using the XSL file if ($result = $xp->transformToXML($xml_doc)) { // save result in uninstall.sql $handle = fopen($_ENV['sql_script_dir'] . 'uninstall.sql', 'w'); if (fwrite($handle, $result) === false) { echo 'Konnte die Datei ' . $_ENV['sql_script_dir'] . 'uninstall.sql nicht beschreiben.'; } fclose($handle); // save result as first command in install.sql $handle = fopen($_ENV['sql_script_dir'] . 'install.sql', 'w'); //if (fwrite($handle, $result) === false) { // echo 'Konnte die Datei ' .$_ENV['sql_script_dir'] . 'install.sql nicht beschreiben.'; //} // run the same with parameter create $xp->setParameter(null, 'target', 'create'); if ($result = $xp->transformToXML($xml_doc)) { if (fwrite($handle, $result) === false) { echo 'Konnte die Datei ' . $_ENV['sql_script_dir'] . 'install.sql nicht beschreiben.'; } } fclose($handle); } else { echo 'XSL transformation failed.'; } }
// fetch a BHL page image and highlight search terms $PageID = $_GET['PageID']; $term = $_GET['term']; $couch_id = 'page/' . $PageID; $resp = $couch->send("GET", "/" . $config['couchdb_options']['database'] . "/" . urlencode($couch_id)); $page = json_decode($resp); if (isset($page->error)) { // badness $html = 'Oops'; } else { $imageUrl = 'http://www.biodiversitylibrary.org/pagethumb/' . $PageID . ',400,400'; if (isset($page->xml)) { $xp = new XsltProcessor(); $xsl = new DomDocument(); $xsl->load(dirname(__FILE__) . '/djvu2html.xsl'); $xp->importStylesheet($xsl); // Load XML $dom = new DOMDocument(); $dom->loadXML($page->xml); $xpath = new DOMXPath($dom); // Export HTML with background image using XSLT $xp->setParameter('', 'imageUrl', $imageUrl); $xp->setParameter('', 'widthpx', 500); $xp->setParameter('', 'term', $term); $html = $xp->transformToXML($dom); } else { $html = '<span style="background-color:orange;color-white;">Warning: no XML!</span><img style="border:1px solid rgb(228,228,228);-webkit-filter: grayscale(100%) contrast(200%);" src="' . $imageUrl . '" width="500" />'; } } //echo $html; $data = new stdclass();
<article id="2"> <title>Something Else Happened</title> <author>Different Person</author> </article> </articles>'; $xsl = '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output indent="yes" method="xml" encoding="iso-8859-1" /> <xsl:template match="/"> <xsl:apply-templates select="//article"/> </xsl:template> <xsl:template match="article"> Title: <xsl:value-of select="title"/><br /> Author: <xsl:value-of select="author"/><br /> <hr /> </xsl:template> </xsl:stylesheet>'; /* load XSLT Stylesheet */ $xslDom = new domDocument(); $xslDom->loadXML($xsl); /* load XML data */ $xmlDom = new domDocument(); $xmlDom->loadXML($xml); $xsl = new XsltProcessor(); // instantiate XSLT processor $xsl->importStylesheet($xslDom); // load stylesheet echo $xsl->transformToXML($xmlDom); // perform the transformation & return HTML
/** * Merge the data and the XML template into an HTML page. * @param type $xslDocument the template. * @return type * @throws Dom\Exception */ public function transform($xslDocument) { if ($xslDocument->domDocument) { $processor = new \XsltProcessor(); $processor->importStylesheet($xslDocument->domDocument); $htmlDomDocument = new self(); $htmlDomDocument->domDocument = $processor->transformToDoc($this->domDocument); if (!$htmlDomDocument->domDocument) { throw new Dom\Exception('The XSL transformation has failed.'); } } else { throw new Dom\Exception('There is no XSL template to transform with.'); } try { return $htmlDomDocument->toString(false); } catch (\Exception $exception) { $this->logger->error($exception->getMessage()); throw new Dom\Exception("dom-transform-failed"); } }