xml - Using table attributes -
this sample 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 2 questions:
1. how can use width attribute in table template?
2. how can use cellpadding attribute in td template?
this sample 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>
how can use width attribute in table template?
you need template process style attribute extract css properties. here i've written while ago xslt-1, xslt-2 can written more elegantly:
<xsl:template name="process-style"> <xsl:param name="style"/> <!-- 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 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 background processing --> </xsl:when> <!-- add addtional processing other css properties here 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 stylesheet, , extend needed support other css properties may appear in style attributes.
than call 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 use cellpadding attribute in td template?
in xsl-fo cannot set cell-padding on table have each cell:
<xsl:template match="td"> <fo:table-cell> <fo:block> <!-- add padding block, maybe overwrite padding might stored td --> <xsl:attribute name="padding"><xsl:value-of select="ancestor::table/@cellpadding"/></xsl:attribute> <xsl:apply-templates /> </fo:block> </fo:table-cell> </xsl:template>
Comments
Post a Comment