diff --git a/Classes/PHPWord/Template.php b/Classes/PHPWord/Template.php index a01a8541..d474195e 100755 --- a/Classes/PHPWord/Template.php +++ b/Classes/PHPWord/Template.php @@ -75,6 +75,36 @@ class PHPWord_Template throw new PHPWord_Exception('Could not create temporary file with unique name in the default temporary directory.'); } } + + /** + * Applies XSL transformation to XML template. + * + * @param DOMDocument &$xslDOMDocument + * @param string $xslOptionsURI + * @param array $xslOptions + */ + public function applyXSLT(&$xslDOMDocument, $xslOptionsURI = '', $xslOptions = []) + { + $processor = new \XSLTProcessor(); + + $processor->importStylesheet($xslDOMDocument); + + if ($processor->setParameter($xslOptionsURI, $xslOptions) === \FALSE) { + throw new \Exception('Could not set values for the given XSL stylesheet parameters.'); + } + + $xmlDOMDocument = new \DOMDocument(); + if ($xmlDOMDocument->loadXML($this->_documentXML) === \FALSE) { + throw new \Exception('Could not create DOM document for the given XML template.'); + } + + $xmlTransformed = $processor->transformToXml($xmlDOMDocument); + if ($xmlTransformed === \FALSE) { + throw new \Exception('Could not apply XSLT to the given DOM document.'); + } + + $this->_documentXML = $xmlTransformed; + } /** * Set a Template value @@ -134,4 +164,4 @@ class PHPWord_Template rename($this->_tempFileName, $strFilename); } -} \ No newline at end of file +}