You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在SpreadsheetML实现Excel换行及Saxon XSLT1.0中node-set()转义行为问题

关于SpreadsheetML中Excel换行的XSLT引擎差异问题(跟进)

这是针对“如何在SpreadsheetML(MSXSLT)中实现Excel换行”问题的跟进,由于不同XSLT引擎行为存在差异,特此单独提出以聚焦功能实现的核心问题。

1. XSLT 1.0(Saxon HE中可生成预期结果)

以下代码在Saxon HE中能正确生成包含
的输出,满足Excel换行需求:

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <root>
            <bar>
                <xsl:text disable-output-escaping="yes">&amp;#10;</xsl:text>
            </bar>
        </root>
    </xsl:template>
</xsl:stylesheet>

对应的输出:

<root>
   <bar>&#10;</bar>
</root>

2. XSLT 1.0(使用exsl:node-set无法生成预期结果)

当使用exsl:node-set处理变量中的节点时,无法保留未转义的&#10;,输出会被二次转义:

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exsl="http://exslt.org/common"
    version="1.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <xsl:variable name="foo">
            <bar>
                <xsl:text disable-output-escaping="yes">&amp;#10;</xsl:text>
            </bar>
        </xsl:variable>
        <root>
            <xsl:copy-of select="exsl:node-set($foo)"/>
        </root>
    </xsl:template>
</xsl:stylesheet>

对应的输出:

<bar>&amp;#10;</bar>

3. 备注(XSLT 3.0的实现情况)

虽然问题针对XSLT 1.0,但XSLT 3.0中可以通过xsl:sequence实现预期效果:

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="3.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <xsl:variable name="foo">
            <bar>
                <xsl:text disable-output-escaping="yes">&amp;#10;</xsl:text>
            </bar>
        </xsl:variable>
        <root>
            <xsl:sequence select="$foo"/>
        </root>
    </xsl:template>
</xsl:stylesheet>

但如果使用xsl:copy-of则无法实现该效果。即便采用sequence模式,在包含call-templates、apply-templates的复杂XSLT场景中,仍需理解节点的解析与序列化逻辑才能保持非转义效果。

内容的提问来源于stack exchange,提问作者MrD at KookerellaLtd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 21:46:06