Question

This is a sample from my XML:

<RecommendedAction>
   <table border="1" cellpadding="1" style="width:500px"><tbody><tr><td></td><td></td></tr><tr><td></td><td></td></tr><tr><td></td><td>f</td></tr></tbody></table>
   <table border="5" cellpadding="50" style="width: 300px;"><tbody><tr><td><br /></td><td><br /></td></tr><tr><td><br /></td><td><br /></td></tr><tr><td><br /></td><td><br /></td></tr></tbody></table>
</RecommendedAction>

I have two questions:
1. How can I use the width attribute in my table template?
2. How can I use the cellpadding attribute in my td template?

This a sample from my XSL:

<xsl:template match="table">
    <fo:table>

        <xsl:if test="@border"> 
            <xsl:attribute name="border-style">
                <xsl:value-of select="'solid'"/>
            </xsl:attribute>
            <xsl:attribute name="border-color">
                <xsl:value-of select="'black'"/>
            </xsl:attribute>
            <xsl:attribute name="border-width">
                <xsl:value-of select="@border"/>
            </xsl:attribute>
        </xsl:if>

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

<xsl:template match="col">
    <fo:table-column column-number="{@number}" column-width="{@width*1.5}"/>
</xsl:template>

<xsl:template match="tbody">
    <fo:table-body start-indent="0pt" end-indent="0pt">
        <xsl:apply-templates />
    </fo:table-body>
</xsl:template>

<xsl:template match="tr">
    <fo:table-row>
        <xsl:apply-templates />
    </fo:table-row>
</xsl:template>

<xsl:template match="td">
    <fo:table-cell>
        <fo:block>
            <xsl:apply-templates />
        </fo:block>
    </fo:table-cell>
</xsl:template> 
Was it helpful?

Solution

How can I use the width attribute in my table template?

You need a template to process your style attribute which will extract its CSS properties. Here is something I've written a while ago for xslt-1, for xslt-2 this can be written much more elegantly:

<xsl:template name="process-style">
    <xsl:param name="style"/>
    <!-- get property name -->
    <xsl:variable name="name" select="normalize-space(substring-before($style, ':'))"/>

    <xsl:if test="$name">
        <xsl:variable name="value-and-rest" select="normalize-space(substring-after($style, ':'))"/>
        <xsl:variable name="value">
            <xsl:choose>
                <xsl:when test="contains($value-and-rest, ';')">
                    <xsl:value-of select="normalize-space(substring-before(
                        $value-and-rest, ';'))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$value-and-rest"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <!-- process css properties -->
        <xsl:choose>
            <xsl:when test="$name = 'width'">
                <xsl:attribute name="width">
                    <xsl:choose>
                        <xsl:when test="contains($value,'px')">
                            <!-- px to mm conversion -->
                            <xsl:value-of
                                select="number(normalize-space(substring-before($value,'px'))) * (2.54 div 72 * 10)"/>
                            <xsl:text>mm</xsl:text>
                        </xsl:when>
                        <!-- add additional conversions here if needed -->
                        <xsl:otherwise>
                            <xsl:value-of select="$value"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:attribute>
            </xsl:when>
            <xsl:when test="$name = 'background'">
                <!-- add implementation for background processing -->
            </xsl:when>
            <!-- add addtional processing for other css properties here as needed
                <xsl:when test="$name = 'color'"></xsl:when>
            -->
            <xsl:otherwise> </xsl:otherwise>
        </xsl:choose>
    </xsl:if>
    <xsl:variable name="rest" select="normalize-space(substring-after($style, ';'))"/>
    <xsl:if test="$rest">
        <xsl:call-template name="process-style">
            <xsl:with-param name="style" select="$rest"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

Add this to your stylesheet, and extend as needed to support other CSS properties that may appear in your style attributes.

Than call it like this:

<xsl:template match="table">
<fo:table>
     <xsl:call-template name="process-style">
        <xsl:with-param name="style" select="@style"/>
    </xsl:call-template>
...

How can I use the cellpadding attribute in my td template?

In xsl-fo you cannot set cell-padding on the table so you would have to get it for each cell:

<xsl:template match="td">
<fo:table-cell>
    <fo:block>
        <!-- 
          add padding to the block, 
          maybe overwrite with padding that might be stored with the td 
        -->
        <xsl:attribute name="padding"><xsl:value-of select="ancestor::table/@cellpadding"/></xsl:attribute>
        <xsl:apply-templates />
    </fo:block>
</fo:table-cell>
</xsl:template> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top