Reputation: 21
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  "). 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 -->
**<xsl:when test="string-length()= 0">
<xsl:attribute name="width">20px</xsl:attribute>
<xsl:text disable-output-escaping="yes">&</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
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 ( 
); that will eliminate the need for the 2 xsl:text
and disable-output-escaping
.
You could also use a zero width space: ​
.
Here's an example of what I would make it look like:
<xsl:when test="not(boolean(normalize-space()))">
<xsl:text>​</xsl:text>
</xsl:when>
Upvotes: 0