Christian Nagel's OneNotes

.NET Training, Consulting, Coaching - C#, Web Services, Enterprise Services, ASP.NET, Whidbey, Longhorn and More!

Affiliations

Books I've written

INETA UG Leaders

My Blogroll

Creating Word Documents with XSLT

Using the Office Schemas it is easy to create Microsoft Word 2003 documents.

Let's start with this XML document:

<?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 result should be a Word document.

A simple Word document containing the above data can be as simple as this:

<?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument xmlns:w="
http://schemas.microsoft.com/office/word/2003/wordml">
  <w:body>
    <w:p>
      <w:r>
        <w:t>MS-2524, XML Web Services Programming</w:t>
      </w:r>
    </w:p>
    <w:p>
      <w:r>
        <w:t>MS-2124, C# Programming</w:t>
      </w:r>
    </w:p>
    <w:p>
      <w:r>
        <w:t>NET2, .NET 2.0 Early Adapter</w:t>
      </w:r>
    </w:p>
  </w:body>
</w:wordDocument>

With XSLT the document can be created using this style sheet:

<?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>
     <xsl:apply-templates select="Courses/Course" />
   </w:body>
  </w:wordDocument>
 </xsl:template>
 <xsl:template match="Course">
    <w:p>
     <w:r>
      <w:t>
       <xsl:value-of select="@Number" />, <xsl:value-of select="Title"/>
      </w:t>
     </w:r>
    </w:p>
 </xsl:template>
</xsl:stylesheet>

Adding the processing instruction mso-application to the document allows the system to deal with the XML file as a Word document. The parser reads the progid for Word to display the Word icon, and to start Word when opening the file.

  <xsl:processing-instruction name="mso-application">
   <xsl:text>progid="Word.Document"</xsl:text>
  </xsl:processing-instruction>

The elements <w:wordDocument> and <w:body> can be compared to the HTML tags <HTML> and <BODY>. <w:t> is the tag for a text output.

The schemas for Microsoft Office 2003 can be downloaded from the MSDN Website. The download is an installation package that includes schemas for Office 2003 as well as documentation. Referencing the schemas with the XML and XSLT editors of Visual Studio 2005 gives intellisense :-)

Christian

 

Comments

Ross said:

Without wanting to obviously plug a company here I thought I would mentioned Aspose who provide a solution to those who want to generate word documents on the server and have been stuck using automation for so long :(

You can get a trial of their software for generating Word 97 format files from http://www.aspose.com

Sorry about the plug, I don't have anything to do with them apart from being a customer.
# September 25, 2004 10:46 AM

TrackBack said:

Random Notes & Bookmarks
# September 28, 2004 5:36 PM

TrackBack said:

# September 30, 2004 8:17 AM

TrackBack said:

在asp.net中无需服务器安装word,利用xml生成word文档
# October 23, 2004 7:15 AM

XML zu Word - XML @ tutorials.de: Forum, Tutorial, Anleitung, Schulung & Hilfe said:

Pingback from  XML zu Word - XML @ tutorials.de: Forum, Tutorial, Anleitung, Schulung &amp; Hilfe

# August 21, 2007 4:05 AM

Alex said:

Please make up the complete code for generate a file word from an xml file and the actions that I must do to generate it.

thank's

# August 31, 2007 11:34 AM

Bernard Murphy said:

I'm having a hard time figuring out what plays role of transformation engine in conversion. Suppose I have an XML and an XSLT. Does Word itself act as the tranformation engine? If so, what version/level of Word do I need? (I'm guessing more that Word small business edition, 2003)

# October 28, 2007 11:07 AM

keshav said:

can we create XML like

<Title><i>XML Web Services Programming</i></Title>

so that "XML Web Services Programming" look like as

itellic.

# September 3, 2009 4:45 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)