Merge pull request #83 from RomanSyroeshko/PHPWord#46
Ability to apply XSL style sheet to Template
This commit is contained in:
commit
50ae8f34a8
|
|
@ -64,7 +64,7 @@ class PHPWord_Template
|
|||
if ($this->_tempFileName !== false) {
|
||||
// Copy the source File to the temp File
|
||||
if (!copy($strFilename, $this->_tempFileName)) {
|
||||
throw new PHPWord_Exception('Could not copy the template from ' . $strFilename . ' to ' . $this->_tempFileName . '.');
|
||||
throw new PHPWord_Exception("Could not copy the template from {$strFilename} to {$this->_tempFileName}.");
|
||||
}
|
||||
|
||||
$this->_objZip = new ZipArchive();
|
||||
|
|
@ -76,6 +76,36 @@ class PHPWord_Template
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies XSL style sheet to template's parts
|
||||
*
|
||||
* @param DOMDocument &$xslDOMDocument
|
||||
* @param array $xslOptions = array()
|
||||
* @param string $xslOptionsURI = ''
|
||||
*/
|
||||
public function applyXslStyleSheet(&$xslDOMDocument, $xslOptions = array(), $xslOptionsURI = '')
|
||||
{
|
||||
$processor = new \XSLTProcessor();
|
||||
|
||||
$processor->importStylesheet($xslDOMDocument);
|
||||
|
||||
if ($processor->setParameter($xslOptionsURI, $xslOptions) === false) {
|
||||
throw new \Exception('Could not set values for the given XSL style sheet parameters.');
|
||||
}
|
||||
|
||||
$xmlDOMDocument = new \DOMDocument();
|
||||
if ($xmlDOMDocument->loadXML($this->_documentXML) === false) {
|
||||
throw new \Exception('Could not load XML from the given template.');
|
||||
}
|
||||
|
||||
$xmlTransformed = $processor->transformToXml($xmlDOMDocument);
|
||||
if ($xmlTransformed === false) {
|
||||
throw new \Exception('Could not transform the given XML document.');
|
||||
}
|
||||
|
||||
$this->_documentXML = $xmlTransformed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a Template value
|
||||
*
|
||||
|
|
|
|||
|
|
@ -0,0 +1,118 @@
|
|||
<?php
|
||||
namespace PHPWord\Tests;
|
||||
|
||||
use PHPWord_Template;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass PHPWord_Template
|
||||
*/
|
||||
class PHPWord_TemplateTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @covers ::applyXslStyleSheet
|
||||
* @test
|
||||
*/
|
||||
final public function testXslStyleSheetCanBeApplied()
|
||||
{
|
||||
$template = new PHPWord_Template(
|
||||
\join(
|
||||
\DIRECTORY_SEPARATOR,
|
||||
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'with_table_macros.docx')
|
||||
)
|
||||
);
|
||||
|
||||
$xslDOMDocument = new \DOMDocument();
|
||||
$xslDOMDocument->load(
|
||||
\join(
|
||||
\DIRECTORY_SEPARATOR,
|
||||
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'remove_tables_by_needle.xsl')
|
||||
)
|
||||
);
|
||||
|
||||
foreach (array('${employee.', '${scoreboard.') as $needle) {
|
||||
$template->applyXslStyleSheet($xslDOMDocument, array('needle' => $needle));
|
||||
}
|
||||
|
||||
$actualDocument = $template->save();
|
||||
$expectedDocument = \join(
|
||||
\DIRECTORY_SEPARATOR,
|
||||
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'documents', 'without_table_macros.docx')
|
||||
);
|
||||
|
||||
$actualZip = new \ZipArchive();
|
||||
$actualZip->open($actualDocument);
|
||||
$actualXml = $actualZip->getFromName('word/document.xml');
|
||||
if ($actualZip->close() === false) {
|
||||
throw new \Exception('Could not close zip file "' . $actualDocument . '".');
|
||||
}
|
||||
|
||||
$expectedZip = new \ZipArchive();
|
||||
$expectedZip->open($expectedDocument);
|
||||
$expectedXml = $expectedZip->getFromName('word/document.xml');
|
||||
if ($expectedZip->close() === false) {
|
||||
throw new \Exception('Could not close zip file "' . $expectedDocument . '".');
|
||||
}
|
||||
|
||||
$this->assertXmlStringEqualsXmlString($expectedXml, $actualXml);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::applyXslStyleSheet
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage Could not set values for the given XSL style sheet parameters.
|
||||
* @test
|
||||
*/
|
||||
final public function testXslStyleSheetCanNotBeAppliedOnFailureOfSettingParameterValue()
|
||||
{
|
||||
$template = new PHPWord_Template(
|
||||
\join(
|
||||
\DIRECTORY_SEPARATOR,
|
||||
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'blank.docx')
|
||||
)
|
||||
);
|
||||
|
||||
$xslDOMDocument = new \DOMDocument();
|
||||
$xslDOMDocument->load(
|
||||
\join(
|
||||
\DIRECTORY_SEPARATOR,
|
||||
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'passthrough.xsl')
|
||||
)
|
||||
);
|
||||
|
||||
/*
|
||||
* We have to use error control below, because XSLTProcessor::setParameter omits warning on failure.
|
||||
* This warning fails the test.
|
||||
*/
|
||||
@$template->applyXslStyleSheet($xslDOMDocument, array(1 => 'somevalue'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::applyXslStyleSheet
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage Could not load XML from the given template.
|
||||
* @test
|
||||
*/
|
||||
final public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromTemplate()
|
||||
{
|
||||
$template = new PHPWord_Template(
|
||||
\join(
|
||||
\DIRECTORY_SEPARATOR,
|
||||
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'corrupted_main_document_part.docx')
|
||||
)
|
||||
);
|
||||
|
||||
$xslDOMDocument = new \DOMDocument();
|
||||
$xslDOMDocument->load(
|
||||
\join(
|
||||
\DIRECTORY_SEPARATOR,
|
||||
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'passthrough.xsl')
|
||||
)
|
||||
);
|
||||
|
||||
/*
|
||||
* We have to use error control below, because DOMDocument::loadXML omits warning on failure.
|
||||
* This warning fails the test.
|
||||
*/
|
||||
@$template->applyXslStyleSheet($xslDOMDocument);
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
|
||||
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
|
||||
|
||||
<xsl:template match='@*|node()'>
|
||||
<xsl:copy>
|
||||
<xsl:apply-templates select='@*|node()'/>
|
||||
</xsl:copy>
|
||||
</xsl:template>
|
||||
|
||||
</xsl:stylesheet>
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsl:stylesheet
|
||||
version="1.0"
|
||||
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
|
||||
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
|
||||
|
||||
<xsl:template match='@*|node()'>
|
||||
<xsl:copy>
|
||||
<xsl:apply-templates select='@*|node()'/>
|
||||
</xsl:copy>
|
||||
</xsl:template>
|
||||
|
||||
<xsl:template match="//w:tbl">
|
||||
<xsl:choose>
|
||||
<xsl:when test="w:tr/w:tc/w:p/w:r/w:t[contains(., $needle)]"/>
|
||||
<xsl:otherwise>
|
||||
<xsl:copy>
|
||||
<xsl:apply-templates select='@*|node()'/>
|
||||
</xsl:copy>
|
||||
</xsl:otherwise>
|
||||
</xsl:choose>
|
||||
</xsl:template>
|
||||
|
||||
</xsl:stylesheet>
|
||||
Loading…
Reference in New Issue