2
votes

I have an XML document and I am creating an XSL-FO file to convert it to pdf with apache-fop.

In the xml, there are sections <code> to show... code.

In the xsl-fo, I added the white-space="pre" sentence to preserve the code format, but tabulations are shown like single space:

XML section:

<code><![CDATA[
function callback( widget )
{
    var ui = widget; // It should be a tab
}
]]></code>

XSL-FO section:

<xsl:template match="code">
    <fo:block font-size="10pt" font-family="monospace" white-space="pre" color="#008000" background-color="#f8f8f8">
        <xsl:apply-templates/>
    </fo:block>
</xsl:template>

Resulting PDF:

function callback( widget )
{
 var ui = widget;  // It should be a tab
}

So my question is: How to preserve or set the size of the tabulation?


Edited: I am using apache-fop 1.1

A full example:

proyecto.xml (do not forget to replace the 4 spaces by a tab before "var ui...")

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="proyecto.xsl"?>
<document>
<code><![CDATA[
function callback( widget )
{
    var ui = widget; // It should be a tab
}
]]></code>
</document>

proyecto.xsl

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <xsl:template match ="document">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
            <fo:layout-master-set>
                <fo:simple-page-master master-name="OnePage" margin="1in" 
                    page-height="29.7cm" 
                    page-width="21cm"
                    margin-top="2.5cm"
                    margin-bottom="2.5cm"
                    margin-left="3.5cm"
                    margin-right="2.5cm">
                    <fo:region-body margin="0cm"/>
                </fo:simple-page-master>
                <fo:page-sequence-master master-name="Page">
                    <fo:single-page-master-reference master-reference="OnePage"/>
                </fo:page-sequence-master>
            </fo:layout-master-set>

            <fo:page-sequence master-reference="Page">
                <fo:flow flow-name="xsl-region-body">
                    <xsl:apply-templates/>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>

    <xsl:template match="code">
        <fo:block font-size="10pt" font-family="monospace" white-space="pre">
            <xsl:apply-templates/>
            <!--<xsl:value-of select="replace( . , 'a', 'b')"/>-->
        </fo:block>
    </xsl:template>

</xsl:stylesheet>

PDF result (screen):

enter image description here

1
You need to provide more context. That is, an actual XML input sample and the XSLT stylesheet that transforms it into XSL-FO. Otherwise, we cannot reproduce your issue. By the way, say what XSLT processor and versions you are working with (XSLT 1.0 or 2.0, version of FOP). - Mathias Müller
@Mathias Müller: I just added an xsl-fo section, but the whole xml take about 100pages and the xsl-fo some pages also. The apache-fop version is the last one 1.1, which is compatible with most of XSL-FO 1.1 - Adrian Maire
@AdrianMaire I was asking you to provide any stylesheet and input XML that expose your problem. It can be simple, but make sure what troubles you is still there. Only then can we reproduce your problem, otherwise all bets are off. @mzjn white-space="pre" should already set those attributes, w3.org/TR/xsl/#white-space. - Mathias Müller
@mzjn: Your solution sound good, but at the moment I am unable to make replace() to work. I still looking what is the issue: apache-fop say something about "wrong type" - Adrian Maire

1 Answers

1
votes

Replace the tabs by four spaces, as suggested by @mzjn already in the comments.

XML Input

<code><![CDATA[
function callback( widget )
{
&#09;var ui = widget; // It should be a tab
}
]]></code>

XSLT Stylesheet

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <fo:root>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="A4-portrait"
              page-height="29.7cm" page-width="21.0cm" margin="2cm">
          <fo:region-body/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="A4-portrait">
        <fo:flow flow-name="xsl-region-body">
          <fo:block font-size="10pt" font-family="monospace" linefeed-treatment="preserve" white-space-collapse="false" white-space-treatment="preserve" wrap-option="no-wrap" color="#008000" background-color="#f8f8f8">
            <xsl:value-of select="replace(code,'&#09;','    ')"/>
          </fo:block>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>

</xsl:stylesheet>

Now, after replacing the tab characters with whitespaces, the output is rendered correctly by FOP.

enter image description here

There is no obvious flaw with outputting tabs, but bear in mind that Apache FOP is only partially compliant in regard to the whitespace-treatment property: http://xmlgraphics.apache.org/fop/compliance.html , even if it says that only fo:inline elements are affected of this.