I'm new to XML/XSLT and am having an issue trying to change an image out when modifying a Word XSLT Template.
I have 4 parts to this...
- A Word Template Document
- XML file
- XSLT stylesheet (taken from the Word Template and added XML into placeholders)
- PHP code that takes the XML and applies it to the XSLT template to generate the Word Document.
This all seems to be working great except when it comes to images. Basically, I can't seem to get the images to load using the XML values.
Word Document Just a Word 2007 document that mimics what the final output should be. Contains placeholder graphs which I'm trying to replace dynamically in the XSLT but the images within the word document template always seem to be displayed no matter what.
XML:
<Assessment>
<Title>
...
</Title>
<Scorecard>
...
<Graph0>http://pathtoimage.jpg</Graph0>
...
</Scorecard>
</Assessment>
Where Assessment/Scorecard/Graph0 is the path to the image i want to insert.
XSLT:
...
<w:drawing>
<wp:inline distT="0" distB="0" distL="0" distR="0">
<wp:extent cx="5709285" cy="1524000"/>
<wp:effectExtent l="19050" t="0" r="5715" b="0"/>
<wp:docPr id="3" name="Picture 3" descr="{Assessment/Scorecard/Graph0}"/>
<wp:cNvGraphicFramePr>
<a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/>
</wp:cNvGraphicFramePr>
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:nvPicPr>
<pic:cNvPr id="0" name="Picture 3" descr="{Assessment/Scorecard/Graph0}"/>
<pic:cNvPicPr>
<a:picLocks noChangeAspect="1" noChangeArrowheads="1"/>
</pic:cNvPicPr>
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="rId9" r:link="rId10" cstate="print"/>
<a:srcRect/>
<a:stretch>
<a:fillRect/>
</a:stretch>
</pic:blipFill>
<pic:spPr bwMode="auto">
<a:xfrm>
<a:off x="0" y="0"/>
<a:ext cx="5709285" cy="1524000"/>
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst/>
</a:prstGeom>
<a:noFill/>
<a:ln w="9525">
<a:noFill/>
<a:miter lim="800000"/>
<a:headEnd/>
<a:tailEnd/>
</a:ln>
</pic:spPr>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>
...
<w:tc>
<w:tcPr>
<w:tcW w:w="0" w:type="auto"/>
<w:tcMar>
<w:top w:w="75" w:type="dxa"/>
<w:left w:w="300" w:type="dxa"/>
<w:bottom w:w="75" w:type="dxa"/>
<w:right w:w="75" w:type="dxa"/>
</w:tcMar>
<w:vAlign w:val="center"/>
<w:hideMark/>
</w:tcPr>
<w:p w:rsidR="0057097D" w:rsidRDefault="0057097D">
<w:pPr>
<w:pStyle w:val="Heading3"/>
<w:rPr>
<w:rFonts w:ascii="Arial" w:eastAsia="Times New Roman" w:hAnsi="Arial" w:cs="Arial"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:ascii="Arial" w:eastAsia="Times New Roman" w:hAnsi="Arial" w:cs="Arial"/>
</w:rPr>
<w:t xml:space="preserve">Prepared for: <xsl:value-of select="Assessment/Title/CompanyName" /></w:t>
</w:r>
</w:p>
<w:p w:rsidR="0057097D" w:rsidRDefault="0057097D">
<w:pPr>
<w:pStyle w:val="Heading3"/>
<w:rPr>
<w:rFonts w:ascii="Arial" w:eastAsia="Times New Roman" w:hAnsi="Arial" w:cs="Arial"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:ascii="Arial" w:eastAsia="Times New Roman" w:hAnsi="Arial" w:cs="Arial"/>
</w:rPr>
<w:t><xsl:value-of select="Assessment/Title/ReportDate" /></w:t>
</w:r>
</w:p>
<xsl:for-each select="Assessment/Title/Address">
<w:p w:rsidR="0057097D" w:rsidRDefault="0057097D">
<w:pPr>
<w:pStyle w:val="NormalWeb"/>
<w:rPr>
<w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/>
</w:rPr>
<w:t><xsl:value-of select="Line" /></w:t>
</w:r>
</w:p>
</xsl:for-each>
</w:tc>
Where {Assessment/Scorecard/Graph0} is the placeholder for the new image.
PHP
$xmlDataFile = ("Assessment.xml");
$xsltFile = ("Assessment.xslt");
$sourceTemplate = ("AssessmentTemplate.docx");
$outputDocument = ("Assessment.docx");
$xmlDocument = new DOMDocument();
$xmlDocument->load($xmlDataFile);
$xsltDocument = new DOMDocument();
$xsltDocument->load($xsltFile);
$xsltProcessor = new XSLTProcessor();
$xsltProcessor->importStylesheet($xsltDocument);
$newContent = $xsltProcessor->transformToXML($xmlDocument);
if (copy($sourceTemplate, $outputDocument)) {
$zipArchive = new ZipArchive();
$zipArchive->open($outputDocument);
$zipArchive->addFromString("word/document.xml", $newContent);
$zipArchive->close();
}
This puts it all together and generates my final Word Document. As I said, it all works great except images. All the text values are updated to the values from the XML file, but I can't seem to get the images to update.
For the values that work I'm using:
<xsl:value-of select="..." />
And for the values that don't work (images) I'm using:
{Assessment/Scorecard/Graph0}
I'm assuming it's something to do with this method of inserting XML content, but it also won't accept the value-of method in the place of the curly bracket method and actually breaks the document.
Any suggestions? Is there a better way to do this?
Thanks in advance!