views:

333

answers:

2

Hi,

I am using XSLT -> FO to generate the PDF.

When the text in a column exceeds the size of the column and there are no spaces in the text - then the text runs over into the next cell or outside the table.

I want the text to be occupied within the same cell.

Thanx in Advance, Swetha

A: 

If possible truncate the extra letters. Use the length() and substring() functions of xslt. You can also provide a tooltip or something to show the whole text if you truncate it

Rashmi Pandit
But in my case it is amount values i cant truncate the extra letters.
maybe u can then truncate something from the preceding text columns
Rashmi Pandit
+1  A: 

You can split the text into words by inserting zero-width space characters (U+200B, HTML entity &#x200b), then the line breaks will occur at these positions:

<xsl:template name="split_value">
    <xsl:param name="value"/>
    <xsl:param name="max_length"/>
    <xsl:variable name="ret">
        <xsl:value-of select="substring($value, 1, $max_length)"/>
        <xsl:if test="string-length($value) &gt; $max_length">
            <xsl:value-of select="'&#x200b;'"/>
            <xsl:call-template name="split_value">
                <xsl:with-param
                    name="value"
                    select="substring($value, $max_length + 1)"
                />
                <xsl:with-param
                    name="max_length"
                    select="$max_length"
                />
            </xsl:call-template>
        </xsl:if>
    </xsl:variable>
    <xsl:value-of select="$ret"/>
</xsl:template>

Note: you might want to enhance the template so that it splits only pieces of text where no whitespace occurs between $max_length continuous characters.


Here's a test case.

Input XML:

<data>0123456789</data>

Stylesheet (generates HTML):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    exclude-result-prefixes="xsl"
>
<xsl:template match="/">
    <html>
        <body
            style="font-family: Arial; font-size: 12pt; font-weight: normal"
        >
            <table width="4cm">
            <xsl:for-each select="/data">
                <tr><td>
                <xsl:call-template name="split_value">
                    <xsl:with-param
                        name="value"
                        select="text()"
                    />
                    <xsl:with-param
                        name="max_length"
                        select="number(4)"
                    />
                </xsl:call-template>
                </td>
                </tr>
            </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>
Juris
can u give me some idea how to insert these zero-width spaces in my value.my value is an amount eg:23456789087 like this.
Thanks Juris.I got my issue solved with zero width space approach.