You need to enable JavaScript to run this app. 如何在XSLT中处理CDATA内的HTML实体编码实现预期转义输出
优惠活动
大模型
产品
解决方案
定价
更多

如何在XSLT中处理CDATA内的HTML实体编码实现预期转义输出

问题分析

核心问题是输入XML的<bl>节点CDATA内为双重转义的HTML实体,直接使用disable-output-escaping仅能输出原始实体字符串,无法完成实体到实际字符、标签的解码。同时手动拼接CDATA标记的写法不符合XSLT规范,容易产生转义冲突。

方案1:全兼容XSLT 1.0 实现

适配所有XSLT 1.0处理器,通过字符串替换模板完成实体解码,同时使用XSLT原生的cdata-section-elements属性自动生成CDATA块,避免手动拼接:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- 指定text元素的内容自动包裹在CDATA中 -->
    <xsl:output method="xml" indent="yes" cdata-section-elements="text"/>

    <!-- 通用字符串替换模板 -->
    <xsl:template name="replace-string">
        <xsl:param name="text"/>
        <xsl:param name="replace"/>
        <xsl:param name="with"/>
        <xsl:choose>
            <xsl:when test="contains($text,$replace)">
                <xsl:value-of select="substring-before($text,$replace)"/>
                <xsl:value-of select="$with"/>
                <xsl:call-template name="replace-string">
                    <xsl:with-param name="text" select="substring-after($text,$replace)"/>
                    <xsl:with-param name="replace" select="$replace"/>
                    <xsl:with-param name="with" select="$with"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- 实体解码模板 -->
    <xsl:template name="decode-entities">
        <xsl:param name="input"/>
        <!-- 替换< -->
        <xsl:variable name="step1">
            <xsl:call-template name="replace-string">
                <xsl:with-param name="text" select="$input"/>
                <xsl:with-param name="replace" select="'&amp;#60;'"/>
                <xsl:with-param name="with" select="'&lt;'"/>
            </xsl:call-template>
        </xsl:variable>
        <!-- 替换> -->
        <xsl:variable name="step2">
            <xsl:call-template name="replace-string">
                <xsl:with-param name="text" select="$step1"/>
                <xsl:with-param name="replace" select="'&amp;#62;'"/>
                <xsl:with-param name="with" select="'&gt;'"/>
            </xsl:call-template>
        </xsl:variable>
        <!-- 替换ü -->
        <xsl:variable name="step3">
            <xsl:call-template name="replace-string">
                <xsl:with-param name="text" select="$step2"/>
                <xsl:with-param name="replace" select="'&amp;#252;'"/>
                <xsl:with-param name="with" select="'ü'"/>
            </xsl:call-template>
        </xsl:variable>
        <!-- 有其他需要解码的实体可以按照以上格式新增步骤 -->
        <xsl:value-of select="$step3"/>
    </xsl:template>

    <xsl:template match="re">
        <text>
            <xsl:call-template name="decode-entities">
                <xsl:with-param name="input" select="bl"/>
            </xsl:call-template>
        </text>
    </xsl:template>
</xsl:stylesheet>

方案2:XSLT 2.0/3.0 简化实现

如果使用支持XSLT 2.0及以上的处理器(如Saxon),可以直接用正则匹配完成所有数字实体的通用解码,代码更简洁:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" cdata-section-elements="text"/>

    <xsl:template match="re">
        <text>
            <xsl:analyze-string select="bl" regex="&amp;#(\d+);">
                <xsl:matching-substring>
                    <xsl:value-of select="codepoints-to-string(number(regex-group(1)))"/>
                </xsl:matching-substring>
                <xsl:non-matching-substring>
                    <xsl:value-of select="."/>
                </xsl:non-matching-substring>
            </xsl:analyze-string>
        </text>
    </xsl:template>
</xsl:stylesheet>

两种方案都可以直接输出符合要求的预期结果。

内容的提问来源于stack exchange,提问作者Alexander S.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 06:42:03