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.

An XSLT file contains a set of instructions for transforming XML from one format to another format. Fortunately, you do not have to understand the XSLT language or WordML to create an XSLT file. Word provides a developer tool to help generate the XSLT file we need. The XSLT file can then be used to transform the customer order XML into the nicely formatted document shown in 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.
 

2.

Use the XML Data Views feature of Word to browse to the XSLT file and transform the XML data file back to the formatted Word document shown in 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.
 

2.

Whenever you open book order XML conforming to the book order schema, Word will automatically apply the XSLT transform to give back the formatted Word document shown in 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 FilesMicrosoft Office 2003 Developer ResourcesMicrosoft 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:

WML2XSLT.EXE "book sales.xml" -o "book sales.xslt"

"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.

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.

This view of the XML is nice and all, but we would like to get it back to the formatted document in Figure 22-21. When you open the XML data file, the XML Document task pane automatically appears. This task pane is the key to converting back to the formatted document view. Click the Browse option in the task pane to browse to the Book Sales.xslt XSLT file you created using WML2XSLT.EXE. After you have located the XSLT file, click Open. Word will then transform the Book Order.xml to a formatted Word document that looks like the document in Figure 22-21 but has the specific data changes you made to the Book Order.xml file. If you go to the XML Structure pane and check the Show XML tags in the document option, you will see that the formatted Word document created by using the XSLT file on the Book Order.xml file still has the XML mapping applied properly to it.

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.

Go to the Schema Library dialog by choosing Templates and Add-Ins from the Tools menu. Click the XML Schema tab in the Templates and Add-Ins dialog. Then, click the Schema Library button to display the Schema Library dialog shown in 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.

As shown in Figure 22-26, the XSLT file we created is now associated with the book order schema. This will cause Word to automatically apply the XSLT file when XML conforming to the book order schema is opened.

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

Now let's verify that the XSLT we have associated with the book order schema will automatically be applied. From Word, choose Open from the File menu. In the list of File Types, choose XML Files (*.xml). Browse to the Book Order.xml file you created and click Open. Instead of defaulting to an XML-only view as shown in Figure 22-24, Word now opens the XML file and automatically applies the XSLT to display the formatted document, as shown in Figure 22-27. It also shows the XML Document task pane, which gives the user the option to go back to the data-only view or pick some other XSLT file that can transform XML conforming to the book order schema into a formatted document.

Figure 22-27. Word automatically applies the book order view solution and XSLT.






Visual Studio Tools for Office(c) Using C# with Excel, Word, Outlook, and InfoPath
Visual Studio Tools for Office(c) Using C# with Excel, Word, Outlook, and InfoPath
ISBN: 321334884
EAN: N/A
Year: N/A
Pages: 214
Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net