Elrond
Elrond

Reputation: 499

xsl:fo don't split word into 2 lines

I want to stop splitting a generated word into two lines . Now I tried to use wrap-option="wrap" but nothing works. I hope some can help me ;)

I use Saxon-HE,xslt 2.0

My xml-file:

<root>
     <out>
          invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. 
          <build>
               <name>John</name>
               <year>29</year>
               <address>London</address>
               <code>12345678902331234313123123123123123123</code>
          </build>At vero eos et    
     </out>
</root>

My xslt-file:

 <xsl:template match="out">
        <fo:block>
               <xsl:apply-templates/>
        </fo:block>
    </xsl:template>

    <xsl:template match="build">
        <fo:inline wrap-option="wrap" color="Red">
            <xsl:value-of select="concat(./name,'-',./year,'-',./address,'+',./code)"/>
        </fo:inline>
    </xsl:template>

My expected output is following:

Invidunt ut labore et dolore magna aliquyam erat,sed diam voluptua
John-29-London+11231231231...

Output with my solution:

Invidunt ut labore et dolore magna aliquyam erat,sed diam voluptua John-29
-London+123123..

Upvotes: 2

Views: 2571

Answers (1)

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22617

If those are the only templates in your stylesheet, text nodes are handled by the built-in template. You should not output text in such an uncontrolled way. If you add an empty template matching text() (as I have told you already), the text inside build appears on a single line.

XSLT Stylesheet

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

    <xsl:template match="/root">
      <fo:root>
        <fo:layout-master-set>
          <fo:simple-page-master master-name="page"
            page-height="297mm" page-width="210mm"
            margin-top="20mm" margin-bottom="10mm"
            margin-left="25mm" margin-right="25mm">
            <fo:region-body
              margin-top="0mm" margin-bottom="15mm"
              margin-left="0mm" margin-right="0mm"/>
            <fo:region-after extent="10mm"/>
          </fo:simple-page-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="out">
        <fo:block>
               <xsl:apply-templates/>
        </fo:block>
    </xsl:template>

    <xsl:template match="build">
        <fo:inline wrap-option="wrap" color="Red">
            <xsl:value-of select="concat(name,'-',year,'-',address,'+',code)"/>
        </fo:inline>
    </xsl:template>

    <xsl:template match="text()"/>
</xsl:transform>

XSL-FO Output

<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:layout-master-set>
      <fo:simple-page-master master-name="page"
                             page-height="297mm"
                             page-width="210mm"
                             margin-top="20mm"
                             margin-bottom="10mm"
                             margin-left="25mm"
                             margin-right="25mm">
         <fo:region-body margin-top="0mm"
                         margin-bottom="15mm"
                         margin-left="0mm"
                         margin-right="0mm"/>
         <fo:region-after extent="10mm"/>
      </fo:simple-page-master>
   </fo:layout-master-set>
   <fo:page-sequence master-reference="page">
      <fo:flow flow-name="xsl-region-body">
         <fo:block>
            <fo:inline wrap-option="wrap" color="Red">John-29-London+12345678902331234313123123123123123123</fo:inline>
         </fo:block>
      </fo:flow>
   </fo:page-sequence>
</fo:root>

Rendered PDF Output

enter image description here


But the real question is, how to prevent text from wrapping if it is actually longer than one line, e.g. if the input looked like

<code>123456789023312343131231231248364387438463846837483643123123123</code>

then, a well-placed keep achieves what you want:

<xsl:template match="build">
    <fo:inline keep-together.within-line="always" color="Red">
        <xsl:value-of select="concat(./name,'-',./year,'-',./address,'+',./code)"/>
    </fo:inline>
</xsl:template>

and then the line of text just overflows the page border

enter image description here

and finally, the keep-together, but without the template matching text():

enter image description here

Upvotes: 5

Related Questions