As you see from the simple ContactML XML schema presented earlier in this chapter, rigorous control of the data types, structure, and content arrangement is a key requirement of working in XML. We've discussed previously how Word's integration with XML also provides great flexibility. This flexibility is provided in part by XML Solutions.
In Word an XML Solution is an XSLT Transformation that is linked to a specific XML schema. This capability allows you to keep one set of data in an XML structure and link transformations representing different purposes of the data. When a new purpose (or solution) is identified, a new transformation can be added to the schema without having to move around any data.
XML Solutions in Word are managed from within the Schema Library. To access the Schema Library, select the Tools, Templates and Add-Ins menu, click the XML Schema tab, and then click the Schema Library button. XML Solutions are managed in the bottom half of this dialog (see Figure 25.33).
Follow these steps to add a solution:
In the Select a Schema list, click on the schema to which the solution will be added.
Click the Add Solution button. The Add Solution dialog is displayed (see Figure 25.34).
Navigate to the XSLT transformation file (.xsl) and click Open. The Solution Settings dialog is displayed (see Figure 25.35).
Enter an alias name to identify the solution and click OK. The added solution will be displayed in the Select a Solution list. If this is the first solution to be added, it will also show up in the Default Solution selection.
To modify the solution settings, select the solution in the Select a Solution list and click the Solution Settings button. You can modify the alias name, change the path to the transformation file, or control whether this change affects all users (see Figure 25.35).
To delete a solution, select the solution in the Select a Solution list and click the Delete Solution button.
If you have multiple solutions attached to a schema, you can change the default solution using the Default Solution drop-down list.
After you have completed your changes to the Solutions, click OK to dismiss the dialog.
We now have a good picture of how to create XML data based on a schema, what XSLT transformations represent, and how to associate them with a schema. Now how can we use that information to manage our data? Remember that our primary goals are to keep data in a well-defined structure, reduce duplication of input, and support repurposing of the information that has already been captured.
Let's think about our simple contact-list example and how this might apply. We have a friendly front end to enter the information with the data being saved in XML format based on an XML schema. We can include as many contacts in our list as we want and can extract the pure data (save as data only) anytime we want. After using this document for a time, we get tired of paging through all the contact records and realize that we need a simple list of names and email addresses. In the past we probably would have created a new document and then paged through the contact list copying and pasting entries into the other document. After this new document was created we would have duplicate sources of data that have to be separately maintained.
To produce our email list using XML, we can follow these steps:
Use File, Save As to create a new XML file with data only.
Create an XSLT transformation file (.xsl) with the following code:
<?xml version=" 1.0" ?> <xsl:stylesheet xmlns:xsl=" http://www.w3.org/1999/XSL/Transform" version=" 1.0" xmlns:cl=" http://tempuri.org/contactML.xsd" > <xsl:template match=" /" > <ContactList> <xsl:for-each select=" cl:ContactList/cl:Contact" > <Contact> <name><xsl:value-of select=" cl:Fullname/cl:lastname" />, <xsl:value-of select=" cl:Fullname/cl:lastname" /> </name> <email><xsl:value-of select=" cl:email" /></email> </Contact> </xsl:for-each> </ContactList> </xsl:template> </xsl:stylesheet>
Add the transformation file as a solution associated with the ContactML schema.
Open Word, select the File, Open menu, and pick the data-only XML file. When the file is loaded, you will be presented with the list of data views that are available (see Figure 25.36).
Select the Data Only and the Email List data views to see the two listings.