Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I insert spacing/padding in XSL-FO generated PDF for fields without any value?
    text
    copied!<p>I have the following XML:</p> <pre><code>&lt;sample&gt; &lt;value1&gt;This is one&lt;/value1&gt; &lt;value2&gt;Number two&lt;/value2&gt; &lt;value4&gt;Last value&lt;/value4&gt; &lt;/sample&gt; </code></pre> <p>Using Apache FOP/XSL-FO I would like a PDF looking similar to this:</p> <pre><code>Value 1: This is one Value 2: Number two Value 3: Value 4: Last value </code></pre> <p>Notice the spacing/padding between "Value 3:" and "Value 4:".</p> <p>The following transformation gives my the result I want. But it seems overly complicated (and might not perform very well for a real-life PDF with many values).</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"&gt; &lt;xsl:template match="sample"&gt; &lt;fo:root&gt; &lt;fo:layout-master-set&gt; &lt;fo:simple-page-master master-name="page-layout"&gt; &lt;fo:region-body margin="10mm" region-name="body"/&gt; &lt;/fo:simple-page-master&gt; &lt;/fo:layout-master-set&gt; &lt;fo:page-sequence master-reference="page-layout"&gt; &lt;fo:flow flow-name="body"&gt; &lt;fo:block&gt; &lt;xsl:variable name="pad"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="value1"&gt;5&lt;/xsl:when&gt; &lt;xsl:otherwise&gt;25&lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:variable&gt; &lt;fo:inline padding-right="{$pad}mm"&gt;Value 1: &lt;xsl:value-of select="value1"/&gt;&lt;/fo:inline&gt; Value 2: &lt;xsl:value-of select="value2"/&gt; &lt;/fo:block&gt; &lt;fo:block&gt; &lt;xsl:variable name="pad"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="value3"&gt;5&lt;/xsl:when&gt; &lt;xsl:otherwise&gt;25&lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:variable&gt; &lt;fo:inline padding-right="{$pad}mm"&gt;Value 3: &lt;xsl:value-of select="value3"/&gt;&lt;/fo:inline&gt; Value 4: &lt;xsl:value-of select="value4"/&gt; &lt;/fo:block&gt; &lt;/fo:flow&gt; &lt;/fo:page-sequence&gt; &lt;/fo:root&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Is there a simpler/better way to implement this?</p> <p><strong>Update:</strong></p> <p>I refactored the above into a template "field":</p> <pre><code>&lt;xsl:template name="field"&gt; &lt;xsl:param name="txt"/&gt; &lt;xsl:param name="val"/&gt; &lt;xsl:param name="pad"/&gt; &lt;xsl:variable name="p"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="$val"&gt;5&lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;xsl:choose&gt; &lt;xsl:when test="$pad"&gt;&lt;xsl:value-of select="$pad"/&gt;&lt;/xsl:when&gt; &lt;xsl:otherwise&gt;25&lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:variable&gt; &lt;fo:inline padding-right="{$p}mm"&gt;&lt;xsl:value-of select="$txt"/&gt;:&lt;/fo:inline&gt; &lt;fo:inline keep-with-previous="always" padding-right="5mm" font-weight="bold"&gt;&lt;xsl:value-of select="$val"/&gt;&lt;/fo:inline&gt; &lt;/xsl:template&gt; </code></pre> <p>Which can be used like this:</p> <pre><code>&lt;xsl:call-template name="field"&gt; &lt;xsl:with-param name="txt"&gt;Value 1&lt;/xsl:with-param&gt; &lt;xsl:with-param name="val"&gt;&lt;xsl:value-of select="sample/value1"/&gt;&lt;/xsl:with-param&gt; &lt;/xsl:call-template&gt; </code></pre> <p>The template takes a third optional parameter, pad. If specified its value will be used as padding.</p> <p>David's template (see accepted answer) uses a simpler if-contruct where the padding-right attribute is overwritten if needed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload