xmlxsltxsl-foantenna-house

XSLFO: Make the Starting Position of One Table Cell the Same as the End Position of Another Table Cell


I have a <fo:table/> with 3 columns, the table body of column 1 is number, column 2 is <fo:block><fo:inline>text</fo:inline><fo:leader/></fo:block>, column 3 is <fo:block><fo:block><fo:inline>text</fo:inline></fo:block>...<!--Repeat-->...</fo:block>. Sometimes the table cell of column 2 has a long text so it wraps to the next line, in this case I want table cell of column 3 starts at the end position of table cell of column 2.

Example:

1. short text table cell column 2 ............. table cell column 3
                                                continue

2. loooooooooooong text table cell              table cell column 3
   column 2 ................................... continue

For example 2 I want it to look like this:

2. loooooooooooong text table cell              
   column 2 ................................... table cell column 3
                                                continue

My code:

<xsl:template match="a">
    <fo:table>
        <fo:table-column column-width="5%"/>
        <fo:table-column column-width="60%"/>
        <fo:table-column column-width="35%"/>
        <fo:table-body>
            <fo:table-row>
                <fo:table-cell>
                    <fo:block>
                        <xsl:apply-templates select="." mode="numbering"/>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block display-align="after" text-align-last="justify">
                        <xsl:apply-templates select="b"/>
                        <fo:leader leader-pattern="dots" leader-pattern-width="1mm" leader-length.minimum="2mm" keep-with-next.within-page="always"/>
                    </fo:block>
                </fo:table-cell>                            
                <fo:table-cell>
                    <xsl:for-each select="c/d">
                        <fo:block>
                            <xsl:choose>
                                <xsl:when test="position()=1">
                                    <xsl:apply-templates select="."/>
                                </xsl:when>
                                <xsl:otherwise>
                                    <fo:block keep-with-previous.within-page="always" text-align="left">
                                        <xsl:apply-templates select="."/>
                                    </fo:block>
                                </xsl:otherwise>
                            </xsl:choose>
                        </fo:block>
                    </xsl:for-each>
                </fo:table-cell>
            </fo:table-row>
        </fo:table-body>
    </fo:table>
</xsl:template>

Thank you!


Solution

  • The short answer is to not use an fo:table but to use an fo:block with a large end-indent and a corresponding negative last-line-end-indent (see https://www.w3.org/TR/xsl11/#last-line-end-indent).

    The "column 3" text then goes in an fo:inline-container containing an fo:block with the text.

    If alignment is a problem, then you can reverse it and put the "column 2" text in an fo:inline-container and use start-indent and negative text-indent instead.


    Sample FO markup and Antenna House Formatter GUI screenshot (with area borders shown):

    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"
      xmlns:axf="http://www.antennahouse.com/names/XSL/Extensions" xml:lang="en">
      <fo:layout-master-set>
        <fo:simple-page-master master-name="spm" size="A5">
          <fo:region-body margin="36pt" />
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="spm">
        <fo:flow flow-name="xsl-region-body">
          <fo:list-block>
            <fo:list-item>
              <fo:list-item-label end-indent="label-end()">
                <fo:block>1.</fo:block>
              </fo:list-item-label>
              <fo:list-item-body start-indent="body-start()">
                <fo:block end-indent="140pt" last-line-end-indent="-140pt">loooooooooooong text table cell column 2<fo:leader
                  leader-pattern="dots" leader-length.optimum="100%" leader-length.minimum="40pt"/><fo:inline-container width="100pt" start-indent="0" end-indent="0" last-line-end-indent="0">
                  <fo:block width="100pt">table cell column 3 continue</fo:block>
                  </fo:inline-container></fo:block>
              </fo:list-item-body>
            </fo:list-item>
            <fo:list-item>
              <fo:list-item-label end-indent="label-end()">
                <fo:block>2.</fo:block>
              </fo:list-item-label>
              <fo:list-item-body start-indent="body-start()">
                <fo:block end-indent="140pt" last-line-end-indent="-140pt">loooooooooooong loooooooooooong text table cell column 2<fo:leader
                  leader-pattern="dots" leader-length.optimum="100%" leader-length.minimum="40pt"/><fo:inline-container width="100pt" start-indent="0" end-indent="0" last-line-end-indent="0">
                  <fo:block width="100pt">table cell column 3 continue</fo:block>
                  </fo:inline-container></fo:block>
              </fo:list-item-body>
            </fo:list-item>
            <fo:list-item>
              <fo:list-item-label end-indent="label-end()">
                <fo:block>3.</fo:block>
              </fo:list-item-label>
              <fo:list-item-body start-indent="body-start()">
                <fo:block end-indent="140pt" last-line-end-indent="-140pt">loooooooooooong loooooooooooong loooooooooooong loooooooooooong loooooooooooong loooooooooooong text table cell column 2<fo:leader
                  leader-pattern="dots" leader-length.optimum="100%" leader-length.minimum="40pt"/><fo:inline-container width="100pt" start-indent="0" end-indent="0" last-line-end-indent="0">
                  <fo:block width="100pt">table cell column 3 continue</fo:block>
                  </fo:inline-container></fo:block>
              </fo:list-item-body>
            </fo:list-item>
          </fo:list-block>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
    

    XSL-FO for right-aligned column 3 as requested in comment:

    <fo:block end-indent="140pt" last-line-end-indent="-140pt">loooooooooooong loooooooooooong loooooooooooong loooooooooooong loooooooooooong loooooooooooong text table cell column 2<fo:leader
      leader-pattern="dots" leader-length.optimum="100%" leader-length.minimum="40pt" leader-alignment="end" /><fo:inline-container width="100pt" start-indent="0" end-indent="0" last-line-end-indent="0" text-align="right" axf:text-align-first="justify"
    f:leader-expansion="force">
      <fo:block width="100pt"><fo:leader leader-length.minimum="0pt" leader-pattern="dots"/>table cell column 3 continue</fo:block>
      </fo:inline-container></fo:block>
    

    Screenshot from Antenna House Formatter GUI