I have an XML file whose contents I need to import into a Microsoft Word 2007 document.
I have an XSL file that constructs a WordprocessingML table:
<?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.microsoft.com/office/word/2003/wordml">
<xsl:output method="xml" indent="yes"/>
<w:style w:type="table" w:styleId="TableStyle">
<w:name w:val="Table Style"/>
<w:tblPr>
<w:tblBorders>
<w:insideH w:val="single" w:sz="4" w:space="0" w:color="auto"/>
</w:tblBorders>
</w:tblPr>
</w:style>
<xsl:template match="list">
<xsl:processing-instruction name="mso-application">
<xsl:text>progid="Word.Document"</xsl:text>
</xsl:processing-instruction>
<w:tbl>
<w:tblPr>
<w:tblStyle w:val="TableStyle"/>
</w:tblPr>
<w:tblGrid>
<w:gridCol w:w="100" />
<w:gridCol w:w="200" />
<w:gridCol w:w="1024" />
</w:tblGrid>
<w:tr>
<w:tc><w:p><w:r><w:t>ID</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>Description</w:t></w:r></w:p></w:tc>
</w:tr>
<xsl:apply-templates select="items/item">
<xsl:sort select="id" order="ascending"/>
</xsl:apply-templates>
</w:tbl>
</xsl:template>
<xsl:template match="items/item">
<w:tr>
<w:tc><w:p><w:r><w:t>
<xsl:value-of select="id"/>
</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>
<xsl:value-of select="description"/>
</w:t></w:r></w:p></w:tc>
</w:tr>
</xsl:template>
</xsl:stylesheet>
I use the Word INCLUDETEXT field to import the XML file and apply the transformation:
{INCLUDETEXT "E:\\Spinner\\Documents\\data.xml" \t "E:\\Spinner\\Documents\\stylesheet.xsl"}
This works fine as far as actually importing the data is concerned – a basic table is displayed with the data I need.
The table, however, is devoid of any formatting – no borders, no gridlines, no shading, etc. Word ignores the column widths I specified in my XSL file (<w:gridCol w:w="100" />, etc.) and sets its own.
I need to format it using (preferably) a style already present in the document, such as “Table Grid”, or “Medium Shading 1 – Accent 3.” However, I can’t get Word to actually apply the style, either for an already-present-in-Word-document style (<w:tblStyle w:val="TableGrid"/>), or a newly-defined-in-XSL-file style (<w:tblStyle w:val="TableStyle"/>).
Has anyone any pointers?
You need something more like this, but I think you may need to adjust the column widths (possibly to take account of the cell borders)