Search code examples
xmlxslt-1.0xslt-2.0apache-fop

Bold and Italic Tags in XSLT - Apache FOP - PDF


I used to generate PDF using Apache FOP with XML and XSL File,

XML

<?xml version="1.0" encoding="UTF-8"?>
<ReportMain>
    <ReportMainBody>
        <company>
             some <b>company</b>
        </company>
        <currency>
            some <i>other <b>currency</b></i>
        </currency>
    </ReportMainBody>
</ReportMain>

XSL

<?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <xsl:template match="ReportMain">
            <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
                <fo:layout-master-set>
                    <fo:simple-page-master master-name="simple"
                        page-height="8.5in" page-width="11in" margin-top=".5in"
                        margin-bottom=".5in" margin-left=".5in" margin-right=".5in">
                        <fo:region-body margin-top="2cm" margin-bottom="2cm" />
                        <fo:region-before extent="2cm" overflow="hidden" />
                        <fo:region-after extent="1cm" overflow="hidden" />
                    </fo:simple-page-master>
                </fo:layout-master-set>
                <fo:page-sequence master-reference="simple"
                    initial-page-number="1">
                    <fo:static-content flow-name="xsl-region-before">
                        <fo:block font-size="13.0pt" font-family="serif"
                            padding-after="2.0pt" space-before="4.0pt" text-align="center"
                            border-bottom-style="solid" border-bottom-width="1.0pt">
                            <fo:block>SAMPLE PDF</fo:block>
                        </fo:block>
                    </fo:static-content>
                    <fo:static-content flow-name="xsl-region-after">
                        <fo:block font-size="12.0pt" font-family="sans-serif"
                            padding-after="2.0pt" space-before="2.0pt" text-align="center"
                            border-top-style="solid" border-bottom-width="1.0pt">
                            <xsl:text>Page</xsl:text>
                            <fo:page-number />
                        </fo:block>
                    </fo:static-content>
                    <fo:flow flow-name="xsl-region-body">
                        <xsl:apply-templates select="ReportMainBody" />
                    </fo:flow>
                </fo:page-sequence>
            </fo:root>
        </xsl:template>

        <xsl:template match="ReportMainBody">
            <fo:block font-size="16pt" space-after="5mm">
                <xsl:value-of select="company"/>
            </fo:block>
            <fo:block font-size="16pt" space-after="5mm">
                <xsl:value-of select="currency"/>
            </fo:block>
        </xsl:template>

    <xsl:template match="b">
        <fo:inline font-weight="bold">
            <xsl:apply-templates select="node()"/>
        </fo:inline>  
    </xsl:template>

    <xsl:template match="i">
        <fo:inline font-style="italic">
            <xsl:apply-templates select="node()"/>
        </fo:inline>
    </xsl:template>
 </xsl:stylesheet>

I need to support bold and italics texts inbetween in my PDF, Actually i tried this solution from similar question in stack,

Please check with my XSLT Fiddle https://xsltfiddle.liberty-development.net/gVhDDyJ

Please post solution. Thanks in advance.


Solution

  • Your templates for b or i will only be used if you use apply-templates and not value-of so change

        <xsl:template match="ReportMainBody">
            <fo:block font-size="16pt" space-after="5mm">
                <xsl:apply-templates select="company"/>
            </fo:block>
            <fo:block font-size="16pt" space-after="5mm">
                <xsl:apply-templates select="currency"/>
            </fo:block>
        </xsl:template>
    

    https://xsltfiddle.liberty-development.net/gVhDDyJ/2

    If you still have problems then tell us exactly which output you want.