Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xml

xslt

How to use

      fn:replace(string,pattern,replace) 

in XSLT

is it like < fn:replace(...)/>??

like image 944
Thorin Oakenshield Avatar asked Aug 19 '10 10:08

Thorin Oakenshield


1 Answers

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
like image 74
polygenelubricants Avatar answered Sep 19 '22 05:09

polygenelubricants