Thread: Help with style
View Single Post
 
Old November 2nd, 2006, 07:11 AM
mhkay's Avatar
mhkay mhkay is offline
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Not a bad start. I would be inclined to use <xsl:apply-templates/> in places where you have used xsl:for-each or xsl:value-of select=".". That's especially with "document-oriented XML" where elements in the input document may occur in unexpected places. For example, tables might be nested one inside another.

So instead of

<xsl:template match="/">
        <body>
            <xsl:for-each select="w:wordDocument/w:body//w:tbl">
                <table>
                    <xsl:for-each select="w:tr">
                        <row>
                            <xsl:for-each select="w:tc">
                                <cell>
                                    <xsl:value-of select="."/>
                                </cell>
                            </xsl:for-each>
                        </row>
                    </xsl:for-each>
                </table>
            </xsl:for-each>
        </body>
    </xsl:template>

you want something more like:

<xsl:template match="*">

  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="text()">

</xsl:template>

<xsl:template match="/">
        <body>
            <xsl:apply-templates/>
        </body>
</xsl:template>

<xsl:template match="w:tbl">
                <table>
                    <xsl:apply-templates/>
                </table>
</xsl:template>

and so on.

One other point:

    <xsl:template match="//w:t">

appears in the email version of your posting but not in the forum version (how did you manage that???). Better to write match="w:t". Both match a w:t element anywhere in the document (the // is redundant). But the "//" changes the default priority of the rule, which can cause unexpected bugs if there are multiple rules that match the same elements.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference