XSL:用 &apos 替换单引号和双引号;和" [英] XSL: replace single and double quotes with ' and "

查看:52
本文介绍了XSL:用 &apos 替换单引号和双引号;和"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XSL,用于将一个 XML 转换为另一个 XML,例如:

I have a XSL which I am using to transform one XML to another like:

<xsl:value-of select="somestatement"/>

其中一些语句有单引号和双引号.例如:这是一个示例"字符串.我有'单引号'

where some statement has single quotes and double quotes. Ex: This is an "example" string. I have 'single quotes'

我想用 &apos; 替换单引号,用 &quot; 替换双引号,以便输出字符串:

I want to replace single quotes with &apos; and double quotes with &quot; so that output string will be :

This is an &quot;example&quot; string. I have &apos;single quotes&apos;

有人可以为此提出解决方案吗?

Can someone please suggest a solution for this?

提前感谢您的帮助.

推荐答案

您需要为此调用命名递归模板.试试:

You need to call a named recursive template for this. Try:

<xsl:template name="escape-quotes">
    <xsl:param name="text"/>
    <xsl:param name="searchString">'</xsl:param>
    <xsl:param name="replaceString">&amp;apos;</xsl:param>
    <xsl:variable name="apos">'</xsl:variable>  
    <xsl:choose>
        <xsl:when test="contains($text,$searchString)">
            <xsl:call-template name="escape-quotes">
                <xsl:with-param name="text" select="concat(substring-before($text,$searchString), $replaceString, substring-after($text,$searchString))"/>
                <xsl:with-param name="searchString" select="$searchString"/>
                <xsl:with-param name="replaceString" select="$replaceString"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:when test="$searchString=$apos">
            <xsl:call-template name="escape-quotes">
                <xsl:with-param name="text" select="$text"/>
                <xsl:with-param name="searchString">"</xsl:with-param>
                <xsl:with-param name="replaceString">&amp;quot;</xsl:with-param>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text" disable-output-escaping="yes"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

调用模板的例子:

<output>
    <xsl:call-template name="escape-quotes">
        <xsl:with-param name="text">This is an "example" string. I have 'single quotes'.</xsl:with-param>
    </xsl:call-template>
</output>

结果:

<output>This is an &quot;example&quot; string. I have &apos;single quotes&apos;.</output>

这篇关于XSL:用 &amp;apos 替换单引号和双引号;和&amp;quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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