Document Generation using WordML (Word 2003)


Grumpy Wookie, Ed Richard, Andrew Buttigieg and I have been working recently on generating documents using WordML in Word 2003, here is a step wise guide to get you started.

Creating documents using XML and WordML, requires two files:
1. XML file containing the data
2. The XSL transformation file.

The XML file contains the data that will be applied to the document, while the XSL transformation file contains the metadata required to transform the data into a Word Document. Multiple XSL files can be used against the one XML document, allowing the data to be displayed in multiple ways.

The Microsoft Office Word 2003 XSLT Inference Tool is used to simplify the creation of the XSL document. The simplest way to create the XSL file is to do the following:
1. Create an XML file with sample data.
2. Open the XML file with Word and format the document.
3. Save the “seed” document with the WordML mark-up.
4. Create the XSL file using the Microsoft Office Word 2003 XSLT Inference Tool.

Additionally you can create an XML schema, and use that when creating the seed document. The additional steps required for this is located in the Word 2003: XML SDK’s Memo Styles Sample.

Install Tools

1. Download & install Word 2003: XML SDK
http://www.microsoft.com/downloads/details.aspx?familyid=ca83cb4f-8dee-41a3-9c25-dd889aea781c&displaylang=en
2. Navigate to following directory : C:\Program Files\Microsoft Office 2003 Developer Resources\Microsoft Office Word 2003 XML SDK\Tools
3. Run installer for “XSLT Inference Tool” (wml2xsl.msi)
a. This will install the command line tool to following dir C:\Program Files\Microsoft Office 2003 Developer Resources\Microsoft Office 2003 WordprocessingML Transform Inference Tool

How to create an XSL file

This section details the steps required to create the XSL file outlined in the previous section.

Step 1: Create an XML file with sample data
With an XML editor or Notepad, create an XML file with the following structure.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<log xmlns="logs"><logon>Logon</logon>
<shutdown>Shutdown</shutdown>
<restart>Restart</restart>
<logoff>Logoff</logoff>
</log>

The XML file created in this step needs to contain elements for each data item to be included in the template. Additionally it is important that the XML namespace is included in the file (eg: <log xmlns="logs">) as it is required to create the XSL and transform the data.

Step 2: Open the XML file with Word and format the document.
Open the previously created XML document with Word. Word will open the XML file, displaying the mark-up Labels. The Ctrl-Shift-X key combination to show and hide the XML mark-up labels.

Now format the document as desired, add headers and footers, etc. Ensure that you do not remove the elements while formatting the document. Additional elements can be added to the document by using the XML Structure task pane.

Step 3: Save the “XSL-template” document with the WordML mark-up.
The “XSL-template” document is the Word document saved in WordML format that is used to by the Microsoft Office Word 2003 XSLT Inference Tool to create the XSL Transform file. Once the document is formatted as desired, create the seed document by:
1. Click File, and then Save As…
2. Ensure Save as Type is XML Document
3. Uncheck Save data only
4. Click Save

Step 4: Create the XSL file using the Microsoft Office Word 2003 XSLT Inference Tool.
The seed document created in step 3 is used by the Microsoft Office Word 2003 XSLT Inference Tool to create the XSL Transform file. Create the XSLT transform file by:
1. Open the command prompt.
2. Change to the Microsoft Office Word 2003 XSLT Inference Tool directory.

Note: The XSLT Inference tool is a command-line–only tool, and is installed by default to the following directory C:\Program Files\Microsoft Office 2003 Developer Resources\Microsoft Office 2003 WordprocessingML Transform Inference Tool

3. Execute the wml2xslt.exe file with the required prameters: wml2xslt.exe “<path to XML template file>” –db
4. Select the namespaces from the dialogue, and then click OK.

Note:

Do not check the http://schemas.microsoft.com/office/word/2003/wordml/sp2 namespace. Doing so will prevent the data from being correctly applied to the document.

Creating the document (merge data)

Once the XML file containing the data and the XSL transformation file are ready, the below function can be used to create the document.

Note: You will need to include a reference to “System.Xml.Xsl”

Public static void WordMLTransform(
string dataPath,
string xsltPath,
string outputFile)
{
  XslCompiledTransform xslt = new XslCompiledTransform();
  xslt.Load(xsltPath);
  xslt.Transform(dataPath, outputFile);
}

The function’s parameters are:
· dataPath: The path of the XML file which contains the data for the document.
· xsltPath: The path of the XSL Transformation file used to transform the data into the document.
· outputFile: The path for the output file which will be created.

Note: When Word is installed on the local computer, Office 2003 detects the application for which an XML file saved out of Word should open. However, when an Office XML file with a ".xml" extension is served from a Web server, the association is not inherently recognizable, so the browser draws the page as an XML tree. To address this issue, use the ".doc" extension to make the file open in the Microsoft Word. The file is still pure XML.

Comments (8)

  1. williamcornwill says:

    divya Says:

    August 31, 2007 at 9:37 pm

    when the xml is opened as a word document…the mark up tags not displayed..only data is displayed…what to do now…

    also when the document is saved theres no option as” save data only”

  2. williamcornwill says:

    susa Says:

    August 31, 2007 at 9:39 pm

    yes …i am also facing the same problem….. please reply sooooooooon………

  3. williamcornwill says:

    sonia Says:

    August 31, 2007 at 9:40 pm

    me too having same problem……..can anyone tell some solution…….i urgently need

    this…

  4. williamcornwill says:

    Code Jedi Says:

    August 31, 2007 at 10:10 pm

    Please note that this article relates to Word 2003. Are you experiencing these problems with that version?

  5. williamcornwill says:

    sonia Says:

    August 31, 2007 at 11:24 pm

    yes..but mine is Microsoft office standard edition 2003..now i got it in professional edition.thanks for ur response.

  6. williamcornwill says:

    susa Says:

    September 1, 2007 at 12:35 am

    in “step 4″ while executing the command i got the following error

    “NO XML MARKUP FOUND”… ” xslt formatting failed”

    Please help me

  7. williamcornwill says:

    David Parry Says:

    October 23, 2007 at 11:23 pm

    Could someone please clarify whether or not this technique will work with Microsoft Office 2003 Standard edition ?

  8. williamcornwill says:

    Code Jedi Says:

    October 24, 2007 at 8:08 am

    David, I can confirm that you will need Office 2003 Professional edition instead of Standard edition in order to accomplish this.

Skip to main content