Hi
I want to search and replace multiple words in a text. I've got a template
that does the search and replace of a word in a text. Now, I want to call
this template for each word in a list of words. If changes are made (words
are replaced) in the text, how can I call the search and replace template
with this updated text the next time? Current I'm calling it with the
original text each time.
<?xml version="1.0"?>
<xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
version="1.0" <xsl:template match="WordList ">
<xsl:apply-templates select="Word"/>
</xsl:template>
<xsl:template match="Word">
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="Text"/<!-- PROBLEM HERE -->
<xsl:with-param name="from" select="'Word'"/>
<xsl:with-param name="to" select="'replac ed'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="from"/>
<xsl:param name="to"/>
<xsl:choose>
<xsl:when test="contains( $text, $from)">
<xsl:variable name="before" select="substri ng-before($text, $from)"/>
<xsl:variable name="after" select="substri ng-after($text, $from)"/>
<xsl:variable name="prefix" select="concat( $before, $to)"/>
<xsl:value-of select="$before "/>
<xsl:value-of select="$to"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$after"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwis e>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
I want to search and replace multiple words in a text. I've got a template
that does the search and replace of a word in a text. Now, I want to call
this template for each word in a list of words. If changes are made (words
are replaced) in the text, how can I call the search and replace template
with this updated text the next time? Current I'm calling it with the
original text each time.
<?xml version="1.0"?>
<xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
version="1.0" <xsl:template match="WordList ">
<xsl:apply-templates select="Word"/>
</xsl:template>
<xsl:template match="Word">
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="Text"/<!-- PROBLEM HERE -->
<xsl:with-param name="from" select="'Word'"/>
<xsl:with-param name="to" select="'replac ed'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="from"/>
<xsl:param name="to"/>
<xsl:choose>
<xsl:when test="contains( $text, $from)">
<xsl:variable name="before" select="substri ng-before($text, $from)"/>
<xsl:variable name="after" select="substri ng-after($text, $from)"/>
<xsl:variable name="prefix" select="concat( $before, $to)"/>
<xsl:value-of select="$before "/>
<xsl:value-of select="$to"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$after"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwis e>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Comment