/** * process xsl template with Flow xml * * @param string xsl source file * @return string xsl transformation output */ public function process($xslfile) { $flow = Nexista_Flow::Singleton('Nexista_Flow'); // The following can be used with the NYT xslt cache. $use_xslt_cache = "yes"; if (!is_file($xslfile)) { Nexista_Error::init('XSL Handler - Error processing XSL file, it is unavailable: ' . $xslfile, NX_ERROR_FATAL); } if ($use_xslt_cache != "yes" || !class_exists('xsltCache')) { $xsl = new DomDocument('1.0', 'UTF-8'); $xsl->substituteEntities = false; $xsl->resolveExternals = false; $xslfilecontents .= file_get_contents($xslfile); $xsl->loadXML($xslfilecontents); $xsl->documentURI = $xslfile; $xslHandler = new XsltProcessor(); $xslHandler->importStyleSheet($xsl); if (4 == 3 && function_exists('setProfiling')) { $xslHandler->setProfiling("/tmp/xslt-profiling.txt"); } } else { $xslHandler = new xsltCache(); $xslHandler->importStyleSheet($xslfile); } $output = $xslHandler->transformToXML($flow->flowDocument); if ($output === false) { Nexista_Error::init('XSL Handler - Error processing XSL file: ' . $xslfile, NX_ERROR_FATAL); } return $output; }
/** * Applies action * * @return boolean success */ protected function main() { $flow = Nexista_Flow::Singleton('Nexista_Flow'); $file_path = Nexista_Path::parseInlineFlow($this->params['xsl']); $xslfile = NX_PATH_APPS . $file_path; if (!is_file($xslfile)) { Nexista_Error::init('XSL Action - file unavailable: ' . $xslfile, NX_ERROR_FATAL); } $xsl = new DomDocument('1.0', 'UTF-8'); $xsl->substituteEntities = false; $xsl->resolveExternals = false; $xslfilecontents .= file_get_contents($xslfile); $xsl->loadXML($xslfilecontents); $xsl->documentURI = $xslfile; $use_xslt_cache = "yes"; if ($use_xslt_cache != "yes" || !class_exists('xsltCache')) { $xslHandler = new XsltProcessor(); } else { $xslHandler = new xsltCache(); } $xslHandler->importStyleSheet($xsl); $my_output = $xslHandler->transformToXML($flow->flowDocument); if ($my_output === false) { Nexista_Error::init('XSL Action - Error processing XSL file: ' . $xslfile, NX_ERROR_FATAL); return false; } $new_node = $this->params['new_node']; Nexista_Flow::add($new_node, $my_output); return true; }
public static function transform($xml, $xsltName, $mimeType = 'text/html', $status = 200) { if (null !== self::$lock) { Logger::backtrace('XSLT recursion: ' . $xsltName . ' while in ' . self::$lock); throw new RuntimeException(t('Рекурсия в XSLT недопустима.')); } $mode = empty($_GET['xslt']) ? 'server' : $_GET['xslt']; $xml = self::fixEntities($xml); if ('none' == $mode or empty($xsltName)) { return new Response('<?xml version="1.0"?>' . $xml, 'text/xml', $status); } if (!file_exists($xsltName)) { throw new RuntimeException(t('Шаблон %name не найден.', array('%name' => $xsltName))); } if ('client' == $mode) { $xml = str_replace('?>', '?><?xml-stylesheet type="text/xsl" href="' . $xsltName . '"?>', $xml); return new Response($xml, 'text/xml'); } $nocache = !empty($_GET['nocache']); $cache = cache::getInstance(); $ckey = 'xml:xsl:' . md5($xml) . ',' . filemtime($xsltName); if (false === ($output = $cache->{$ckey}) or $nocache) { set_error_handler(array(__CLASS__, 'eh')); $doc = new DOMDocument(); $doc->loadXML($xml); if (class_exists('xsltCache') and !$nocache) { $proc = new xsltCache(); $proc->importStyleSheet($xsltName); } else { $xsl = new DOMDocument(); @$xsl->load($xsltName); $proc = new XSLTProcessor(); $proc->importStyleSheet($xsl); } self::$lock = $xsltName; if ($output = str_replace(' xmlns=""', '', $proc->transformToXML($doc))) { $cache->{$ckey} = $output; } self::$lock = null; restore_error_handler(); } if (empty($output)) { throw new RuntimeException(t('Шаблон %xslt ничего не вернул.', array('%xslt' => $xsltName))); } if (null === $mimeType) { return trim(str_replace('<?xml version="1.0"?>', '', $output)); } return new Response($output, $mimeType, $status); }
/** * Execute xsl transformation process with xslcache (http://code.nytimes.com/projects/xslcache) library. * @param string $templatePath * @param \DOMDocument $source * @param array $parameters * @return string */ public function execute($templatePath, \DOMDocument $source, array $parameters = array()) { if (!extension_loaded('xslcache')) { throw new \RuntimeException('xslcache extension is not found, see http://code.nytimes.com/projects/xslcache'); } $xslt = new \xsltCache(); $xslt->importStyleSheet($templatePath); $outputDom = $xslt->transformToDoc($source); if ($parameters['output.type'] && $parameters['output.type'] == 'xml') { $result = $outputDom->saveXML(); } else { $result = $outputDom->saveHTML(); } return $result; }
public function getHTML($xml) { global $current_user; Log::timingplus('XSLTProcessor'); $xslTemplate = new DOMDocument(); $filename = Config::need('xslt_files_path') . DIRECTORY_SEPARATOR . $current_user->getTheme() . DIRECTORY_SEPARATOR . $this->xsltFileName; if (Config::need('xslcache')) { $xslProcessor = new xsltCache(); $xslProcessor->importStyleSheet($filename); } else { $xslProcessor = new XSLTProcessor(); $xslTemplate->load($filename, LIBXML_NOENT | LIBXML_DTDLOAD); $xslProcessor->importStyleSheet($xslTemplate); } $html = $xslProcessor->transformToXML($xml); Log::timingplus('XSLTProcessor'); return $html; }
/** * Error... * * @param object $e error object * * @return null */ function Nexista_builderError($e) { if ($e->getCode() == NX_ERROR_FATAL || $e->getCode() == NX_ERROR_WARNING) { $use_xslt_cache = 'yes'; if ($use_xslt_cache != 'yes' || !class_exists('xsltCache')) { $exceptionXsl = new XsltProcessor(); } else { $exceptionXsl = new xsltCache(); } $xsl = new DomDocument(); $my_xsl_file = NX_PATH_BASE . 'extensions/dev_buffer/s/xsl/exception.xsl'; if (file_exists($my_xsl_file)) { $xsl->load($my_xsl_file); $exceptionXsl->importStyleSheet($xsl); $xml = new DomDocument(); $xml->loadXML($e->outputXml()); $exceptionXsl->setParameter('', 'link_prefix', dirname($_SERVER['SCRIPT_NAME']) . '/index.php?nid='); $result = $exceptionXsl->transformToXML($xml); echo $result; } } }
/** * @copydoc ITransformer::transform * * @throws SystemException 'ERR_DEV_NOT_WELL_FORMED_XSLT' */ public function transform() { //При наличии модуля xslcache http://code.nytimes.com/projects/xslcache //используем его if (extension_loaded('xslcache') && $this->getConfigValue('document.xslcache') == 1) { $xsltProc = new xsltCache(); //есть одна проблема с ним //при неправильном xslt - сваливается в корку с 500 ошибкой $xsltProc->importStyleSheet($this->fileName); $result = $xsltProc->transformToXML($this->document); } else { $xsltProc = new \XSLTProcessor(); $xsltDoc = new \DOMDocument('1.0', 'UTF-8'); if (!@$xsltDoc->load($this->fileName)) { throw new SystemException('ERR_DEV_NOT_WELL_FORMED_XSLT', SystemException::ERR_DEVELOPER); } $xsltDoc->documentURI = $this->fileName; $xsltProc->importStylesheet($xsltDoc); $result = $xsltProc->transformToXml($this->document); } E()->getResponse()->setHeader('Content-Type', 'text/html; charset=UTF-8', false); return $result; }
/** * Builder::Transform() * * @param mixed $xslfile * @param bool $return * @return */ public function Transform($xslfile, $return = false) { if (Param('_Debug')) { header("Content-type: text/xml"); echo $this->asXML(); die; // exit(); } if (class_exists('xsltCache')) { $xslt = new xsltCache(); $xslt->importStyleSheet(TPL_DIR . $xslfile); } else { $xslt = new xsltProcessor(); $xsltDoc = new DomDocument(); $xsltDoc->load(TPL_DIR . $xslfile); $xslt->importStyleSheet($xsltDoc); } $result = $xslt->transformToXML($this->doc); if (!$return) { header("Content-type: text/html"); print $result; return null; } return $result; }
function Transform($xslfile, $xml, $return = false) { if (Param('_Debug')) { header("Content-type: text/xml"); print $xml; exit; } if (5 == 5) { if (class_exists('xsltCache')) { $xslt = new xsltCache(); $xslt->importStyleSheet(TPL_DIR . $xslfile); } else { $xslt = new xsltProcessor(); $xsltDoc = DomDocument::load(TPL_DIR . $xslfile); $xslt->importStyleSheet($xsltDoc); } // $result = $xslt->transformToXML(DomDocument::loadXML($xml)); $doc = new DOMDocument(); $load_succesfull = @$doc->loadXML($xml); if (!$load_succesfull) { $result = $xslt->transformToXML(iceERROR('XML', $xml)); } else { $result = $xslt->transformToXML($doc); } } else { // ob_end_clean(); $xslt = domxml_xslt_stylesheet_file(TPL_DIR . $xslfile); $dom = @domxml_open_mem($xml); if (!$dom) { $dom = domxml_open_mem(iceERROR('XML', $xml)); } $final = $xslt->process($dom); print serialize($xslt); exit; $load_end_time = microtime(true); $result = $xslt->result_dump_mem($final); unset($dom); unset($xslt); } if (!$return) { header("Content-type: text/html"); print $result; } else { //print $b; // ajax "<sajax>" string return $result; } }
function transform($xslfile, $xml, $no_dbg = false) { if (Param('_DBG') && !$no_dbg) { //print_r(debug_backtrace()); //setcookie('111', '222'); //die(); header("Content-type: text/xml"); print trim($xml); die; } //$phpver = explode('.', PHP_VERSION); //if ($phpver[0] == 5) { if (extension_loaded('xslcache')) { $xslt = new xsltCache(); $xslt->importStyleSheet($xslfile); } else { $xslt = new xsltProcessor(); $xsltDoc = DomDocument::load($xslfile); $xslt->importStyleSheet($xsltDoc); } $doc = new DOMDocument(); $load_succesfull = $doc->loadXML($xml); if ((!$load_succesfull || @$_GET['_BAD_XML_TEST']) && !Param('_DBG')) { //error //Redirect( '/error.php?error=xml&url='.$_SERVER['REQUEST_URI'] ); if (SITE == 'http://dev.starhit.ru') { die(file_get_contents('http://www.starhit.ru/error.php?error=xml')); } die(file_get_contents(SITE . '/error.php?error=xml')); } else { $galleries = $doc->getElementsByTagName('gallery'); foreach ($galleries as $gallery) { $images = $gallery->getElementsByTagName('image'); foreach ($images as $image) { foreach ($this->dsp->ae->size as $size => $params) { $img = $image->getElementsByTagName($size)->item(0); $width = $img->getAttribute('width'); if (!$width) { $img_sizes = $this->dsp->eis->GetSizeByURL($img->nodeValue); $img->setAttribute('width', $img_sizes[0]); $img->setAttribute('height', $img_sizes[1]); } } } } $result = $xslt->transformToXML($doc); } //} if (@$_GET['XSLT_INFO']) { echo '$xslt->hasExsltSupport(): ' . $xslt->hasExsltSupport() . ' $xslfile: ' . $xslfile . ' extension_loaded("xslcache"): ' . extension_loaded("xslcache") . ' XSL_CLONE_AUTO: ' . XSL_CLONE_AUTO . ' XSL_CLONE_NEVER: ' . XSL_CLONE_NEVER . ' XSL_CLONE_ALWAYS: ' . XSL_CLONE_ALWAYS . ' LIBXSLT_VERSION: ' . LIBXSLT_VERSION . ' LIBXSLT_DOTTED_VERSION: ' . LIBXSLT_DOTTED_VERSION . ' LIBEXSLT_VERSION: ' . LIBEXSLT_VERSION . ' LIBEXSLT_DOTTED_VERSION: ' . LIBEXSLT_DOTTED_VERSION . ' $xslt: ' . print_r($xslt, 1) . ' '; var_dump($doc->saveXML(), $xsltDoc->saveXML(), $xslt->transformToXML($doc)); die; } return $result; }