如何在Word中使用XSL设置导入的XML样式?

时间:2012-08-10 13:25:54

标签: xml xslt ms-word

我有一个XML文件,其中包含我需要导入Microsoft Word 2007文档的内容。

我有一个构造WordprocessingML表的XSL文件:

<?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>

我使用Word INCLUDETEXT字段导入XML文件并应用转换:

{INCLUDETEXT  "E:\\Spinner\\Documents\\data.xml" \t "E:\\Spinner\\Documents\\stylesheet.xsl"}

就实际导入数据而言,这样可以正常工作 - 显示一个基本表格,其中包含我需要的数据。

然而,该表没有任何格式 - 没有边框,没有网格线,没有阴影等.Word忽略我在我的XSL文件(<w:gridCol w:w="100" />等)中指定的列宽并设置了自己的格式

我需要使用(最好)文档中已存在的样式来格式化它,例如“Table Grid”或“Medium Shading 1 - Accent 3”。但是,我无法让Word实际应用该样式,无论是对于已存在的Word文档样式(<w:tblStyle w:val="TableGrid"/>)还是新定义的XSL文件样式({{ 1}})。

有没有人指点?

1 个答案:

答案 0 :(得分:3)

您需要更像这样的东西,但我认为您可能需要调整列宽(可能需要考虑单元格边框)

<?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"/> 

<xsl:template match="list">
  <xsl:processing-instruction name="mso-application">
  <xsl:text>progid="Word.Document"</xsl:text>
  </xsl:processing-instruction>
  <w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"  w:macrosPresent="no" w:embeddedObjPresent="no" 
w:ocxPresent="no" xml:space="preserve">
    <w:styles>
      <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>
    </w:styles>
    <w:body>
      <w:tbl>
        <w:tblPr>
            <w:tblStyle w:val="TableStyle"/>
        </w:tblPr>
        <w:tblGrid>
            <w:gridCol w:w="100" />
            <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>
    </w:body>
  </w:wordDocument>
</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>