Replace() multiple strings Options
chris
Posted: Tuesday, February 05, 2008 3:08:28 PM
Rank: Devotee

Joined: 4/12/2007
Posts: 93
Location: Amsterdam
Hi all!
I have this bit of code to replace certain words in a string.
Code:
<xsl:value-of select="umbraco.library:Replace($KeywordsRAW,'passeert','')"/>

The problem I have that this I don't know how I should check on MULTIPLE (different) words in for example my bodytext.
This anybody knows how to do this??
thanks in advance,
Chris
mortenbock
Posted: Tuesday, February 05, 2008 5:21:57 PM

Rank: Addict

Joined: 7/19/2006
Posts: 790
Location: Århus, Denmark
I guess you could do it like this:

Code:
<xsl:value-of select="umbraco.library:Replace(umbraco.library:Replace($KeywordsRAW,'MyFirstWord',''),'MySecondWord','')"/>


It's pretty repetitive and not very pretty... How many different words are we talking here? You could probably take advantage of XSLT's template syntax to do this as well...

Morten Bock - Level 2 certified - MVP 2008/2009 - My danish blog with a few english posts

mortenbock
Posted: Tuesday, February 05, 2008 11:24:33 PM

Rank: Addict

Joined: 7/19/2006
Posts: 790
Location: Århus, Denmark
I just played around with it a bit, and came up with this example. It does concatenation instead of replace action, but it proves the point.

The xml that it uses it this:
<root>
<value>test1</value>
<value>test2</value>
<value>test3</value>
</root>

Code:

  <xsl:template match="/">
    <xsl:variable name="allnodes" select="root" />
    <xsl:call-template name="myloop">
      <xsl:with-param name="test" select="$allnodes/value" />
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="myloop">
    <xsl:param name="test" />
    <xsl:param name="mystring" />
    <xsl:variable name="remnodes" select="$test[position() &gt; 1]" />
    <xsl:choose>
      <xsl:when test="count($test) &gt; 1">
        <xsl:variable name="newstring" select="concat($mystring,concat($test[position() = 1],','))" />
        <xsl:call-template name="myloop">
          <xsl:with-param name="test" select="$remnodes" />
          <xsl:with-param name="mystring" select="$newstring" />
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="count($test) = 1">
        <xsl:variable name="newstring" select="concat($mystring,$test[position() = 1])" />
        <xsl:call-template name="myloop">
          <xsl:with-param name="test" select="$remnodes" />
          <xsl:with-param name="mystring" select="$newstring" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$mystring"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>



Can you follow what is happening here?

Morten Bock - Level 2 certified - MVP 2008/2009 - My danish blog with a few english posts

drobar
Posted: Tuesday, February 05, 2008 11:34:44 PM

Rank: Umbracoholic

Joined: 9/8/2006
Posts: 1,698
Location: KY, USA
This might be a good use of a small C# function right in the xslt file. Maybe using regex?

cheers,
doug.

MVP 2007-2009 - Official Umbraco Trainer for North America - Percipient Studios
chris
Posted: Wednesday, February 06, 2008 3:53:30 PM
Rank: Devotee

Joined: 4/12/2007
Posts: 93
Location: Amsterdam
Thanks for holding my hand! I've got it working!
greetings,
Chris
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.