• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 4. Creating XML Templates in Wor... > How the onload XSLT Stylesheet Is Se...

4.5. How the onload XSLT Stylesheet Is Selected

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 any


We 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:

  1. Word opens a press release XML document.

  2. Is the root element's namespace the WordprocessingML namespace? No.

  3. Is the root element's namespace in the schema library? Yes.

  4. Does that schema library entry have an associated XSLT solution? Yes.

  5. Word applies the "Elegant" stylesheet, pr2word.xsl, to our press release document.

  6. Is the result document's root element in the WordprocessingML namespace? Yes, now it is.

  7. Word displays the result.

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:

  1. Word opens a press release XML document.

  2. Is the root element's namespace the WordprocessingML namespace? No.

  3. Is the root element's namespace in the schema library? No.

  4. Word applies its own default XSLT stylesheet.

  5. Word displays the result.

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 document


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

The loop that is present in the flow chart in Figure 4-10 reflects the fact that it is possible to create a switching pipeline of XSLT transformations, where the next stylesheet in the chain is determined based on the namespace of the previous result document's root element. This is certainly not a normal (or probably even intended) scenario, but it does raise some interesting possibilities. It gives you the ability to choose which view to apply based on values in the source document (assuming the necessary schema library entries). It probably always makes the most sense to just stick to conditional formatting within a single stylesheet, but, hey, it was an interesting behavior to discover.


4.5.1. Multiple Views for the Same Schema

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

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint