Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT 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
  #1 (permalink)  
Old February 17th, 2006, 04:14 PM
Registered User
 
Join Date: Feb 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Multiple String Replace

Hi,

I am new to XSLT and I want to do the following:

From a document that looks like this:

<content>
  <text>This is the var1 I want to var2</text>
  <replacements>
    <word>
      <from>var1</from>
      <to>text</to>
    </word>
    <word>
      <from>var2</from>
      <to>replace</to>
    </word>
  </replacements
</content>

I want to replace the 'from' element with the 'to' element on the 'text' element.

Any help will be appreciated.

  #2 (permalink)  
Old February 17th, 2006, 04:38 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

String replacement is easier in XSLT 2.0 than in 1.0.

Start with http://www.dpawson.co.uk/xsl/sect2/replace.html

Note also that there's a useful string-replace template at www.exslt.org.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
  #3 (permalink)  
Old February 20th, 2006, 03:41 PM
Registered User
 
Join Date: Feb 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanx

  #4 (permalink)  
Old May 19th, 2008, 10:46 PM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mhkay, all,

I tried your xslt in the xlst 2.0 wrox book. How do I extend to multiple characters? For example "copyright" symbol as well.

My source:
<root>
<content>Register symbol® Copyright symbol© registration symbol again® trademark symbol™ copyright symbol again©</content>
</root>

My output:
<root>
<content>Register symbol#174; Copyright symbol© registration symbol again#174; trademark symbol™ copyright symbol again©</content>
</root>

Your XSL (slighly modified)

<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[#174;]]></xsl:text>
    </xsl:param>
    <xsl:param name="copy.search" select="'©'"/>
    <xsl:param name="copy.replace">
        <xsl:text><![CDATA[#153;]]></xsl:text>
    </xsl:param>
    <!--
-->
    <xsl:template name="search-replace">
        <xsl:param name="text"/>
          <xsl:param name="search" select="document('replace.xml')/*/replace/from" />
        <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: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>


  #5 (permalink)  
Old May 19th, 2008, 10:48 PM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One tag not needed. Forgot to omit

<xsl:param name="search" select="document('replace.xml')/*/replace/from" />

  #6 (permalink)  
Old May 20th, 2008, 02:47 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Are you using XSLT 1.0 or 2.0? In 2.0, it's much easier to use the replace() function.

With the 1.0 solution, you basically need to call the search-replace template once for each from/to replacement pair. If you know statically how many replacements are needed (in your case two) then just call the template twice. If the number of replacements is a dynamic list, then you need another level of iteration, typically implemented using head-tail recursion.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help in instruction string-replace hugoscp XML 9 October 20th, 2006 12:07 PM
Replace multiple instances of a String interrupt Javascript How-To 2 June 14th, 2005 06:48 AM
replace string value str33ts XSLT 1 April 22nd, 2005 04:01 AM
how to replace a string with another string/number crmpicco Javascript How-To 4 March 14th, 2005 12:59 PM
String.replace() method JohnD Javascript 0 August 20th, 2004 06:46 AM





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