在 Excel 中打开文件时,使用 XSL 创建的空 html 列太宽



我正在使用XSL创建一个将XML文件转换为HTML文件的报告。在某些情况下,我有一列总是空的。我的问题是,无论我设置此列有多窄,它总是太宽(通常,当有一些文本时,它是一个或两个字母,在这种情况下,该列将同样宽以容纳最大的字符串,这比空时窄 3-4 倍)。当我没有数据时,有没有办法使此列非常窄?下面是创建 HTML 文件的 XSL 代码的一部分(放置空值的代码是"将空元素替换为 &nbsp"下的代码)。对于宽度属性,我尝试了不同的值,无论我输入什么值,当该列为空时,列的宽度是相同的(在Excel中打开时太宽):" 字体粗细:粗体

        <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>

谢谢。

除非您在这些元素上使用xsl:strip-space,否则string-length()也会计算空格。

尝试将测试更改为:

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

您也可以将其更改为:

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

我认为这更容易理解。

你也可以对不间断空格(&#160;)使用十进制(或十六进制)引用;这将消除对2个xsl:textdisable-output-escaping的需要。

您也可以使用零宽度空格:&#8203;

下面是一个我让它看起来像什么的例子:

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

最新更新