Sunday, November 30, 2003 4:15 PM bart

Generating Word documents with XML and XSLT

For the SchoolServer project I needed to create Word documents which contain the login, initial password and root folder for each student, teacher and staff member (mail merge). Thanks to Office 2003 and the support for XML in Word it's now possible to create Word 2003 documents by transforming and XML data source (i.e. a DataSet object) using and XSLT stylesheet to a Word document. Really cool! Let's post some code:

XmlDocument doc=

new XmlDataDocument(persons);
XslTransform students =
students.Load(dir + "students.xsl");
XmlWriter oStudents =
XmlTextWriter(dir + "\\students.xml",Encoding.Default);

In here, the persons object is a dataset which contains all the data of the students. The output document is students.xml (dir is just the output directory of the mail merge), so it has the extension .xml instead of .doc. Now, how does the system know it's a word document? This is specified in the first part of the XSLT file:

<xsl:stylesheet version="1.0"
xmlns:w="" >
   < xsl:template match="/" >
      < xsl:processing-instruction name="mso-application">progid="Word.Document"< / xsl:processing-instruction >
      < w:wordDocument xmlns:w="" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:sl="" xmlns:aml="" xmlns:wx="" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no" xml:space="preserve" >
         < w:docPr >
            < w:view w:val="print" / >
         < / w:docPr >
         < w:body >
            < wx:sect >
               < xsl:apply-templates / >
            < / wx:sect >
         < / w:body >
      < / w:wordDocument >
   < / xsl:template >

The xsl:processing-instruction part does the trick. This value is read by the Windows shell to determine the associated program for the xml file type. Of course your primary question is how to find this XML code with the , , magic tags? Well, that's very easy: open Word 2003 and save the document as an XML document. The only thing left you need to do is to create an XSLT file based on the generated XML file.

Welcome in the world of WordML! Enjoy it...

?> | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under:


# re: Generating Word documents with XML and XSLT

Friday, July 02, 2004 12:16 PM by bart

Thx for your example, can you please provide me the different documents.

Because I don't understand everything.

Thank you

# re: Generating Word documents with XML and XSLT

Saturday, September 04, 2004 5:24 PM by bart

Have a look at this mate:

it's quite easy and really cool stuff!

# re: Generating Word documents with XML and XSLT

Tuesday, February 15, 2005 6:29 AM by bart

Its cool but Open Office does that for years.

Check XAML in 3 years.. You'll say "its cool" but Firefox/mozilla has this since 2002 (Xul).

Microsoft is not innovating.. but that's a good move :) Usefull for my online mail merge software.

# Wont work in down versions of word

Monday, August 29, 2005 2:26 PM by bart

Nice is your article..but the document wont run in lower versions of MS Word .i mean downward compatability is not there..
if you try opening the document in word2002 or 2000 will see only WordML syntax bcoz lower versions cant parse it..
There is less information on net on how to tackle this issue..
i am trying to solve it from 1 week..if you have any solution plz help me..