How to use fn:replace(string,pattern,replace) in XSLT

The function is specified as follows:

fn:replace($input, $pattern, $replacement, [$flags])

$input        xs:string?  the string to change
$pattern      xs:string   regular expression to match the areas to be replaced
$replacement  xs:string   the replacement string
$flags        xs:string   flags for multiline mode, case insensitivity, etc
return value  xs:string

Note that $pattern is a regular expression, and the replacement string also has some special substitution syntax.

Here are some examples:

# simple replacement
replace('query', 'r', 'as')               queasy

# character class
replace('query', '[ry]', 'l')             quell

# capturing group substitution
replace('abc123', '([a-z])', '$1x')       axbxcx123

# practical example
replace('2315551212',                     (231) 555-1212
    '(\d{3})(\d{3})(\d{4})',
    '($1) $2-$3'
)

References

  • xqueryfunctions.com - Strings - fn:replace
  • w3.org/XPath Functions - fn:replace, Flags
  • regular-expressions.info - a good tutorial

I think you do it this way:

<xsl:value-of select="fn:replace(value, 'some-pattern', 'with some text')" />

Edit:

Found this question on stackoverflow

Tags:

Xml

Xslt