Applying an XSLT Transform [Word 2003 XML Reference] -- Microsoft Office Word 2003 XML Software Development Kit
Microsoft© Office Word 2003 allows the application of Extensible Stylesheet Language Transformation (XSLT) files to XML while several different ways. You do it either manually or automatically while opening a document or saving a document. You can also apply an XSLT while the document is open using Microsoft Visual Basic© for Applications (VBA). A complete description of each method follows.
Applying an XSL Transformation while saving a document
Caution If you apply an Extensible Stylesheet Language Transformation (XSLT) when you save an Extensible Markup Language (XML) file, Word saves only data that is included in the output of the transformation.
- 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.
Figure 1. XSLT options in the File Save As dialog box.
- Click Transform, select an existing XSLT file, and then click Open.
- Click Save.
Note This setting applies the XSLT to the entire contents of the document. To discard Office Word 2003 formatting and save only XML data, select the Save data only check box in the Save as dialog.
- On the Tools menu, click Templates and Add-Ins, and then click the XML Schema tab.
- Click XML Options.
Figure 2. XSLT options in the XML Options dialog box.
- Select the Apply custom transform check box, and then click Browse to locate the XSLT file.
Note You can override this default XSLT by selecting the Apply transform check box in the Save As dialog box and selecting a different XSLT file.
Note This setting applies the XSLT to the entire contents of the document. To discard Office Word 2003 formatting and save only XML data, select the Save data only check box in the XML Options dialog.
Applying an XSL Transformation to while opening a document
When you add an XML schema to the Schema Library, you can associate one or more Extensible Stylesheet Language Transformation (XSLT) files with the schema. When you attach that schema to a document and open the document in Microsoft Word, you can specify how to display the data by selecting one of the available data views, or XSLTs, to apply to the incoming XML data.
- On the File menu, click Open.
- Select the XML file to open.
- From the drop-down arrow on the Open button, select Open with transform.
- Select the XSLT to apply in the Choose an XSL Transformation dialog box.
- Click OK.
- On the Tools menu, click Templates and Add-Ins, and then click the XML Schemas tab.
- Click Schema Library.
Figure 3. Solution options in the Schema Library dialog box.
Note For more information see Schema Library.
- In the Select a schema box, click the schema defining the XML that you want to associate XSLTs with.
- In the Use solution with box, click Word.
- Click Add Solution, browse to the XSLT file, and then click Open.
- In the Solution Settings dialog box, select the options you want, and type a name for the XSLT in the Alias box.
Note The name you type for the alias is the name that appears in the list of available data views in the XML Document task pane.
Figure 4. Two sample schemas available in the XML Document task pane.
- Click OK.
- Repeat steps 4 through 6 for each of the XSLTs you want to associate with the schema.
- In the Default solution box, click the name of the XSLT that you want to use by default.
When you open a document containing XML in the namespace of a schema added by using the Select a schema button in the Schema Library dialog box, Office Word 2003 displays the XML Document task pane.
If you open a raw XML file in Office Word 2003 with no XSLT file selected, then Office Word 2003 uses a default data-only XSLT that lays the data out with the tags showing. When you open a raw XML file with no transform specified, Office Word 2003 uses its own default data-only transform that lays out the data in the XML file and displays it wrapped in the appropriate tags.
Note The XML Document task pane goes away once the document is edited. This happens because as soon as the document is edited Office Word 2003 can no longer guarantee the result of the XSLT.
Applying an XSL Transformation on an open document with VBA
Note The following steps assume that you are familiar with VBA and using the Office Word 2003 object model.
You can use VBA to apply a transform to the active document with a small amount of code.
- On the Tools menu, point to Macro, and then click Visual Basic Editor. You can also use the shortcut Alt+F11.
- From the View menu, choose Immediate Window.
- In the Immediate Window type:
- After the open parenthesis, enter values for
TransformDocument's two parameters:
- Path As String is the full path to the location of applied XSLT.
- DataOnly As Boolean is false you want the transform to affect all of the document's WordprocessingML and true if you want it to only affect the custom XML. This parameter parallel's the Save data only check box in the Save As dialog when saving a document as XML. This parameter defaults to True.
- Add the final closing parenthesis and hit Enter to apply the transform specified by the Path parameter.
For more information on the option settings used to configure the XML features in Word, see Understanding XML Options. For suggestions on troubleshooting problems that may occur while using these features, see Troubleshooting XML Documents in Word.
For additional information and examples that describe or demonstrate the end-to-end process of working with XML documents in Word, see the following articles:
©2004 Microsoft Corporation. All rights reserved. Permission to copy, display and distribute this document is available at: http://msdn.microsoft.com/library/en-us/odcXMLRef/html/odcXMLRefLegalNotice.asp