Search

A new XSLT utility, “String Replace” is now available for download. Comments and feedback can be left here but if you discover any issues, please post it on the issue tracker.

Works great! Just used it to ad a lytebox rel in a blogspot’s xml content node

Just used it to ad a lytebox rel in a blogspot’s xml content node

I guess you could’ve used plain ol’ XML transformations for that. Can you show me your XML and the desired result?

Yes indeed, Made this last year before i got into symphony, so i guess the markup could be more efficient But it works! here’s the tempate:

http://pastie.org/1131340

and the output: www.100procentskateshop.nl

don’t know if i put the disable-output-escaping in the best place though….

Doesn't take into account numbers that start with 0. Updated at this gist

wrong thread, sorry.

Hello,

thanks for this useful utility, phoce. I would like to use it on a formatted text that I outputted this way before:

<xsl:copy-of select="/data/profil/entry/description/*" />

calling your template the same way outputs the first paragraph of my text only:

<xsl:with-param name="haystack" select="/data/profil/entry/description/*" />

Is there a trick do do string replace on a multi paragraph text? Thanks – Moritz

Not sure if that's possible. The utility is meant for strings, while your field returns HTML/XML.

Try something like this...

<xsl:for-each select="/data/profil/entry/description/*">
    <xsl:call-template name="string-replace">
        <xsl:with-param name="haystack" select="."/>
        <xsl:with-param name="search" select="'ab'"/>
        <xsl:with-param name="replace" select="'zz'"/>
    </xsl:call-template>
</xsl:for-each>

Thanks for your idea, Jens. I tried it and failed. Maybe I should use a different approach? I've got a textbox field that will be filled with multiple paragraphs of text and I want to use a variable in the text that will be exhanged against a value in xsl (namely the number of employees).

The trick for applying string replace on multiple paragraphs (formatted text) is to use the utility in a matching template:

<xsl:apply-templates select="/data/profile/entry/description/p" />

<xsl:template match="/data/profile/entry/description/p">
  <p>
    <xsl:call-template name="string-replace">
      <xsl:with-param name="haystack" select="current()" />
      <xsl:with-param name="search" select="ab" />
      <xsl:with-param name="replace" select="xy" />
    </xsl:call-template>
  </p>
</xsl:template>

The trick for applying string replace on multiple paragraphs (formatted text) is to use the utility in a matching template

Isn't this basically the same as my example? Except from the current() instead of the ., which might be the reason mine didn't work.

I also think the problem with this approach is that it doesn't preserve inline markup, like <strong>, <a>, etc.

Now that you say it, yes, it's quite the same… After I used your idea and it did not work I tried this and that and finally oversaw that my solution is basically yours. I switched now back to * instead of p. The crucial part was indeed current() instead of .. Sorry and thank you!

Sorry and thank you!

No need to apologize. I didn't mean to brag about who came up with it first. My intention was to point out that your idea has the same flaws as mine and that our solution might not be such a good solution after all.

You could easily eliminate the flaws using the Ninja technique. I would apply identity transformation to all elements and attributes, and call the replacing template for text nodes. Something like this:

<xsl:apply-templates select="/data/profile/entry/description/*" mode="pimp-my-description"/>

<xsl:template match="*" mode="pimp-my-description">
    <xsl:element name="{name()}">
        <xsl:apply-templates select="* | @* | text()" mode="pimp-my-description"/>
    </xsl:element>
</xsl:template>

<xsl:template match="@*" mode="pimp-my-description">
    <xsl:attribute name="{name()}">
        <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>

<xsl:template match="text()" mode="pimp-my-description">
    <xsl:call-template name="string-replace">
        <xsl:with-param name="haystack" select="." />
        <xsl:with-param name="search" select="ab" />
        <xsl:with-param name="replace" select="xy" />
    </xsl:call-template>
</xsl:template>

This is untested.

You may also introduce less and more specifically matching templates for text nodes.

However, as soon as you need to search/replace several different strings, I suggest to switch to the EXSLT str:replace function resp. template. That will make your life much easier, because you can pass all search strings and replacements at once.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details