Mihai Valcu
Mihai Valcu

Reputation: 21

Empty html columns created with XSL are too wide when file opened in Excel

I am creating a report transforming an XML file into an HTML one by using XSL. In some cases I have a column that is always empty. My problem is that no matter how narrow I set this column, it always comes too wide (usually, when there's some text it's one or 2 letters, in which case the column will be just as wide to accommodate the largest string, which is 3-4 times narrower than when it's empty). Is there a way to make this column very narrow when I don't have data? Here's a portion of the XSL code that creates the HTML file (the code that puts the empty value is the one under "replace empty element with &nbsp"). For the width attribute I tried different values and no matter what value I put, when that column is empty the width of the column is the same (too wide when opened in Excel): " FONT-WEIGHT: bold

        <xsl:choose>
        **<!-- replace empty element with &nbsp; -->
        **<xsl:when test="string-length()= 0">
                        <xsl:attribute name="width">20px</xsl:attribute>
                        <xsl:text disable-output-escaping="yes">&amp;</xsl:text>
                        <xsl:text>nbsp;</xsl:text>**
           </xsl:when>**

       <xsl:when test="contains(name(.), 'Location')">
           <xsl:attribute name="align">left</xsl:attribute>
           <xsl:attribute name="colspan">2</xsl:attribute>
                   <xsl:apply-templates/>
           </xsl:when>

    <xsl:otherwise>
        <xsl:attribute name="align">left</xsl:attribute>
        <xsl:apply-templates/>
    </xsl:otherwise>
    </xsl:choose>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>

Thank you.

Upvotes: 1

Views: 87

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52858

Unless you are using xsl:strip-space on those elements, string-length() will also count whitespace.

Try changing your test to:

test="string-length(normalize-space())=0"

You could also change it to:

test="not(boolean(normalize-space()))"

which I think is easier to understand.

You can also use a decimal (or hex) reference for your non-breaking space (&#160;); that will eliminate the need for the 2 xsl:text and disable-output-escaping.

You could also use a zero width space: &#8203;.

Here's an example of what I would make it look like:

<xsl:when test="not(boolean(normalize-space()))">
    <xsl:text>&#8203;</xsl:text>
</xsl:when>

Upvotes: 0

Related Questions