1

I have the following function which replaces all occurences of a search-string ($replace) in a string ($text) with another string ($by):

<xsl:template name="string-replace-all">
  <xsl:param name="text" />
  <xsl:param name="replace" />
  <xsl:param name="by" />
  <xsl:choose>
    <xsl:when test="contains($text, $replace)">
      <xsl:value-of select="substring-before($text,$replace)" />
      <xsl:value-of select="$by" />
      <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text"
        select="substring-after($text,$replace)" />
        <xsl:with-param name="replace" select="$replace" />
        <xsl:with-param name="by" select="$by" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$text" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

This works fine for replacing text in individual strings, however it doesn't work when trying to replace text in a node-set.

What I am looking for is a function which takes, for example, the following XML document:

<nodeSet> 
  <node>a1;a2;a3</node> 
  <node>b1;b2;b3</node>
</nodeSet>

and outputs the following:

<nodeSet> 
  <node>a1#a2#a3</node> 
  <node>b1#b2#b3</node>
</nodeSet>  

The following template does the job when the target and replacement strings are known in advance:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl"
>

  <xsl:template name="string-replace-all-in-nodeset">
    <xsl:param name="nodeset" />
    <xsl:apply-templates select="exsl:node-set($nodeset)" mode="str-repl-in-nodeset"/>
  </xsl:template>

  <xsl:template match="*/text()" mode="str-repl-in-nodeset">
    <xsl:call-template name="string-replace-all">
      <xsl:with-param name="text" select="."/>
      <xsl:with-param name="replace" select=" ';' "/>
      <xsl:with-param name="by" select=" '#' "/>
    </xsl:call-template>
  </xsl:template>


  <xsl:template name="string-replace-all">
    <xsl:param name="text" />
    <xsl:param name="replace" />
    <xsl:param name="by" />
    <xsl:choose>
      <xsl:when test="contains($text, $replace)">
        <xsl:value-of select="substring-before($text,$replace)" />
        <xsl:value-of select="$by" />
        <xsl:call-template name="string-replace-all">
          <xsl:with-param name="text" select="substring-after($text,$replace)" />
          <xsl:with-param name="replace" select="$replace" />
          <xsl:with-param name="by" select="$by" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="@*|node()" mode="str-repl-in-nodeset">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" mode="str-repl-in-nodeset"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

However, I need to be able to pass the target and replacement string (';' and '#' in this case) dynamically. Is there any way of passing these parameters to the template matching all text nodes (match="*/text()") or any other way of achieving what I want?

silentsurfer
  • 1,998
  • 2
  • 17
  • 29
  • Where is the `nodeset` variable being set? Is it being passed in as a parameter? – Tim C Mar 21 '16 at 14:10
  • @Tim C, it is indeed. I amended the question accordingly. Thanks for pointing it out. – silentsurfer Mar 21 '16 at 14:14
  • 1
    Why can't you simply use `` instead of ``? If you want to pass parameters around then `apply-templates` allows that the same way as call-template. As you use XSLT 1.0, you only need to make sure all your templates in that mode pass the parameters on. In XSLT 2.0 you could use tunnel parameters instead. – Martin Honnen Mar 21 '16 at 14:21
  • @MartinHonnen I wasn't aware this is actually possible. Would you mind posting your solution? – silentsurfer Mar 21 '16 at 14:34

1 Answers1

3

Here is a stylesheet that defines global parameters for the replace and by strings and then passes them on to all templates in that mode str-repl-in-nodeset:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0"
    xmlns:exsl="http://exslt.org/common"
    exclude-result-prefixes="exsl">

    <xsl:param name="ns1">
        <nodeSet> 
            <node>a1;a2;a3</node> 
            <node>b1;b2;b3</node>
        </nodeSet>
    </xsl:param>

    <xsl:param name="replace" select="';'"/>
    <xsl:param name="by" select="'#'"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/">
        <xsl:apply-templates select="exsl:node-set($ns1)" mode="str-repl-in-nodeset">
            <xsl:with-param name="replace" select="$replace"/>
            <xsl:with-param name="by" select="$by"/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="/ | @* | node()" mode="str-repl-in-nodeset">
        <xsl:param name="replace"/>
        <xsl:param name="by"/>
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" mode="str-repl-in-nodeset">
                <xsl:with-param name="replace" select="$replace"/>
                <xsl:with-param name="by" select="$by"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*/text()" mode="str-repl-in-nodeset">
        <xsl:param name="replace"/>
        <xsl:param name="by"/>
        <xsl:call-template name="string-replace-all">
            <xsl:with-param name="text" select="."/>
            <xsl:with-param name="replace" select="$replace"/>
            <xsl:with-param name="by" select="$by"/>
        </xsl:call-template>
    </xsl:template>


    <xsl:template name="string-replace-all">
        <xsl:param name="text" />
        <xsl:param name="replace" />
        <xsl:param name="by" />
        <xsl:choose>
            <xsl:when test="contains($text, $replace)">
                <xsl:value-of select="substring-before($text,$replace)" />
                <xsl:value-of select="$by" />
                <xsl:call-template name="string-replace-all">
                    <xsl:with-param name="text" select="substring-after($text,$replace)" />
                    <xsl:with-param name="replace" select="$replace" />
                    <xsl:with-param name="by" select="$by" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>


</xsl:transform>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • thanks for your answer. However, I'm afraid hard-coding the parameters in global variables is not an option. I would like to pass them dynamically from another template when calling `string-replace-all-in-nodeset`. I have tried passing the parameters to all templates in the stylesheet by using `with-param`, but I'm now getting a stack overflow. – silentsurfer Mar 21 '16 at 15:04
  • I have used global parameters, not global variables. So the solution allows you to run the code and set the parameters as needed. And of course the `` can be used at any position and with any parameters you want, I only tried to write a self-contained example that performs the transformation you want. – Martin Honnen Mar 21 '16 at 15:41
  • As for the stack overflow you get, make sure your mode has a template matching `/`, as shown, or, make sure you call `apply-templates select="exsl:node-set($rtf)/node()"`. – Martin Honnen Mar 21 '16 at 15:43