简体   繁体   中英

XSLT 1.0: Replacing all occurences of a string in a node-set

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM