如何替换“单引号"到“双单引号"在 XSLT 中 [英] How to replace "single quote" to "double single quote" in XSLT

查看:63
本文介绍了如何替换“单引号"到“双单引号"在 XSLT 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何替换一个xml值,例如:

How do you replace an xml value, for example:

<name>Linda O'Connel</name>

到:

<name>Linda O''Connel</name>

通过 XSLT?

我需要这个,因为我必须在 powershell 命令行和其他平台中传递这个值,因为需要双单引号"来转义撇号/单引号.

I need this because I have to pass this value in a powershell command line and to other platforms since the "double single quote" is needed to escape the apostrophe/single quotes.

推荐答案

假设使用 XSLT 1.0 处理器,您将需要为此使用递归命名模板,例如:

Assuming an XSLT 1.0 processor, you will need to use a recursive named template for this, e.g:

<xsl:template name="replace">
    <xsl:param name="text"/>
    <xsl:param name="searchString">'</xsl:param>
    <xsl:param name="replaceString">''</xsl:param>
    <xsl:choose>
        <xsl:when test="contains($text,$searchString)">
            <xsl:value-of select="substring-before($text,$searchString)"/>
            <xsl:value-of select="$replaceString"/>
           <!--  recursive call -->
            <xsl:call-template name="replace">
                <xsl:with-param name="text" select="substring-after($text,$searchString)"/>
                <xsl:with-param name="searchString" select="$searchString"/>
                <xsl:with-param name="replaceString" select="$replaceString"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

调用示例:

<xsl:template match="name">
    <xsl:copy>
        <xsl:call-template name="replace">
            <xsl:with-param name="text" select="."/>
        </xsl:call-template>
    </xsl:copy>
</xsl:template>

这篇关于如何替换“单引号"到“双单引号"在 XSLT 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆