Get basic HTML formattings working in word 2003, formattings inside table working in word documents

This commit is contained in:
Nathan Gray 2011-08-25 20:28:24 +00:00
parent fb62831e86
commit 0571ac47e2
3 changed files with 34 additions and 17 deletions

View File

@ -377,13 +377,14 @@ abstract class bo_merge
);
// It seems easier to split the parent tags here
$replace_tags = array(
// Tables, lists don't go inside <w:p>
'/<(ol|ul|table)( [^>]*)?>/' => '</w:t></w:r></w:p><$1$2>',
'/<\/(ol|ul|table)>/' => '</$1><w:p><w:r><w:t>',
// Fix for things other than text (newlines) inside table row
'/<(td)( [^>]*)?>((?!<w:t>))(.*?)<\/td>[\s]*?/' => '<$1$2><w:t>$4</w:t></td>',
'/<(li)(.*?)>(.*?)<\/\1>/' => '<$1 $2>$3</$1>',
/*
'/<(span)(.*?)>/' => "\n".'</w:t></w:r><$1$2>',
'/<\/(span)>/' => '</$1><w:r><w:t>'."\n",
*/
// Remove extra whitespace
'/<w:t>[\s]+(.*?)<\/w:t>/' => '<w:t>$1</w:t>'
);
$content = preg_replace(array_keys($replace_tags),array_values($replace_tags),$content);
//echo $content;die();
@ -400,17 +401,18 @@ abstract class bo_merge
try
{
$element = new SimpleXMLelement($content);
$content = $xslt->transformToXml($element);
$content = @$xslt->transformToXml($element);
// Word 2003 needs invalid XML, add back in
if($mimetype == 'application/xml' && $mso_application_progid == 'Word.Document') {
// Word 2003 needs two declarations, add extra declaration back in
if($mimetype == 'application/xml' && $mso_application_progid == 'Word.Document' && strpos($content, '<?xml') !== 0) {
$content = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'.$content;
}
// Validate
/*
$doc = new DOMDocument();
// $doc->loadXML($content);
// $doc->schemaValidate('/home/nathan/Downloads/WordprocessingML Schemas/wordnet.xsd');
//echo $content;die();
$doc->loadXML($content);
$doc->schemaValidate(*Schema (xsd) file*);
*/
}
catch (Exception $e)
{

View File

@ -86,7 +86,7 @@ Breakers
</xsl:template>
-->
<xsl:template match="w:r[descendant::strong|descendant::em|descendant::u|descendant::span]">
<xsl:template name="apply-styles" match="w:r[descendant::strong|descendant::em|descendant::u|descendant::span]">
<xsl:for-each select="node()|@*[not(w:rPr)]">
<xsl:choose>
<xsl:when test="descendant::strong|descendant::em|descendant::u|descendant::span" >
@ -221,7 +221,7 @@ Breakers
<xsl:template match="table">
<w:tbl>
<w:tblPr>
<w:tblW w:type="dxa" w:w="9972"/>
<w:tblW w:type="pct" w:w="4500"/>
<w:jc w:value="left"/>
<xsl:if test="@border &gt; 0">
<w:tblBorders>
@ -251,7 +251,13 @@ Breakers
<w:right w:color="000000" w:space="0" w:sz="{$width}" w:val="single"/>
</w:tcBorders></w:tcPr>
</xsl:if>
<w:p><w:r><w:t><xsl:apply-templates select="child::node()" /></w:t></w:r></w:p>
<w:p>
<xsl:call-template name="apply-styles">
<w:r>
<xsl:copy-of select="node()|@*"/>
</w:r>
</xsl:call-template>
</w:p>
</w:tc>
</xsl:for-each>
</w:tr>

View File

@ -87,7 +87,7 @@ Breakers
</xsl:template>
-->
<xsl:template match="w:r[descendant::strong|descendant::em|descendant::u|descendant::span]">
<xsl:template match="w:r[descendant::strong|descendant::em|descendant::u|descendant::span]" name="apply-styles">
<xsl:for-each select="node()|@*[not(w:rPr)]">
<xsl:choose>
<xsl:when test="descendant::strong|descendant::em|descendant::u|descendant::span" >
@ -123,6 +123,7 @@ Breakers
</w:t>
<w:br/>
</xsl:template>
<xsl:template match="i|em">
<w:i />
</xsl:template>
@ -165,7 +166,7 @@ Breakers
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<w:shd w:fill="{$hex}" w:val="solid"/>
<w:shd w:fill="{$hex}" w:val="clear"/>
</xsl:if>
<xsl:if test="starts-with(.,'font-size')">
<xsl:variable name="font-size" select="substring-after(text(),'font-size:')" />
@ -218,8 +219,8 @@ Breakers
<xsl:template match="table">
<w:tbl>
<w:tblPr>
<w:tblW w:type="dxa" w:w="9972"/>
<w:jc w:val="left"/>
<w:tblW w:w="5000" w:type="pct"/>
</w:tblPr>
<w:tblGrid>
<xsl:for-each select="./tr[1]/td">
@ -229,7 +230,15 @@ Breakers
<xsl:for-each select="./tr">
<w:tr>
<xsl:for-each select="./td">
<w:tc><w:p><w:r><w:t><xsl:apply-templates select="child::node()" /></w:t></w:r></w:p></w:tc>
<w:tc>
<w:p>
<xsl:call-template name="apply-styles">
<w:r>
<xsl:copy-of select="node()|@*"/>
</w:r>
</xsl:call-template>
</w:p>
</w:tc>
</xsl:for-each>
</w:tr>
</xsl:for-each>