16

I am writing an XSLT transformation in which I wish to use the Replace function to do a regex match and replace.

However, Visual Studio 2008 reports that

'replace()' is an unknown XSLT function.

The bit of code itself is:

<xsl:otherwise>
    <td style="border: solid 1px black; background-color:#00CC66;">
          <xsl:variable name="FeatureInfo" select="Text" />
                <xsl:value-of select="replace($FeatureInfo,'Feature=','TESTING')"/>
     </td>
 </xsl:otherwise>

Is there anything that I am doing wrong?

Thanks :)

Edit: I am using this version of XSLT, but it looks like it is Visual Studio's version that is a problem...I'll have to try to find a workaround.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
Fiona - myaccessible.website
  • 14,481
  • 16
  • 82
  • 117

6 Answers6

33

The replace function is only available in XSLT version 2.0, not in version 1.0 which is what Visual Studio uses. Just because you've specified version="2.0" doesn't mean that Visual Studio supports it.

Here's a template on codesling that implements string-replace in XSLT 1.0. You should be able to use it but I can't vouch for its efficiency.

(Taken from the link above)

<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>

You'd call it like this:

<xsl:otherwise>
  <td style="border: solid 1px black; background-color:#00CC66;">
    <xsl:variable name="FeatureInfo" select="Text" />
    <xsl:call-template name="string-replace-all">
      <xsl:with-param name="text" select="$FeatureInfo"/>
      <xsl:with-param name="replace" select="Feature="/>
      <xsl:with-param name="by" select="TESTING"/>
    </xsl:call-template>
  </td>
</xsl:otherwise>
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Welbog
  • 59,154
  • 9
  • 110
  • 123
  • 4
    You need single quotes in your s, such as select="'TESTING'". – David Rivers Mar 22 '11 at 19:04
  • Correct me if I'm wrong but this could fail if you try to replace with something that contains the same as you are replacing, example would be escaping " with \", this would result in a infinite loop – Peter Jan 05 '22 at 12:28
10

Replace is not valid in XSLT 1.0. You have "translate()", which might work for you, but replace() is XSLT 2, and not part of the MS .NET XML codebase. You can get it with some third party XML libraries though.

DigDoug
  • 306
  • 1
  • 5
7

How about embedding a c# script to do the replacement?

Add the following to the bottom of your stylesheet:

<msxsl:script language="C#" implements-prefix="scr"> <![CDATA[ public string Replace(string stringToModify, string pattern, string replacement) { return stringToModify.Replace(pattern, replacement); } ]]> </msxsl:script>

Add a namespace attribute to the stylesheet element:

xmlns:scr="urn:scr.this"

Then implement as....

<xsl:value-of select="scr:Replace(description/text(), 'ABC', '123')"/>

Note: This is only supported on .NET Framework, Code blocks are not supported on .NET Core and .NET 5

Peter
  • 37,042
  • 39
  • 142
  • 198
Flippsie
  • 472
  • 1
  • 7
  • 19
4

For simple string replacement the translate function (available in xslt 1.0) worked fine for me.

I used it to strip out spaces for numeric values.

mikey
  • 41
  • 1
0

you should have placed the Feature= string between quotes as follows

<xsl:otherwise><td style="border: solid 1px black; background-color:#00CC66;">    <xsl:variable name="FeatureInfo" select="Text" />    <xsl:call-template name="string-replace-all">      <xsl:with-param name="text" select="$FeatureInfo"/>      <xsl:with-param name="replace" select="'Feature='"/>      <xsl:with-param name="by" select="TESTING"/>    </xsl:call-template>  </td></xsl:otherwise>

Thanks
Mina Wissa
  • 10,923
  • 13
  • 90
  • 158
-1

As far as I know, replace() was introduces in XLST 2.0. What is the version definition of your document? Maybe you've to set VS 2008 to use XLST 2.0 (if possible).

Philippe Gerber
  • 17,457
  • 6
  • 45
  • 40