Intro to Word XML Part 5: Opening custom XML
Intro to Word XML Part 5: Opening custom XML files
I've been talking for awhile now about the support for custom defined schemas in Office. I'm actually going to pull together a post in the next week or so that addresses the uses and motivations behind the XML support we have in Office. We talk about XML a lot, and it should be clear by now that there are a ton of uses. From an Office point of view, there is no such thing as a single "XML editor", but instead a collection of tools that use XML to improve the power of their scenarios. Word can open generic XML, but that doesn't mean it should be used as a generic XML editor. It wouldn't really make sense to open Excel's XML in Word, since SpreadsheetML is used to describe a spreadsheet, and would be fairly difficult to edit in a Word processor. Of course Word and Excel both have a collection of shared functionality, but those are subsets of the larger overall set of functionality in each application. I plan to address this in more detail soon because I think it's really important to understand this when you are exploring the XML functionality and trying to determine what tools best suit your scenarios.
For today though let's talk about generic XML editing in Word. You can open any XML file you want it Word, and depending on how you set Word up, you can even teach Word to display your XML in a rich way. In part 3, I showed how you could create a WordprocessingML file that had your own XML in it as well. If you start with an XML file that is just made up of your XML, you can create an XSLT that will teach Word how to display your XML.
Opening an XML file
Let's start with a basic XML file:
<?xml version="1.0"?>
<s:employee xmlns:s="http://jonesxml.com/schemas/example1">
<s:name>Brian Jones</s:name>
<s:occupation>Program Manager</s:occupation>
</s:employee>
Try opening that file in Word. The result should be that you get a simple text document with your tags showing. This gives the appearance that Word is able to internally open any XML file. This is actually not quite what's going on. It's really more similar to what happens when you open an XML file in IE without applying a transform. Word sees that the XML is not in it's namespace, so it looks to see if there is a transform specified. If there isn't a transform, Word will fall back to using a default XSLT that transforms into WordprocessingML. The transform that we use is found in the programs folder: c:\Program Files\Microsoft Office\OFFICE11\XML2WORD.XSL
Go ahead and open that file up. You'll see that we map custom XML into a hybrid of WordprocessingML and the custom XML. We apply some indentation based on how deep the tags are nested which gives you that tree view like appearance. We also specify that that XML tag view should be on, just like we did in the example I posted in part 3 of the intro to Word XML. Also notice that we create this tag: <w:removeWordSchemaOnSave w:val="on" />. That tells Word that when the user hits the save button, the document should be saved as "data only", which removes the WordprocessingML. That's why you can open any generic XML file, make some edits, and press just press save.
Now, what I've just described doesn't exactly fit with what our goals were for the XML support in Word. We weren't trying to make Word into a generic XML editor. Our main goals were to make it much easier for people to build solutions in Word that were document based solutions. Word is a document editor, and by adding XML support to Word, the solutions you build become easier and more powerful. Visual Studio is really a better example of a generic XML editor.
Applying an XSLT to your data
If you want to open you're XML data in Word and have it formatted in a richer way than the default XSLT provides (which is probably almost always the case), then you can generate an XSLT. Let's say that we want to format this custom XML to look the same as the file looked that we built in part 3 of the intro to Word XML. We would just need to create an XSLT that output that same WordprocessingML. The XSLT would look something like this:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:s="http://jonesxml.com/schemas/example1">
<xsl:template match="/">
<w:wordDocument>
<w:docPr>
<w:showXMLTags w:val="off" />
</w:docPr>
<xsl:apply-templates />
</w:wordDocument>
</xsl:template>
<xsl:template match="s:employee">
<w:body>
<s:employee>
<w:p>
<w:r>
<w:rPr>
<w:b />
</w:rPr>
<w:t xml:space="preserve">Name: </w:t>
</w:r>
<s:name>
<w:r>
<w:t><xsl:value-of select="s:name" /></w:t>
</w:r>
</s:name>
</w:p>
<w:p>
<w:r>
<w:rPr>
<w:b />
</w:rPr>
<w:t xml:space="preserve">Occupation: </w:t>
</w:r>
<s:occupation>
<w:r>
<w:t><xsl:value-of select="s:occupation" /></w:t>
</w:r>
</s:occupation>
</w:p>
</s:employee>
</w:body>
</xsl:template>
</xsl:stylesheet>
Save that XSLT file onto you're machine and now open the custom XML file again in Word. Notice the task pane to the right called the "XML Document" pane. You can see that the "Data only" transform was applied, but you can choose to browse for a different one. Choose "Browse..." and find the XSLT file we just created. The XSLT should now be applied and you should have a file that looks really similar to the one we created the other week. We specified that the XML tag view should be off, but you can turn them back on by pressing "CTRL + Shift + X".
There's a simple example of creating an XSLT. You can now play around with changing properties in the XSLT so that the data is displayed in different ways.
-Brian