importStylesheet($xsl_dom); return $xsl_processor; } /** * prepare the xsl stylesheet and add params dynamically * @param string $xsl * @param array $params * @return DOMDocument */ public static function prepareXsl($xsl, $params = []) { // load xsl $xsl_dom = self::getDOM($xsl); // a way to use param xsl dynamically return self::addParams($xsl_dom, $params); // voir avec lionel si peut rester la } /** * * @param mixed $xml (string or DOMDocument * @param XSLTProcessor $xsl_proc * @param array $params * @return string the result of transformation */ public static function transform($xml, $xsl, $params = []) { // if xml is string tranform to DOM if (is_string($xml)) { $xml = self::getDOM($xml); } // if xsl is string tranform to DOM if (is_string($xsl)) { $xsl = self::getDOM($xsl); } //get the processor with the stylesheet $xsl_proc = self::getProcessor($xsl); // set values for params if (sizeof($params)) { $xsl_proc->setParameter(self::PARAMS_NS, $params); } // transformation return $xsl_proc->transformToXML($xml); } /** * return the DOM corresponding to the parameter * @param string $str * @return DOMDocument */ public static function getDOM($str) { //create domdocument $dom = new DOMDocument(); //load by file or string is_file($str) ? $dom->load($str) : $dom->loadXML($str); // return the DOMDoc return $dom; } /** * insert the tag param for each params noexistent in xsl stylesheet * @param DOMPDocument $dom * @return DOMDocument */ private static function addParams($dom, $params) { if (!sizeof($params)) { return $dom; } // get xpath $xpath = new DOMXPath($dom); //registering namespace xsl $xpath->registerNamespace(self::XSLT_SHORT_NS, self::XSLT_NS); // get the params tag direct child of the stylesheet (don't touch other params as template for example) $items = $xpath->query('/' . self::XSLT_SHORT_NS . ':stylesheet|' . self::XSLT_SHORT_NS . ':transform/' . self::XSLT_SHORT_NS . ':param'); // list existing to don't duplicate it $existing = []; foreach ($items as $param) { $existing[] = $param->getAttribute('name'); } // for each item in param foreach ($params as $name => $val) { // if not exist in dom if (!in_array($name, $existing)) { // we add it to the dom $node = $dom->createElementNS(self::XSLT_NS, self::XSLT_SHORT_NS . ":param"); $node->setAttribute('name', $name); // log to say param added dynamically common_Logger::w('Parameters "' . $name . '"added automatically by the ' . get_class() . ':addParams \'s function because it was missing in the xsl stylesheet'); } } return $dom; } }