Template-based generation of WordprocessingML

| No Comments | No TrackBacks

Well, here is a small basic example how to generate WordprocessingML documents basing on templates. Saying "template" I don't mean regular *.dot Word templates, but just WordprocessingML XML document with predefined document/paragraph/run level properties and styles. Having such template document basically all needs to be done is to fill it with real content.

Here is such a template, it's empty Word 2003 document where I defined new style called MyFancyStyle and saved document as XML.

Here is the source of the content - a hypothetical report:

<?xml version="1.0">
url="http://www.internettrafficreport.com/main.htm">Internet Traffic Report</link>
reports on the current performance of major Internet routes around the world.</report>

And here goes XSLT stylesheet:

<xsl:stylesheet version="1.0" 
    <xsl:variable name="root" select="/*"/>
    <xsl:template match="/">
    <xsl:template match="@*|node()" mode="template">
            <xsl:apply-templates select="@*|node()" 
    <xsl:template match="w:body/wx:sect" mode="template">
            <xsl:apply-templates select="$root"/>
            <xsl:copy-of select="wx:sectPr"/>
    <xsl:template match="report">
                <w:pStyle w:val="MyFancyStyle"/>
    <xsl:template match="text()">
            <w:t><xsl:value-of select="."/></w:t>
    <xsl:template match="link">
        <w:hlink w:dest="{@url}">
                    <w:rStyle w:val="Hyperlink"/>
Basically what the stylesheet does? It opens template document, recursively copies all its content till it reaches w:body/wx:sect element. That's considered to be entry point for the content. Then stylesheet runs usual processing of the actual content in the source document, applying MyFancyStyle to the paragraph and after that copies wx:sectPr. Not rocket engineering indeed.

And finally here is how the result looks like. Note, real content is styled by MyFancyStyle style defined in the template.

Related Blog Posts

No TrackBacks

TrackBack URL: http://www.tkachenko.com/cgi-bin/mt-tb.cgi/118

Leave a comment