When Word opens an XML file, it first checks to see if the file is a WordprocessingML document, by comparing the namespace URI of the root element with the WordprocessingML namespace (http://schemas.microsoft.com/office/word/2003/wordml). If they are not equal, then Word applies an XSLT transformation to the document. Which stylesheet it applies depends on whether there is an entry in the machine's schema library that corresponds to the namespace of the document's root element, and whether that entry has an accompanying XSLT "solution." If Word does not find one, it applies its own default XSLT stylesheet. The flow chart in Figure 4-10 details this logic. Figure 4-10. How Word decides which XSLT stylesheet to apply, if anyWe can relate this back to our press release example very easily. When an employee in our imaginary PR department opens an XML document whose root element's namespace is http://xmlportfolio.com/pressRelease, then Word will apply the default XSLT stylesheet associated with the "Press Release" solution. This is assuming that the user's machine has the schema and accompanying stylesheets registered in its schema library (as was reflected in the example "Schema Library" dialog in Figure 4-8). So the sequence (with respect to the flow chart in Figure 4-10) goes like this:
In Figure 4-6, we saw an example of the result of this sequence Word displaying the "Elegant" view of a newly opened press release document. On the other hand, if a user was to open a press release document without ever having installed the Press Release schema in their machine's schema library, then the sequence would be different:
In this case, the user gets a very different view of the XML document, without any custom formatting specific to press releases, and without any document protection features (editing and formatting restrictions) enabled. Figure 4-11 shows an example of Word's generic view for arbitrary XML documents. This view is called "Data only" in the Word UI (not to be confused with the "Save data only" Save option). Figure 4-11. The "Data only" view what Word displays when opening an arbitrary XML documentWhat we see in Figure 4-11 is the result of applying Word's default onload XSLT stylesheet to the press release XML document from Example 4-2. You can find Word's default onload stylesheet on your hard disk at C:\Program Files\Microsoft Office\OFFICE11\XML2WORD.XSL. This XML2WORD.XSL file contains the actual stylesheet that Word executes to display the "Data only" view. It is a good example of how to write an onload XSLT stylesheet; it even includes descriptive comments.
4.5.1 Multiple Views for the Same SchemaIt is possible in Word to create multiple views, i.e., multiple onload XSLT stylesheets, for the same schema. These are represented as multiple associated "solutions" in the schema library, one of which must be the default. When a user first opens an instance document, the non-default views are presented as alternative "XML data views" in the XML Document task pane. Even when there is only one XSLT stylesheet associated with the schema like the "Elegant" view in our press release example Word still shows the XML Document task pane, giving the user the option to view Word's generic "Data only" view, or to browse to another XSLT file to apply. In any case, once the user makes any changes to the document, the XML Document task pane will disappear and they will not be able to change the view again. |