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

XSLT 1.0处理引号及管道符的代码问题咨询

解决方案

修改说明

  • 新增全局单引号移除逻辑:通过translate()函数过滤所有文本节点的单引号,满足场景3要求
  • 修复原代码未闭合双引号兼容问题:输入存在单个双引号时也能正常处理不丢失内容
  • 保留原有双引号处理逻辑:双引号内部管道符自动删除,双引号外部管道符不受影响,符合场景1、2要求

完整XSLT 1.0代码

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- 身份转换模板,原样复制所有节点和属性 -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="text()">
    <xsl:call-template name="process">
        <xsl:with-param name="text" select="."/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="process">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text, '&quot;')">
            <!-- 输出第一个双引号前的内容,移除单引号 -->
            <xsl:value-of select="translate(substring-before($text, '&quot;'), '&apos;', '')"/>
            <xsl:variable name="afterFirstQuote" select="substring-after($text, '&quot;')"/>
            <xsl:choose>
                <!-- 存在闭合双引号的场景 -->
                <xsl:when test="contains($afterFirstQuote, '&quot;')">
                    <!-- 输出双引号中间内容,移除管道符和单引号 -->
                    <xsl:value-of select="translate(substring-before($afterFirstQuote, '&quot;'), '|&apos;', '')"/>
                    <!-- 递归处理闭合双引号后的内容 -->
                    <xsl:call-template name="process">
                        <xsl:with-param name="text" select="substring-after($afterFirstQuote, '&quot;')"/>
                    </xsl:call-template>
                </xsl:when>
                <!-- 无闭合双引号的场景 -->
                <xsl:otherwise>
                    <!-- 输出剩余内容,移除单引号 -->
                    <xsl:value-of select="translate($afterFirstQuote, '&apos;', '')"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
            <!-- 无双引号场景,直接移除所有单引号输出 -->
            <xsl:value-of select="translate($text, '&apos;', '')"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

效果验证

使用你提供的输入示例运行该代码,输出完全符合预期。如果输入包含单引号(例如<Node>'test'123</Node>),会自动处理为<Node>test123</Node>。

内容的提问来源于stack exchange,提问作者Sree

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 17:18:02