Importing an XML Data File into the Mapped Document
Let's consider the problem of how to get XML conforming to our customer order schema, such as the XML shown in Listing 22-1, imported into our formatted Word document shown in Figure 22-21. Word does not provide a menu command to import XML like Excel provides. Instead, Word relies on something called an XSLT file to transform XML conforming to our customer order schema to a formatted Word document in WordML format.Figure 22-21. We will first provide a brief checklist of the steps to do this and then consider the steps in more detail.To create the XSLT file, follow these steps:
1. | Save the formatted and XML mapped Word document shown in Figure 22-21 to the WordML file format. |
2. | Run the WordprocessingML Tranform Inference Tool (WML2XSLT.EXE) on the WordML formatted file to generate an XSLT file. This XSLT file will transform XML conforming to the book order schema back to the formatted Word document in WordML format. |
To manually convert the book order XML using the XSLT file, follow these steps:
1. | Open an XML file conforming to the book order schema in Word.Figure 22-21. |
To automatically use the XSLT file when book order XML is opened, follow these steps:
1. | Use the Schema Library dialog to add the XSLT file created by the WML2XSLT tool as a solution associated with the book order schema.Figure 22-21. |
Creating the XSLT File
The first step to creating an XSLT file is to take the document you created as shown in Figure 22-21 and save it in the WordML file format. To do this, choose Save As from the File menu. From the File Type drop-down, choose XML Document. Then make sure that the check boxes next to Apply transform and Save data only are not checked. Give the resulting WordML XML file a name such as Book Sales.xml. Save the file to a location where you can find it in the next step. Then click the Save button.Book Sales.xml is a WordML format document. It can be used as input to the WordprocessingML Transform Inference Tool to create an XSLT file that can transform XML conforming to our book order schema back to the Book Sales formatted Word document. The WordprocessingML Transform Inference Tool is available for download at http://www.microsoft.com/downloads/details.aspx?Family ID=2cb5b04e-61d9-4f16-9b18-223ec626080e&DisplayLang=en. Download and install the tool on your machine. It will typically install to the directory C:\Program Files\Microsoft Office 2003 Developer Resources\Microsoft Office 2003 WordprocessingML Transform Inference Tool.The transform inference tool is a console application called WML2XSLT.EXE. Open a command prompt and navigate to the directory where WML2XSLT.EXE is installed. For simplicity, we have copied the Book Sales.xml WordML file to the same directory where WML2XSLT.EXE is installed. At the command-line type this command:
"book sales.xml" is the input WordML file. "book sales.xslt" is the output XSLT file that WML2XSLT.EXE creates. After running this command, Book Sales.xslt is created in the same directory where WML2XSLT.EXE is installed.
WML2XSLT.EXE "book sales.xml" -o "book sales.xslt"
Manually Converting the Book Order XML File Using the XSLT File
Now, take the XML in Listing 22-1 and save it to a file called Book Order.xml. Edit the content of the file in some way so that it is different from the XML that was in Book Sales.xml. For example, change the customer name and some of the book titles. This will help convince you later that the XSLT file really works with arbitrary XML that conforms to the book order schema.Now, from within Word, choose Open from the File menu. In the list of File Types, choose XML Files (*.xml). Browse to the Book Order.xml file and click Open. Word opens the XML file in a nice data-only view, as shown in Figure 22-24.
Figure 22-24. Opening Book Order.XML and displaying it in Word's XML data-only view.

Automatically Applying an XSLT File When XML Conforming to the Book Order Schema Is Opened
Word provides a way of bypassing the extra steps of browsing to the XSLT file whenever you open the XML data file. Using the Schema Library, we can associate an XSLT file with an XML schema so that whenever XML conforming to that schema is opened the XSLT file will be applied automatically to the XML.Figure 22-6. With the book order schema selected, click the Add Solution button in the lower half of the dialog to associate an XSLT file with the book order schema. You will be prompted to browse for an XSLT file. Browse to the XSLT file created by WML2XSLT.EXE called Book Sales.xslt. Then click the Open button. The dialog shown in Figure 22-25 will appear. Give the XSLT file an alias (friendly name) of Book Order View. Then click the OK button.
Figure 22-25. Adding an XSLT solution to the book order schema.

Figure 22-26. The book order view and XSLT is associated with the book order schema in the Schema Library.

Figure 22-27. Word automatically applies the book order view solution and XSLT.
[View full size image]
