XSL Transforms and WordML


I was recently reviewing some tech comments from the product team for an article we wanted to publish on the Office Developer Center (http://msdn.microsoft.com/office). Among the comments was one about XSL transforms when a document loads in Microsoft Office Word 2003 (yes- that’s the officially branded name!). I wanted to review this comment here, because we do get questions about this behavior in Word 2003. Also, I had not really noticed the behavior in the little demos and so forth I had written, mainly because of the way I ran them.

Here’s how it works: when you open an XML Word document, the XML Data  Views task pane opens up. In this pane, you can choose whether you want to see the data of the document only or if you want to apply a transform. If your document has a stylesheet reference, you will see the referenced XSL listed. Optionally, you can browse for a different XSL.

Now, here’s the key- if you make a change to the document, the option of choosing an XSL goes away. In other words, choosing an XSL transform can only be done when the document is freshly opened or when you are saving the document. Once you make a change in the document, you lose the “live” xsl transform capability, although you can still apply a transform when you save the document.

Closing an re-opening the XML document will always give you the ability to reapply a transform, you just can’t reapply while a document is being modified

Comments (6)

  1. Kent Tegels says:

    Is there a way with VBA to replly the transform dynamically, like on a button click?

  2. Bill Coan says:

    In my opinion, the way to think about the Data View feature (i.e. XSL support) is that it allows you to view previously created data through your choice of transforms, rather than to switch views of your current data on an ongoing basis while you edit.

    Keep in mind that the XSL file needs to run on a complete XML file, one that has no well-formedness errors and, ideally, one that has no validation errors.

    After you start editing the file, all bets are off and you need to save your work so that Word can write out the changes as a complete XML file (with or without WordProcessingML elements, at your option). Then, if desired, you can give the XSL another whack at the file by closing and reopening it.

    Certainly these steps could be accomplished in a macro and initiated with a button-click, but the macro would have to prompt you to decide what to do about the changes, whether to throw them out or save them before closing and reopening the file. If that’s important to you, you could post a question to the microsoft.public.office.xml newsgroup.

  3. Kent Tegels says:

    Granted, and will do. Thanks for the response, Bill.

  4. Mary McRae says:

    Hi Kent,

    Another option is to create a Smart Document – if you do that, you can automatically include transforms as part of your code. In our SyntoniX solutions, XML is inserted via the Doc Actions task pane. The code does two things – inserts the proper XML structure at the proper location and applies styles to that new content – either through a transform or through traditional VBA techniques, depending on what’s most appropriate.