home contribute faq download

FunctX XSLT Functions

functx:replace-first

Replaces the first match of a pattern

Google
Webdatypic.com

Description

The functx:replace-first function replaces, within $arg, the first area that matches $pattern) with $replacement. If no area matches the pattern, no replacement is made.

Arguments and Return Type

NameTypeDescription
$arg xs:string? the entire string to change
$pattern xs:string the pattern of characters to replace
$replacement xs:string the replacement string
return value xs:string

XSLT Function Declaration

See XQuery definition.
<xsl:function name="functx:replace-first" as="xs:string"
              xmlns:functx="http://www.functx.com">
  <xsl:param name="arg" as="xs:string?"/>
  <xsl:param name="pattern" as="xs:string"/>
  <xsl:param name="replacement" as="xs:string"/>

  <xsl:sequence select="
   replace($arg, concat('(^.*?)', $pattern),
             concat('$1',$replacement))
 "/>

</xsl:function>

Examples

XPath ExampleResults
functx:replace-first('abcabcabc', 'ab', 'x')
xcabcabc
functx:replace-first('elementary', 'e.*e', 'x')
xntary
functx:replace-first(
   'elementary', 'e.*?e', 'x')
xmentary
functx:replace-first('9999-9999', '\d+', 'X')
X-9999
functx:replace-first('9999-9999', '\d{3}', 'X')
X9-9999

See Also

functx:replace-beginningReplaces the beginning of a string, up to a matched pattern
fn:replaceReplaces parts of a string that match a regular expression

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26Priscilla Walmsley, Datypic, pwalmsley@datypic.com, https://www.datypic.com
Datypic XSLT Services

Recommended Reading:

XQuery