htmlToFo Transformer (an XSL file which should have logic to copy even style attributes of HTML to FO)

144 Views Asked by At

I am using javax.xml.transform.Transformer to transform XHTML to XSL-FO using an XSL parser. But the parser I have is not able to copy style attributes to XSL-FO. So, can you help with valid parser which should be able to parse XHTML with style attributes to XSL-FO.

1

There are 1 best solutions below

0
On

You can expand on this simple example, using a choose structure to eliminate/change the names to recognized XSL FO attributes. There are things in HTML like a style that starts-with "-moz" that likely has no meaning in XSL FO. Some others need adjustments made. You would also need to handle direct attributes (like @colspan or @rowspan) that are not in the @style attribute.

Given this simple input:

<p style="font-size:12pt; font-weight:bold; color: red">This is a sample</p>

Using this XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" version="2.0">
<xsl:template match="/">
    <xsl:apply-templates/>
</xsl:template>
<xsl:template match="p">
    <fo:block>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates/>
    </fo:block>
</xsl:template>
<xsl:template match="@style">
    <xsl:variable name="styleList" select="tokenize(.,';')"/>
    <xsl:for-each select="$styleList">
        <xsl:attribute name="{normalize-space(substring-before(.,':'))}">
            <xsl:value-of select="normalize-space(substring-after(.,':'))"/>
        </xsl:attribute>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

You get this output:

<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format" font-size="12pt" font-weight="bold" color="red">This is a sample</fo:block

You can see the tokenize() function split the @style attribute into parts and those parts are used to create appropriate XSL FO attributes.

The site using similar methods is linked below. It has a very complex XSL for handling inline @style to XSL FO attributes.

Cloudformatter CSStoPDF