From abbd2885092d0a96df7b28eda57f183bf16cd3b4 Mon Sep 17 00:00:00 2001 From: Roman Syroeshko Date: Wed, 1 Jan 2014 06:28:10 -0800 Subject: [PATCH] https://github.com/PHPOffice/PHPWord/issues/46 Ability to apply XSLT to Template --- Classes/PHPWord/Template.php | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) 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 +}