Betsy Rolland
LIS 600 Independent Study

Transforming XML to Microsoft XML
Terry Brooks
Winter 2006

Transform a plain XML file to WordprocessingML using XSL

The instructions below will walk you through creating an XSL style sheet to transform plain XML into a WordprocessingML file using XSL and XPath. The resulting file can be opened directly in Microsoft Word.

Documents:

PenPals.xml
PenPals_Styles_MS.xsl
PenPals_Transformed.xml

Steps:

  1. Start with an XML file, like PenPals.xml, above.

  2. Create a new XSL document.

  3. Your XSL file will need the following headers:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"> <!-- WordprocessingML namespace -->

  4. Open your XSL template tag (<xsl:template match="/">). Inside the template tag, add the following lines:
    <xsl:processing-instruction name="mso-application"> <!-- Associates this file with MS Word -->
    <xsl:text>progid="Word.Document"</xsl:text> <!-- Write this to XML doc to associate it with Word -->
    </xsl:processing-instruction>
    <w:wordDocument> <!-- Defining the Word Document -->
    <xsl:attribute name="xml:space">preserve</xsl:attribute> <!-- Preserves whitespace in the document. -->


  5. Define any styles you want to use in your document. For more information, please see the tutorial on using styles in Wordprocessing ML.

  6. Open your w:body tag. <w:body></w:body>

  7. Within the w:body, insert your text paragraph structures, as described in the previous tutorial. Use XPath and XSL elements to produce your text. For example:
    <w:p>      
      <w:r>    
        <w:t>  
          Whenever we have a party, my first penpal starts with her last snackfood, <xsl:value-of select="PenPals/Person[1]/SnackFood[last()]"/>, just like my last penpal who eats <xsl:value-of select="PenPals/Person[last()]/SnackFood[last()]"/>. It's my second penpal, <xsl:value-of select="PenPals/Person[2]/FirstName"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="PenPals/Person[2]/LastName"/>, who insists at starting with her first snack: <xsl:value-of select="PenPals/Person[2]/SnackFood[1]"/>.
        </w:t>  
      </w:r>    
    </w:p>      
    Note:
    WordprocessingML tags in purple
    XSL and XPath in red
    Text in black

  8. Close off all tags, then transform the original XML file.