Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > BOOK: XSLT Programmer's Reference, 2nd Edition
|
BOOK: XSLT Programmer's Reference, 2nd Edition
This is the forum to discuss the Wrox book XSLT: Programmer's Reference, 2nd Edition by Michael Kay; ISBN: 9780764543814
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: XSLT Programmer's Reference, 2nd Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 20th, 2008, 04:38 PM
Registered User
 
Join Date: May 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Replacing multiple characters in a string

Hi,

Goal: Replace multiple characters in a text string. I want to replace all occurences.
Parser: Altova built-in parser
OS: Windows XP
Issue: First character in the search replaces all occurences
  Second character replaces last character, not first occurence
Third character does not get replaced

Source XML
<root>
<content>Register symbol® Copyright symbol© registration symbol again® trademark symbol™ copyright symbol again©</content>
</root>

Output XML (ignore "_" underscore in numeric character entities)
<root>
 <content>Register symbol&_#174; Copyright symbol© registration symbol again#_174; trademark symbol™ copyright symbol#_169; again</content>
</root>

XSLT
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <!--
-->
<xsl:param name="reg.search" select="'®'"/>
<xsl:param name="reg.replace">
<xsl:text><![CDATA[®]]></xsl:text>
</xsl:param>
<xsl:param name="copy.search" select="'©'"/>
<xsl:param name="copy.replace">
<xsl:text><![CDATA[©]]></xsl:text>
</xsl:param>
<xsl:param name="trade.search" select="'™'"/>
<xsl:param name="trade.replace">
<xsl:text><![CDATA[™]]></xsl:text>
</xsl:param>
    <!--
-->
<xsl:template name="search-replace">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, $reg.search)">
<xsl:value-of select="substring-before($text, $reg.search)"/>
<xsl:value-of select="$reg.replace" disable-output-escaping="yes"/>
<xsl:call-template name="search-replace">
<xsl:with-param name="text" select="substring-after($text, $reg.search)"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($text, $copy.search)">
<xsl:value-of select="substring-before($text, $copy.search)"/>
<xsl:value-of select="$copy.replace" disable-output-escaping="yes"/>
<xsl:call-template name="search-replace">
<xsl:with-param name="text" select="substring-after($text, $copy.search)"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($text, $trade.search)">
<xsl:value-of select="substring-before($text, $trade.search)"/>
<xsl:value-of select="$trade.replace" disable-output-escaping="yes"/>
<xsl:call-template name="search-replace">
<xsl:with-param name="text" select="substring-after($text, $trade.search)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
    <!--
-->
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
    <!--
-->
<xsl:template match="text()">
<xsl:call-template name="search-replace">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:template>
    <!--
-->
</xsl:transform>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Separating strings and replacing characters sunrain XSLT 4 April 7th, 2008 09:33 AM
replacing numbers in a string cole SQL Server 2000 4 April 2nd, 2007 04:24 PM
Replacing characters in a string semilemon C# 2005 2 June 16th, 2006 11:31 PM
Replacing characters in external csv file Axxess Access VBA 2 July 15th, 2005 01:39 AM
Replacing a character from string itHighway Classic ASP Basics 5 March 14th, 2005 11:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.