1

I want to implement find and replace string using xslt 1.0. The problem is, I have to replace multiple strings with different values.. For example, my input xml is as below

<process xmlns:client="http://xmlns.oracle.com/ErrorHandler" xmlns="http://xmlns.oracle.com/ErrorHandler">
    <client:result>The city name is $key1$ and Country name is $key2$   </client:result>
</process>

result should be

<process xmlns:client="http://xmlns.oracle.com/ErrorHandler" xmlns="http://xmlns.oracle.com/ErrorHandler">
    <client:result>The city name is London and Country name is England  </client:result>
</process>

$key1$ and $key2$ from the input string should be replaced with London and England. I found many examples to find and replace single string, but I am not sure how to replace multiple strings with different value.. Any suggestion?

Thanks in advance

1

1 Answer 1

2

It would be much easier if you could use XSLT 2.0 or higher. It could be as simple as:

replace(replace(., '\$key1\$', 'London'), '\$key2\$', 'England')

However, if you are stuck with XSLT 1.0, you could use a recursive template to perform the replace, and invoke it for each of the tokens you want to replace(using the product of the previous calls as input to the next):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:client="http://xmlns.oracle.com/ErrorHandler"
    version="1.0">

    <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 match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="client:result">
        <xsl:variable name="orig" select="string(.)"/>
        <xsl:variable name="key1">
            <xsl:call-template name="replace-string">
                <xsl:with-param name="text" select="$orig"/>
                <xsl:with-param name="replace" select="'$key1$'"/>
                <xsl:with-param name="with" select="'London'"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="key2">
            <xsl:call-template name="replace-string">
                <xsl:with-param name="text" select="$key1"/>
                <xsl:with-param name="replace" select="'$key2$'"/>
                <xsl:with-param name="with" select="'England'"/>
            </xsl:call-template>
        </xsl:variable>

        <xsl:copy>
            <xsl:value-of select="$key2"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.