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.

Advertisements

16 Responses to Document Generation using WordML (Word 2003)

  1. divya says:

    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. susa says:

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

  3. sonia says:

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

  4. Code Jedi says:

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

  5. sonia says:

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

  6. susa says:

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

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

    Please help me

  7. David Parry says:

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

  8. Code Jedi says:

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

  9. Ed Metz says:

    Do you have to create the document in the fashion described above as the last step. How would you run that?

    I thought once you had generated the XSLT all ytou needed to do was open up an XML file in word and then follow these steps

    On the File menu, click Save As.
    In the Save as type box, click XML Document.
    In the File name box, type the document name.
    Select the Apply transform check box.
    Click Transform, select an XSLT file, and then click Open.
    Click Save.

  10. nagashree says:

    How to include the reference to System.Xml.Xsl and where to include the function given after step 4 i.e., creating the document(merge data)? can anyone plz tell the solution for this?…..it’s very urgent plz….

  11. Deepika says:

    Can I use the tool with office 2007.

  12. Yasharth says:

    Hi,
    I am having a couple of issues regarding this method.
    First: how to deal with images in the data?I need to have a double column layout report which contains images as well.
    Second : A component of the report is in tabular form.I have the data to be filled in the table but am not able to figure out how to insert that data into the document.
    To add to the woes, I am using JAVA in this service.
    Is there a way to solve this issue?

  13. Nandu4u says:

    Hi,
    My problem relates to word ml. I have a checkbox in my word document. I am generating a pdf file of this document using stylus studio.I am tweaking the code of xsl files of stylus. Now my problem is i need to distinguish a tag which shows the checkbox checked or unchecked. I couldn’t able to find the difference. Please any one can help, do this..Appreciate ur help..

  14. John S says:

    My problem is that when I apply the transform to an input document, the content in the result is the content from the seed document and not the input document. What am I doing wrong?

  15. siddiq says:

    How can i use the same approach with office 2010 document templates

  16. Veery energetic blog, I loved that bit. Will there be a part
    2?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: