Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT Replace function not found

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">
like image 275
Fiona - myaccessible.website Avatar asked Jul 01 '09 13:07

Fiona - myaccessible.website


People also ask

What is the function of XSLT replace?

Definition of XSLT replace Function XSLT replace is deterministic and does string manipulation that replaces a sequence of characters defined inside a string that matches an expression. In simple terms, it does string substitution in the specified place by replacing any substrings.

How to remove unwanted characters from a string in XSLT?

With the help of the XSLT2.0 replace () function removes undesired characters from a string in powerful regular expressions. XSLT replaces corresponding single characters, not the entire string.

How do I replace a string in XPath?

Because string manipulation is a common task in transforming documents, XPath 2.0 provides the very useful replace () function. This lets us provide the original string, the string we want to replace, and the string we want substituted in its place. To review our earlier example, we want to make three replacements in our text:

How does the replace function work in Python?

The Replace function works by writing an XSLT template for this function and calls this replace whenever replacement of the string action is required. This function is very similar to regex-group ().


2 Answers

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>
like image 158
Welbog Avatar answered Sep 21 '22 19:09

Welbog


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.

like image 21
DigDoug Avatar answered Sep 19 '22 19:09

DigDoug