您好我正在尝试从PHP应用程序生成MS-Word报告。为了做到这一点,我converting XML data into Microsoft Office Open XML by using XSLT Transformations。
我有一个简单的XML文件来提取数据:
<?xml version="1.0" encoding="UTF-8"?>
<Movies>
<Genre name="Action">
<Movie>
<Name>Crash</Name>
<Released>2005</Released>
</Movie>
</Genre>
<Genre name="Drama">
<Movie>
<Name>The Departed</Name>
<Released>2006</Released>
</Movie>
<Movie>
<Name>The Pursuit of Happyness</Name>
<Released>2006</Released>
</Movie>
</Genre>
<Genre name="Comedy">
<Movie>
<Name>The Bucket List</Name>
<Released>2007</Released>
</Movie>
</Genre>
</Movies>
我还有一个XSLT文件,可以将XML转换为MS Office XML:
<xsl:for-each select="Movies/Genre">
<w:p w:rsidR="00EC137C" w:rsidRPr="00BF ...
<w:pPr>
<w:pStyle w:val="Heading2"/>
</w:pPr>
<w:r w:rsidRPr="00BF350E">
<w:t>
<xsl:value-of select="@name"/>
</w:t>
</<xsl:value-of select w:r>
</w:p>
<xsl:for-each select="Movie">
<w:p w:rsidR="00EC137C" w:rsidRPr="00EC1 ...
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="0"/>
<w:numId w:val="1"/>
</w:numPr>
</w:pPr>
<w:r w:rsidRPr="00BF350E">
<w:rPr>
<w:b/>
</w:rPr>
<w:t>
<xsl:value-of select="Name"/>
</w:t>
</w:r>
<w:r w:rsidR="00C46B60">
<w:t xml:space="preserve"> (<xsl:value-of select="Released"/>)
</w:t>
</w:r>
</w:p>
</xsl:for-each>
</xsl:for-each>
...
还有PHP脚本:
<?php
//Declare variables for file names.
$xmlDataFile = "MyMovies.xml";
$xsltFile = "MyMovies.xslt";
$sourceTemplate = "MyMoviesTemplate.docx";
$outputDocument = "MyMovies.docx";
//Load the xml data and xslt and perform the transformation.
$xmlDocument = new DOMDocument();
$xmlDocument->load($xmlDataFile);
$xsltDocument = new DOMDocument();
$xsltDocument->load($xsltFile);
$xsltProcessor = new XSLTProcessor();
$xsltProcessor->importStylesheet($xsltDocument);
//After the transformation, $newContentNew contains
//the XML data in the Open XML Wordprocessing format.
$newContent = $xsltProcessor->transformToXML($xmlDocument);
//Copy the Word 2007 template document to the output file.
if (copy($sourceTemplate, $outputDocument)) {
//Open XML files are packaged following the Open Packaging
//Conventions and can be treated as zip files when
//accessing their content.
$zipArchive = new ZipArchive();
$zipArchive->open($outputDocument);
//Replace the content with the new content created above.
//In the Open XML Wordprocessing format content is stored
//in the document.xml file located in the word directory.
$zipArchive->addFromString("word/document.xml", $newContent);
$zipArchive->close();
echo "Processing Complete";
}
?>
现在我需要在报告中插入图像。我应该在XML文件中拥有什么以及我应该在XSLT文件中拥有什么?我是XML和XSLT的初学者我刚刚从msdn中取样,但现在我需要在报告中插入图像我不知道该怎么做?
此致