New Microsoft Regional Directors
Keith Brown's Security Book

Creating Word Documents with XSLT (Part 2 - Creating Tables)

With my last blog about creating Word documents with XSLT I've shown an example to do a very simple document.

Here I'm showing how to create a table using Word. The same XML file as before is used:

<?xml version="1.0" encoding="utf-8" ?>
<Courses>
 <Course Number="MS-2524">
  <Title>XML Web Services Programming</Title>
 </Course>
 <Course Number="MS-2124">
  <Title>C# Programming</Title>
 </Course>
 <Course Number="NET2">
  <Title>.NET 2.0 Early Adapter</Title>
 </Course>
</Courses>

The XSLT file to create a Word document with a 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" />
 <xsl:template match="/">
  <xsl:processing-instruction name="mso-application">
   <xsl:text>progid="Word.Document"</xsl:text>
  </xsl:processing-instruction>
  <w:wordDocument>
   <w:body>
    <w:tbl>
     <w:tblPr>
      <w:tblStyle w:val="TableGrid"/>
      <w:tblW w:w="0" w:type="auto"/>
      <w:tblLook w:val="01E0"/>
     </w:tblPr>
     <w:tblGrid>
      <w:gridCol w:w="4428"/>
      <w:gridCol w:w="4428"/>
     </w:tblGrid>

     <w:tr>
      <w:tc>

       <w:p>
        <w:r>
         <w:t>Number</w:t>
        </w:r>
       </w:p>
      </w:tc>
      <w:tc>

       <w:p>
        <w:r>
         <w:t>Course Title</w:t>
        </w:r>
       </w:p>
      </w:tc>
     </w:tr>

     <xsl:apply-templates select="Courses/Course" />
    </w:tbl>
   </w:body>
  </w:wordDocument>
 </xsl:template>
 <xsl:template match="Course">
  <w:tr>
   <w:tc>
    <w:p>
     <w:r>
      <w:t>
       <xsl:value-of select="@Number" />
      </w:t>
     </w:r>
    </w:p>
   </w:tc>
   <w:tc>
    <w:p>
     <w:r>
      <w:t>
       <xsl:value-of select="Title"/>
      </w:t>
     </w:r>
    </w:p>
   </w:tc>
  </w:tr>
 </xsl:template>
</xsl:stylesheet>

With WordML these are some elements that make up a table:

<tbl> <tbl> represents a table. This is similar to HTML <TABLE>. A WordML <table> element exists, too. However, the <table> element is used for a Office Data Source Object. 
<tblPr> With the element <tblPr> table-wide properties such as the style and the width are defined.
<tblGrid> The element <tblGrid> defines the grid layout of the table. In the example two columns with similar width are defined.
<tr> <tr> is the row of a table
<tc> <tc> is a column inside the row

This helps creating Word documents with WordML:

  • Create a document with Microsoft Word and save it as XML file. Check the XML source.
  • Using Visual Studio 2005, add the Word schemas to the XML file for Intellisense-support.
  • Check the documentation of the Office schemas.

The Office schemas (including documentation) can be found on the MSDN Website.

Christian

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Christian Nagel's OneNotes

AUTHOR: Christian Nagel's OneNotes
IP: 000.000.000.000
DATE: 10/17/2004 05:52:00 PM

Christian Nagel's OneNotes

AUTHOR: Christian Nagel's OneNotes
IP: 000.000.000.000
DATE: 10/18/2004 06:08:00 PM

buy kamagra

had the unbelievable good fortune to attend one day of summer camp last week, and let me tell you--it left me hungry for more!

christian louboutin

*I do wish you true happiness every day!

UGG Shoes

oh so funny!

Avs Video Converter Cracked full download

Awesome read, i was chatting to a friend the other week about the same topic and gave her this article to visit.

air jordans

*Cool! Very interesting post. I like Momo (also because it reminds me of Momotarou, the peach boy) and Pochi and Tama. What a

nice dog in the pic!

cherif

how to make this example
i create 2 file
1: example.xml
2: example.xsl

an i put
in the xml file but i have nothing
thanks for your help

coach outlet online

I agree you the up.As it give me the same some feeling!

Kids Moncler Outlet

Very, very nicely done!

www.salescheapuggboots.com

ugg outlet store salescheapuggboots.com cheap uggs for women

The comments to this entry are closed.